Commit 0c65d7edab

0c65d7edabd85f041f3197cab2acf703976abcae

parent: 5cdcea89f2

Unsigned

Wraient <wraient.911@gmail.com> · 2025-01-23 20:32 UTC

Add: COLOURS

Layout: unified · split

go.mod +1 −1
@@ -5,13 +5,13 @@ go 1.23.2
55require (
66 github.com/Microsoft/go-winio v0.6.2
77 github.com/charmbracelet/bubbletea v1.1.1
8 github.com/charmbracelet/lipgloss v0.13.0
89 github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
910 github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2
1011)
1112
1213require (
1314 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
14 github.com/charmbracelet/lipgloss v0.13.0 // indirect
1515 github.com/charmbracelet/x/ansi v0.2.3 // indirect
1616 github.com/charmbracelet/x/term v0.2.0 // indirect
1717 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
internal/selection_menu.go +63 −30
@@ -2,17 +2,19 @@ package internal
22
33import (
44 "bytes"
5 "crypto/md5"
56 "fmt"
67 "io"
78 "net/http"
89 "os"
910 "os/exec"
11 "path/filepath"
1012 "sort"
1113 "strings"
1214 "time"
13 "github.com/charmbracelet/bubbletea"
14 "crypto/md5"
15 "path/filepath"
15
16 tea "github.com/charmbracelet/bubbletea"
17 "github.com/charmbracelet/lipgloss"
1618)
1719
1820// SelectionOption holds the label and the internal key
@@ -29,10 +31,40 @@ type Model struct {
2931 selected int
3032 terminalWidth int
3133 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
3436}
3537
38var (
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
3668// Init initializes the model
3769func (m Model) Init() tea.Cmd {
3870 return nil
@@ -108,12 +140,15 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
108140func (m Model) View() string {
109141 var b strings.Builder
110142
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")
114149
115150 if len(m.filteredKeys) == 0 {
116 b.WriteString("\nNo matches found.\n")
151 b.WriteString("\n" + noMatchesStyle.Render("No matches found.") + "\n")
117152 } else {
118153 visibleItems := m.visibleItemsCount()
119154
@@ -127,9 +162,9 @@ func (m Model) View() string {
127162 // Render the options within the visible range
128163 for i := start; i < end; i++ {
129164 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")
131166 } else {
132 b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) // Regular option
167 b.WriteString(regularItemStyle.Render(" "+m.filteredKeys[i].Label) + "\n")
133168 }
134169 }
135170 }
@@ -177,7 +212,6 @@ func (m *Model) filterOptions() {
177212 })
178213}
179214
180
181215func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption bool) (SelectionOption, error) {
182216 // Pre-download first 14 images in background
183217 go preDownloadImages(options, 14)
@@ -196,7 +230,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
196230 Log(fmt.Sprintf("Error caching image: %v", err), logFile)
197231 continue
198232 }
199
233
200234 // Format: "Title\x00icon\x1f/path/to/cached/image\n"
201235 // This tells Rofi to use the image as an icon for this entry
202236 rofiInput.WriteString(fmt.Sprintf("%s\x00icon\x1f%s\n", option.Title, cachePath))
@@ -210,17 +244,17 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
210244
211245 // Get the absolute path to the rasi config
212246 configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanimepreview.rasi")
213
247
214248 // Create the command with explicit arguments
215249 args := []string{
216250 "-dmenu",
217251 "-theme", configPath,
218252 "-show-icons",
219253 "-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
222256 }
223
257
224258 // Create the command
225259 rofiCmd := exec.Command("rofi", args...)
226260
@@ -229,7 +263,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
229263 var stdout, stderr bytes.Buffer
230264 rofiCmd.Stdout = &stdout
231265 rofiCmd.Stderr = &stderr
232
266
233267 // Run the command
234268 err := rofiCmd.Run()
235269 if err != nil {
@@ -238,7 +272,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
238272 Log(fmt.Sprintf("Rofi stdout: %s", stdout.String()), logFile)
239273 return SelectionOption{}, fmt.Errorf("failed to execute rofi: %w", err)
240274 }
241
275
242276 selectedTitle := strings.TrimSpace(stdout.String())
243277
244278 // Handle special cases
@@ -328,7 +362,6 @@ func showCachedImagePreview(imageURL string) error {
328362 return nil
329363}
330364
331
332365func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, error) {
333366 userCurdConfig := GetGlobalConfig()
334367 if userCurdConfig.StoragePath == "" {
@@ -340,37 +373,37 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
340373 for _, value := range options {
341374 optionsList = append(optionsList, value)
342375 }
343
376
344377 // Sort the options alphabetically
345378 sort.Strings(optionsList)
346
379
347380 // Add "Add new anime" and "Quit" options
348381 if addanimeopt {
349382 optionsList = append(optionsList, "Add new anime", "Quit")
350383 } else {
351384 optionsList = append(optionsList, "Quit")
352385 }
353
386
354387 // Join all options into a single string, separated by newlines
355388 optionsString := strings.Join(optionsList, "\n")
356
389
357390 // Prepare the Rofi command
358391 cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanime.rasi"), "-i", "-p", "Select an anime")
359
392
360393 // Set up pipes for input and output
361394 cmd.Stdin = strings.NewReader(optionsString)
362395 var out bytes.Buffer
363396 cmd.Stdout = &out
364
397
365398 // Run the command
366399 err := cmd.Run()
367400 if err != nil {
368401 return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err)
369402 }
370
403
371404 // Get the selected option
372405 selected := strings.TrimSpace(out.String())
373
406
374407 // Handle special cases
375408 switch selected {
376409 case "":
@@ -380,14 +413,14 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
380413 case "Quit":
381414 return SelectionOption{Label: "Quit", Key: "-1"}, nil
382415 }
383
416
384417 // Find the key for the selected value
385418 for key, value := range options {
386419 if value == selected {
387420 return SelectionOption{Label: value, Key: key}, nil
388421 }
389422 }
390
423
391424 // If we get here, the selected option wasn't found in the original map
392425 return SelectionOption{}, fmt.Errorf("selected option not found in original list")
393426}