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 (
44 "bufio"
55 "fmt"
66 "os"
7 "os/exec"
78 "path/filepath"
89 "reflect"
9 "strconv"
10 "os/exec"
1110 "runtime"
11 "strconv"
1212 "strings"
1313)
1414
@@ -20,6 +20,7 @@ type CurdConfig struct {
2020 SubOrDub string `config:"SubOrDub"`
2121 StoragePath string `config:"StoragePath"`
2222 AnimeNameLanguage string `config:"AnimeNameLanguage"`
23 MenuOrder string `config:"MenuOrder"`
2324 PercentageToMarkComplete int `config:"PercentageToMarkComplete"`
2425 NextEpisodePrompt bool `config:"NextEpisodePrompt"`
2526 SkipOp bool `config:"SkipOp"`
@@ -44,6 +45,7 @@ func defaultConfigMap() map[string]string {
4445 "StoragePath": "$HOME/.local/share/curd",
4546 "AnimeNameLanguage": "english",
4647 "SubsLanguage": "english",
48 "MenuOrder": "CURRENT,ALL,UNTRACKED,UPDATE,CONTINUE_LAST",
4749 "SubOrDub": "sub",
4850 "PercentageToMarkComplete": "85",
4951 "NextEpisodePrompt": "false",
@@ -178,73 +180,73 @@ func createDefaultConfig(path string) error {
178180}
179181
180182func getTokenFromTempFile(isWindowsPlatform bool) (string, error) {
181 // Create a temporary file for the token
182 tempFile, err := os.CreateTemp("", "curd-token-*.txt")
183 if err != nil {
184 return "", fmt.Errorf("error creating temp file: %v", err)
185 }
186 tempPath := tempFile.Name()
187 tempFile.Close()
188
189 // 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" +
191 "Replace this text with your token and save the file.\n"
192 if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil {
193 return "", fmt.Errorf("error writing instructions: %v", err)
194 }
195
196 // Open the file with appropriate editor
197 var cmd *exec.Cmd
198 if isWindowsPlatform {
199 cmd = exec.Command("notepad.exe", tempPath)
200 } else {
201 editor := os.Getenv("EDITOR")
202 if editor == "" {
203 editor = "nano" // Default to nano if $EDITOR is not set
204 }
205 cmd = exec.Command(editor, tempPath)
206 cmd.Stdin = os.Stdin
207 cmd.Stdout = os.Stdout
208 cmd.Stderr = os.Stderr
209 }
210
211 if err := cmd.Run(); err != nil {
212 return "", fmt.Errorf("error opening editor: %v", err)
213 }
214
215 // Read the token from the file
216 content, err := os.ReadFile(tempPath)
217 if err != nil {
218 return "", fmt.Errorf("error reading token: %v", err)
219 }
220
221 // Clean up the temp file
222 os.Remove(tempPath)
223
224 // Extract token (remove instructions and whitespace)
225 return strings.TrimSpace(string(content)), nil
183 // Create a temporary file for the token
184 tempFile, err := os.CreateTemp("", "curd-token-*.txt")
185 if err != nil {
186 return "", fmt.Errorf("error creating temp file: %v", err)
187 }
188 tempPath := tempFile.Name()
189 tempFile.Close()
190
191 // Write instructions to the temp file
192 instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" +
193 "Replace this text with your token and save the file.\n"
194 if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil {
195 return "", fmt.Errorf("error writing instructions: %v", err)
196 }
197
198 // Open the file with appropriate editor
199 var cmd *exec.Cmd
200 if isWindowsPlatform {
201 cmd = exec.Command("notepad.exe", tempPath)
202 } else {
203 editor := os.Getenv("EDITOR")
204 if editor == "" {
205 editor = "nano" // Default to nano if $EDITOR is not set
206 }
207 cmd = exec.Command(editor, tempPath)
208 cmd.Stdin = os.Stdin
209 cmd.Stdout = os.Stdout
210 cmd.Stderr = os.Stderr
211 }
212
213 if err := cmd.Run(); err != nil {
214 return "", fmt.Errorf("error opening editor: %v", err)
215 }
216
217 // Read the token from the file
218 content, err := os.ReadFile(tempPath)
219 if err != nil {
220 return "", fmt.Errorf("error reading token: %v", err)
221 }
222
223 // Clean up the temp file
224 os.Remove(tempPath)
225
226 // Extract token (remove instructions and whitespace)
227 return strings.TrimSpace(string(content)), nil
226228}
227229
228230func ChangeToken(config *CurdConfig, user *User) {
229 var err error
230 tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token")
231
232 switch {
233 case runtime.GOOS == "darwin" || runtime.GOOS == "windows":
234 user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows")
235 case config.RofiSelection:
236 user.Token, err = GetTokenFromRofi()
237 default:
238 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)
240 }
241
242 if err != nil {
243 Log("Error getting user input: "+err.Error())
244 ExitCurd(err)
245 }
246
247 WriteTokenToFile(user.Token, tokenPath)
231 var err error
232 tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token")
233
234 switch {
235 case runtime.GOOS == "darwin" || runtime.GOOS == "windows":
236 user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows")
237 case config.RofiSelection:
238 user.Token, err = GetTokenFromRofi()
239 default:
240 fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token")
241 fmt.Scanln(&user.Token)
242 }
243
244 if err != nil {
245 Log("Error getting user input: " + err.Error())
246 ExitCurd(err)
247 }
248
249 WriteTokenToFile(user.Token, tokenPath)
248250}
249251
250252// Load config file from disk into a map (key=value format)
@@ -331,3 +333,50 @@ func populateConfig(configMap map[string]string) CurdConfig {
331333
332334 return config
333335}
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
693693 }
694694 } else {
695695 // Create category selection map
696 categories := map[string]string{
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 }
696 categories := getOrderedCategories(userCurdConfig)
703697
704698 // Select category using DynamicSelect
705699 var err error
internal/selection_menu.go +22 −3
@@ -428,18 +428,37 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
428428
429429// DynamicSelect displays a simple selection prompt without extra features
430430func DynamicSelect(options map[string]string, addnewoption bool) (SelectionOption, error) {
431
432431 if GetGlobalConfig().RofiSelection {
433432 return RofiSelect(options, addnewoption)
434433 }
435434
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
436444 model := &Model{
437445 options: options,
438 filteredKeys: make([]SelectionOption, 0),
446 filteredKeys: orderedOptions,
439447 addNewOption: addnewoption,
440448 }
441449
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
443462 p := tea.NewProgram(model)
444463
445464 finalModel, err := p.Run()