Commit 97ec259891

97ec25989196564f7080fa5bf2e54b76a9838a7a

parent: c3d1c0fecf

Unsigned

Wraient <wraient.911@gmail.com> · 2025-02-22 20:06 UTC

Add: Configure menu order

Layout: unified · split

internal/config.go +115 −66
@@ -4,11 +4,11 @@ import (
4 "bufio" 4 "bufio"
5 "fmt" 5 "fmt"
6 "os" 6 "os"
7 "os/exec"
7 "path/filepath" 8 "path/filepath"
8 "reflect" 9 "reflect"
9 "strconv"
10 "os/exec"
11 "runtime" 10 "runtime"
11 "strconv"
12 "strings" 12 "strings"
13) 13)
14 14
@@ -20,6 +20,7 @@ type CurdConfig struct {
20 SubOrDub string `config:"SubOrDub"` 20 SubOrDub string `config:"SubOrDub"`
21 StoragePath string `config:"StoragePath"` 21 StoragePath string `config:"StoragePath"`
22 AnimeNameLanguage string `config:"AnimeNameLanguage"` 22 AnimeNameLanguage string `config:"AnimeNameLanguage"`
23 MenuOrder string `config:"MenuOrder"`
23 PercentageToMarkComplete int `config:"PercentageToMarkComplete"` 24 PercentageToMarkComplete int `config:"PercentageToMarkComplete"`
24 NextEpisodePrompt bool `config:"NextEpisodePrompt"` 25 NextEpisodePrompt bool `config:"NextEpisodePrompt"`
25 SkipOp bool `config:"SkipOp"` 26 SkipOp bool `config:"SkipOp"`
@@ -44,6 +45,7 @@ func defaultConfigMap() map[string]string {
44 "StoragePath": "$HOME/.local/share/curd", 45 "StoragePath": "$HOME/.local/share/curd",
45 "AnimeNameLanguage": "english", 46 "AnimeNameLanguage": "english",
46 "SubsLanguage": "english", 47 "SubsLanguage": "english",
48 "MenuOrder": "CURRENT,ALL,UNTRACKED,UPDATE,CONTINUE_LAST",
47 "SubOrDub": "sub", 49 "SubOrDub": "sub",
48 "PercentageToMarkComplete": "85", 50 "PercentageToMarkComplete": "85",
49 "NextEpisodePrompt": "false", 51 "NextEpisodePrompt": "false",
@@ -178,73 +180,73 @@ func createDefaultConfig(path string) error {
178} 180}
179 181
180func getTokenFromTempFile(isWindowsPlatform bool) (string, error) { 182func getTokenFromTempFile(isWindowsPlatform bool) (string, error) {
181 // Create a temporary file for the token 183 // Create a temporary file for the token
182 tempFile, err := os.CreateTemp("", "curd-token-*.txt") 184 tempFile, err := os.CreateTemp("", "curd-token-*.txt")
183 if err != nil { 185 if err != nil {
184 return "", fmt.Errorf("error creating temp file: %v", err) 186 return "", fmt.Errorf("error creating temp file: %v", err)
185 } 187 }
186 tempPath := tempFile.Name() 188 tempPath := tempFile.Name()
187 tempFile.Close() 189 tempFile.Close()
188 190
189 // Write instructions to the temp file 191 // Write instructions to the temp file
190 instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + 192 instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" +
191 "Replace this text with your token and save the file.\n" 193 "Replace this text with your token and save the file.\n"
192 if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { 194 if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil {
193 return "", fmt.Errorf("error writing instructions: %v", err) 195 return "", fmt.Errorf("error writing instructions: %v", err)
194 } 196 }
195 197
196 // Open the file with appropriate editor 198 // Open the file with appropriate editor
197 var cmd *exec.Cmd 199 var cmd *exec.Cmd
198 if isWindowsPlatform { 200 if isWindowsPlatform {
199 cmd = exec.Command("notepad.exe", tempPath) 201 cmd = exec.Command("notepad.exe", tempPath)
200 } else { 202 } else {
201 editor := os.Getenv("EDITOR") 203 editor := os.Getenv("EDITOR")
202 if editor == "" { 204 if editor == "" {
203 editor = "nano" // Default to nano if $EDITOR is not set 205 editor = "nano" // Default to nano if $EDITOR is not set
204 } 206 }
205 cmd = exec.Command(editor, tempPath) 207 cmd = exec.Command(editor, tempPath)
206 cmd.Stdin = os.Stdin 208 cmd.Stdin = os.Stdin
207 cmd.Stdout = os.Stdout 209 cmd.Stdout = os.Stdout
208 cmd.Stderr = os.Stderr 210 cmd.Stderr = os.Stderr
209 } 211 }
210 212
211 if err := cmd.Run(); err != nil { 213 if err := cmd.Run(); err != nil {
212 return "", fmt.Errorf("error opening editor: %v", err) 214 return "", fmt.Errorf("error opening editor: %v", err)
213 } 215 }
214 216
215 // Read the token from the file 217 // Read the token from the file
216 content, err := os.ReadFile(tempPath) 218 content, err := os.ReadFile(tempPath)
217 if err != nil { 219 if err != nil {
218 return "", fmt.Errorf("error reading token: %v", err) 220 return "", fmt.Errorf("error reading token: %v", err)
219 } 221 }
220 222
221 // Clean up the temp file 223 // Clean up the temp file
222 os.Remove(tempPath) 224 os.Remove(tempPath)
223 225
224 // Extract token (remove instructions and whitespace) 226 // Extract token (remove instructions and whitespace)
225 return strings.TrimSpace(string(content)), nil 227 return strings.TrimSpace(string(content)), nil
226} 228}
227 229
228func ChangeToken(config *CurdConfig, user *User) { 230func ChangeToken(config *CurdConfig, user *User) {
229 var err error 231 var err error
230 tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") 232 tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token")
231 233
232 switch { 234 switch {
233 case runtime.GOOS == "darwin" || runtime.GOOS == "windows": 235 case runtime.GOOS == "darwin" || runtime.GOOS == "windows":
234 user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") 236 user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows")
235 case config.RofiSelection: 237 case config.RofiSelection:
236 user.Token, err = GetTokenFromRofi() 238 user.Token, err = GetTokenFromRofi()
237 default: 239 default:
238 fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") 240 fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token")
239 fmt.Scanln(&user.Token) 241 fmt.Scanln(&user.Token)
240 } 242 }
241 243
242 if err != nil { 244 if err != nil {
243 Log("Error getting user input: "+err.Error()) 245 Log("Error getting user input: " + err.Error())
244 ExitCurd(err) 246 ExitCurd(err)
245 } 247 }
246 248
247 WriteTokenToFile(user.Token, tokenPath) 249 WriteTokenToFile(user.Token, tokenPath)
248} 250}
249 251
250// Load config file from disk into a map (key=value format) 252// Load config file from disk into a map (key=value format)
@@ -331,3 +333,50 @@ func populateConfig(configMap map[string]string) CurdConfig {
331 333
332 return config 334 return config
333} 335}
336
337func getOrderedCategories(userCurdConfig *CurdConfig) map[string]string {
338 // Define the default categories and their labels
339 defaultOrder := []string{"CURRENT", "ALL", "UNTRACKED", "UPDATE", "CONTINUE_LAST"}
340 defaultLabels := map[string]string{
341 "CURRENT": "Currently Watching",
342 "ALL": "Show All",
343 "UNTRACKED": "Untracked Watching",
344 "UPDATE": "Update (Episode, Status, Score)",
345 "CONTINUE_LAST": "Continue Last Session",
346 }
347
348 // Create ordered map to store final result
349 finalOrder := make([]string, 0)
350 seen := make(map[string]bool)
351
352 // If no menu order specified, use default order
353 if userCurdConfig.MenuOrder == "" {
354 finalOrder = defaultOrder
355 } else {
356 // First, process user-specified order
357 menuItems := strings.Split(userCurdConfig.MenuOrder, ",")
358 for _, key := range menuItems {
359 key = strings.TrimSpace(key)
360 if _, exists := defaultLabels[key]; exists && !seen[key] {
361 finalOrder = append(finalOrder, key)
362 seen[key] = true
363 }
364 }
365
366 // Add remaining default items at the end
367 for _, key := range defaultOrder {
368 if !seen[key] {
369 finalOrder = append(finalOrder, key)
370 seen[key] = true
371 }
372 }
373 }
374
375 // Create the final ordered map
376 orderedMap := make(map[string]string)
377 for _, key := range finalOrder {
378 orderedMap[key] = defaultLabels[key]
379 }
380
381 return orderedMap
382}
internal/curd.go +1 −7
@@ -693,13 +693,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
693 } 693 }
694 } else { 694 } else {
695 // Create category selection map 695 // Create category selection map
696 categories := map[string]string{ 696 categories := getOrderedCategories(userCurdConfig)
697 "CURRENT": "Currently Watching",
698 "ALL": "Show All",
699 "UNTRACKED": "Untracked Watching",
700 "UPDATE": "Update (Episode, Status, Score)",
701 "CONTINUE_LAST": "Continue Last Session",
702 }
703 697
704 // Select category using DynamicSelect 698 // Select category using DynamicSelect
705 var err error 699 var err error
internal/selection_menu.go +22 −3
@@ -428,18 +428,37 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
428 428
429// DynamicSelect displays a simple selection prompt without extra features 429// DynamicSelect displays a simple selection prompt without extra features
430func DynamicSelect(options map[string]string, addnewoption bool) (SelectionOption, error) { 430func DynamicSelect(options map[string]string, addnewoption bool) (SelectionOption, error) {
431
432 if GetGlobalConfig().RofiSelection { 431 if GetGlobalConfig().RofiSelection {
433 return RofiSelect(options, addnewoption) 432 return RofiSelect(options, addnewoption)
434 } 433 }
435 434
435 // Create a slice to maintain order
436 var orderedOptions []SelectionOption
437 for key, value := range options {
438 orderedOptions = append(orderedOptions, SelectionOption{
439 Key: key,
440 Label: value,
441 })
442 }
443
436 model := &Model{ 444 model := &Model{
437 options: options, 445 options: options,
438 filteredKeys: make([]SelectionOption, 0), 446 filteredKeys: orderedOptions,
439 addNewOption: addnewoption, 447 addNewOption: addnewoption,
440 } 448 }
441 449
442 model.filterOptions() 450 if addnewoption {
451 model.filteredKeys = append(model.filteredKeys, SelectionOption{
452 Label: "Add new anime",
453 Key: "add_new",
454 })
455 }
456
457 model.filteredKeys = append(model.filteredKeys, SelectionOption{
458 Label: "Quit",
459 Key: "-1",
460 })
461
443 p := tea.NewProgram(model) 462 p := tea.NewProgram(model)
444 463
445 finalModel, err := p.Run() 464 finalModel, err := p.Run()