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