Commit 0c65d7edab
Unsigned
Layout: unified · split
go.mod +1 −1
| @@ -5,13 +5,13 @@ go 1.23.2 | |||
| 5 | require ( | 5 | require ( |
| 6 | github.com/Microsoft/go-winio v0.6.2 | 6 | github.com/Microsoft/go-winio v0.6.2 |
| 7 | github.com/charmbracelet/bubbletea v1.1.1 | 7 | github.com/charmbracelet/bubbletea v1.1.1 |
| 8 | github.com/charmbracelet/lipgloss v0.13.0 | ||
| 8 | github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 | 9 | github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 |
| 9 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 | 10 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 |
| 10 | ) | 11 | ) |
| 11 | 12 | ||
| 12 | require ( | 13 | require ( |
| 13 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | 14 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect |
| 14 | github.com/charmbracelet/lipgloss v0.13.0 // indirect | ||
| 15 | github.com/charmbracelet/x/ansi v0.2.3 // indirect | 15 | github.com/charmbracelet/x/ansi v0.2.3 // indirect |
| 16 | github.com/charmbracelet/x/term v0.2.0 // indirect | 16 | github.com/charmbracelet/x/term v0.2.0 // indirect |
| 17 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | 17 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect |
internal/selection_menu.go +63 −30
| @@ -2,17 +2,19 @@ package internal | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "crypto/md5" | ||
| 5 | "fmt" | 6 | "fmt" |
| 6 | "io" | 7 | "io" |
| 7 | "net/http" | 8 | "net/http" |
| 8 | "os" | 9 | "os" |
| 9 | "os/exec" | 10 | "os/exec" |
| 11 | "path/filepath" | ||
| 10 | "sort" | 12 | "sort" |
| 11 | "strings" | 13 | "strings" |
| 12 | "time" | 14 | "time" |
| 13 | "github.com/charmbracelet/bubbletea" | 15 | |
| 14 | "crypto/md5" | 16 | tea "github.com/charmbracelet/bubbletea" |
| 15 | "path/filepath" | 17 | "github.com/charmbracelet/lipgloss" |
| 16 | ) | 18 | ) |
| 17 | 19 | ||
| 18 | // SelectionOption holds the label and the internal key | 20 | // SelectionOption holds the label and the internal key |
| @@ -29,10 +31,40 @@ type Model struct { | |||
| 29 | selected int | 31 | selected int |
| 30 | terminalWidth int | 32 | terminalWidth int |
| 31 | terminalHeight int | 33 | terminalHeight int |
| 32 | scrollOffset int // Track the topmost visible item | 34 | scrollOffset int // Track the topmost visible item |
| 33 | addNewOption bool // Add this field | 35 | addNewOption bool // Add this field |
| 34 | } | 36 | } |
| 35 | 37 | ||
| 38 | var ( | ||
| 39 | // Style definitions | ||
| 40 | titleStyle = lipgloss.NewStyle(). | ||
| 41 | Foreground(lipgloss.Color("#7CB9E8")). // Light blue | ||
| 42 | Bold(true) | ||
| 43 | |||
| 44 | filterLabelStyle = lipgloss.NewStyle(). | ||
| 45 | Foreground(lipgloss.Color("#FF69B4")). // Hot pink | ||
| 46 | Bold(true) | ||
| 47 | |||
| 48 | filterTextStyle = lipgloss.NewStyle(). | ||
| 49 | Foreground(lipgloss.Color("#98FB98")) // Pale green | ||
| 50 | |||
| 51 | selectedItemStyle = lipgloss.NewStyle(). | ||
| 52 | Foreground(lipgloss.Color("#000000")). // Black text | ||
| 53 | Background(lipgloss.Color("#7CB9E8")). // Light blue background | ||
| 54 | Bold(true). | ||
| 55 | Padding(0, 1) | ||
| 56 | |||
| 57 | regularItemStyle = lipgloss.NewStyle(). | ||
| 58 | Foreground(lipgloss.Color("#E6E6FA")) // Light lavender | ||
| 59 | |||
| 60 | noMatchesStyle = lipgloss.NewStyle(). | ||
| 61 | Foreground(lipgloss.Color("#FF6B6B")). // Coral red | ||
| 62 | Italic(true) | ||
| 63 | |||
| 64 | quitHintStyle = lipgloss.NewStyle(). | ||
| 65 | Foreground(lipgloss.Color("#FFD700")) // Gold | ||
| 66 | ) | ||
| 67 | |||
| 36 | // Init initializes the model | 68 | // Init initializes the model |
| 37 | func (m Model) Init() tea.Cmd { | 69 | func (m Model) Init() tea.Cmd { |
| 38 | return nil | 70 | return nil |
| @@ -108,12 +140,15 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | |||
| 108 | func (m Model) View() string { | 140 | func (m Model) View() string { |
| 109 | var b strings.Builder | 141 | var b strings.Builder |
| 110 | 142 | ||
| 111 | // Display the search prompt and filter | 143 | // Display the search prompt and filter with colors |
| 112 | b.WriteString("Search (Press Ctrl+C to quit):\n") | 144 | b.WriteString(titleStyle.Render("Search") + " (Press " + |
| 113 | b.WriteString("Filter: " + m.filter + "\n") | 145 | quitHintStyle.Render("Ctrl+C") + " to quit):\n") |
| 146 | |||
| 147 | b.WriteString(filterLabelStyle.Render("Filter: ") + | ||
| 148 | filterTextStyle.Render(m.filter) + "\n") | ||
| 114 | 149 | ||
| 115 | if len(m.filteredKeys) == 0 { | 150 | if len(m.filteredKeys) == 0 { |
| 116 | b.WriteString("\nNo matches found.\n") | 151 | b.WriteString("\n" + noMatchesStyle.Render("No matches found.") + "\n") |
| 117 | } else { | 152 | } else { |
| 118 | visibleItems := m.visibleItemsCount() | 153 | visibleItems := m.visibleItemsCount() |
| 119 | 154 | ||
| @@ -127,9 +162,9 @@ func (m Model) View() string { | |||
| 127 | // Render the options within the visible range | 162 | // Render the options within the visible range |
| 128 | for i := start; i < end; i++ { | 163 | for i := start; i < end; i++ { |
| 129 | if i == m.selected { | 164 | if i == m.selected { |
| 130 | b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label)) // Highlight the selected option | 165 | b.WriteString(selectedItemStyle.Render("▶ "+m.filteredKeys[i].Label) + "\n") |
| 131 | } else { | 166 | } else { |
| 132 | b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) // Regular option | 167 | b.WriteString(regularItemStyle.Render(" "+m.filteredKeys[i].Label) + "\n") |
| 133 | } | 168 | } |
| 134 | } | 169 | } |
| 135 | } | 170 | } |
| @@ -177,7 +212,6 @@ func (m *Model) filterOptions() { | |||
| 177 | }) | 212 | }) |
| 178 | } | 213 | } |
| 179 | 214 | ||
| 180 | |||
| 181 | func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption bool) (SelectionOption, error) { | 215 | func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption bool) (SelectionOption, error) { |
| 182 | // Pre-download first 14 images in background | 216 | // Pre-download first 14 images in background |
| 183 | go preDownloadImages(options, 14) | 217 | go preDownloadImages(options, 14) |
| @@ -196,7 +230,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo | |||
| 196 | Log(fmt.Sprintf("Error caching image: %v", err), logFile) | 230 | Log(fmt.Sprintf("Error caching image: %v", err), logFile) |
| 197 | continue | 231 | continue |
| 198 | } | 232 | } |
| 199 | 233 | ||
| 200 | // Format: "Title\x00icon\x1f/path/to/cached/image\n" | 234 | // Format: "Title\x00icon\x1f/path/to/cached/image\n" |
| 201 | // This tells Rofi to use the image as an icon for this entry | 235 | // This tells Rofi to use the image as an icon for this entry |
| 202 | rofiInput.WriteString(fmt.Sprintf("%s\x00icon\x1f%s\n", option.Title, cachePath)) | 236 | rofiInput.WriteString(fmt.Sprintf("%s\x00icon\x1f%s\n", option.Title, cachePath)) |
| @@ -210,17 +244,17 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo | |||
| 210 | 244 | ||
| 211 | // Get the absolute path to the rasi config | 245 | // Get the absolute path to the rasi config |
| 212 | configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanimepreview.rasi") | 246 | configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanimepreview.rasi") |
| 213 | 247 | ||
| 214 | // Create the command with explicit arguments | 248 | // Create the command with explicit arguments |
| 215 | args := []string{ | 249 | args := []string{ |
| 216 | "-dmenu", | 250 | "-dmenu", |
| 217 | "-theme", configPath, | 251 | "-theme", configPath, |
| 218 | "-show-icons", | 252 | "-show-icons", |
| 219 | "-p", "Select Anime", | 253 | "-p", "Select Anime", |
| 220 | "-i", // Case-insensitive matching | 254 | "-i", // Case-insensitive matching |
| 221 | "-no-custom", // Disable custom input | 255 | "-no-custom", // Disable custom input |
| 222 | } | 256 | } |
| 223 | 257 | ||
| 224 | // Create the command | 258 | // Create the command |
| 225 | rofiCmd := exec.Command("rofi", args...) | 259 | rofiCmd := exec.Command("rofi", args...) |
| 226 | 260 | ||
| @@ -229,7 +263,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo | |||
| 229 | var stdout, stderr bytes.Buffer | 263 | var stdout, stderr bytes.Buffer |
| 230 | rofiCmd.Stdout = &stdout | 264 | rofiCmd.Stdout = &stdout |
| 231 | rofiCmd.Stderr = &stderr | 265 | rofiCmd.Stderr = &stderr |
| 232 | 266 | ||
| 233 | // Run the command | 267 | // Run the command |
| 234 | err := rofiCmd.Run() | 268 | err := rofiCmd.Run() |
| 235 | if err != nil { | 269 | if err != nil { |
| @@ -238,7 +272,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo | |||
| 238 | Log(fmt.Sprintf("Rofi stdout: %s", stdout.String()), logFile) | 272 | Log(fmt.Sprintf("Rofi stdout: %s", stdout.String()), logFile) |
| 239 | return SelectionOption{}, fmt.Errorf("failed to execute rofi: %w", err) | 273 | return SelectionOption{}, fmt.Errorf("failed to execute rofi: %w", err) |
| 240 | } | 274 | } |
| 241 | 275 | ||
| 242 | selectedTitle := strings.TrimSpace(stdout.String()) | 276 | selectedTitle := strings.TrimSpace(stdout.String()) |
| 243 | 277 | ||
| 244 | // Handle special cases | 278 | // Handle special cases |
| @@ -328,7 +362,6 @@ func showCachedImagePreview(imageURL string) error { | |||
| 328 | return nil | 362 | return nil |
| 329 | } | 363 | } |
| 330 | 364 | ||
| 331 | |||
| 332 | func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, error) { | 365 | func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, error) { |
| 333 | userCurdConfig := GetGlobalConfig() | 366 | userCurdConfig := GetGlobalConfig() |
| 334 | if userCurdConfig.StoragePath == "" { | 367 | if userCurdConfig.StoragePath == "" { |
| @@ -340,37 +373,37 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e | |||
| 340 | for _, value := range options { | 373 | for _, value := range options { |
| 341 | optionsList = append(optionsList, value) | 374 | optionsList = append(optionsList, value) |
| 342 | } | 375 | } |
| 343 | 376 | ||
| 344 | // Sort the options alphabetically | 377 | // Sort the options alphabetically |
| 345 | sort.Strings(optionsList) | 378 | sort.Strings(optionsList) |
| 346 | 379 | ||
| 347 | // Add "Add new anime" and "Quit" options | 380 | // Add "Add new anime" and "Quit" options |
| 348 | if addanimeopt { | 381 | if addanimeopt { |
| 349 | optionsList = append(optionsList, "Add new anime", "Quit") | 382 | optionsList = append(optionsList, "Add new anime", "Quit") |
| 350 | } else { | 383 | } else { |
| 351 | optionsList = append(optionsList, "Quit") | 384 | optionsList = append(optionsList, "Quit") |
| 352 | } | 385 | } |
| 353 | 386 | ||
| 354 | // Join all options into a single string, separated by newlines | 387 | // Join all options into a single string, separated by newlines |
| 355 | optionsString := strings.Join(optionsList, "\n") | 388 | optionsString := strings.Join(optionsList, "\n") |
| 356 | 389 | ||
| 357 | // Prepare the Rofi command | 390 | // Prepare the Rofi command |
| 358 | cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanime.rasi"), "-i", "-p", "Select an anime") | 391 | cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanime.rasi"), "-i", "-p", "Select an anime") |
| 359 | 392 | ||
| 360 | // Set up pipes for input and output | 393 | // Set up pipes for input and output |
| 361 | cmd.Stdin = strings.NewReader(optionsString) | 394 | cmd.Stdin = strings.NewReader(optionsString) |
| 362 | var out bytes.Buffer | 395 | var out bytes.Buffer |
| 363 | cmd.Stdout = &out | 396 | cmd.Stdout = &out |
| 364 | 397 | ||
| 365 | // Run the command | 398 | // Run the command |
| 366 | err := cmd.Run() | 399 | err := cmd.Run() |
| 367 | if err != nil { | 400 | if err != nil { |
| 368 | return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err) | 401 | return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err) |
| 369 | } | 402 | } |
| 370 | 403 | ||
| 371 | // Get the selected option | 404 | // Get the selected option |
| 372 | selected := strings.TrimSpace(out.String()) | 405 | selected := strings.TrimSpace(out.String()) |
| 373 | 406 | ||
| 374 | // Handle special cases | 407 | // Handle special cases |
| 375 | switch selected { | 408 | switch selected { |
| 376 | case "": | 409 | case "": |
| @@ -380,14 +413,14 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e | |||
| 380 | case "Quit": | 413 | case "Quit": |
| 381 | return SelectionOption{Label: "Quit", Key: "-1"}, nil | 414 | return SelectionOption{Label: "Quit", Key: "-1"}, nil |
| 382 | } | 415 | } |
| 383 | 416 | ||
| 384 | // Find the key for the selected value | 417 | // Find the key for the selected value |
| 385 | for key, value := range options { | 418 | for key, value := range options { |
| 386 | if value == selected { | 419 | if value == selected { |
| 387 | return SelectionOption{Label: value, Key: key}, nil | 420 | return SelectionOption{Label: value, Key: key}, nil |
| 388 | } | 421 | } |
| 389 | } | 422 | } |
| 390 | 423 | ||
| 391 | // If we get here, the selected option wasn't found in the original map | 424 | // If we get here, the selected option wasn't found in the original map |
| 392 | return SelectionOption{}, fmt.Errorf("selected option not found in original list") | 425 | return SelectionOption{}, fmt.Errorf("selected option not found in original list") |
| 393 | } | 426 | } |