Enhance search with match highlighting, count display, Ctrl+U to clear

This commit is contained in:
Calmcacil
2026-01-12 23:43:24 +01:00
parent 6629598574
commit 99b9dc17db

View File

@@ -1,8 +1,9 @@
package components package components
import ( import (
"fmt"
"github.com/calmcacil/wg-admin/internal/tui/theme" "github.com/calmcacil/wg-admin/internal/tui/theme"
"strconv"
"strings" "strings"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
@@ -35,20 +36,20 @@ type SearchModel struct {
// Styles (using theme package) // Styles (using theme package)
var ( var (
searchBarStyle = lipgloss.NewStyle(). searchBarStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("255")). Foreground(lipgloss.Color("255")).
Background(theme.StyleBackground). Background(theme.StyleBackground).
Padding(0, 1). Padding(0, 1).
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
BorderForeground(theme.StyleBorder) BorderForeground(theme.StyleBorder)
searchPromptStyle = lipgloss.NewStyle(). searchPromptStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("226")). Foreground(lipgloss.Color("226")).
Bold(true) Bold(true)
searchFilterStyle = lipgloss.NewStyle(). searchFilterStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("147")) Foreground(lipgloss.Color("147"))
searchCountStyle = lipgloss.NewStyle(). searchCountStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("241")) Foreground(lipgloss.Color("241"))
searchHelpStyle = lipgloss.NewStyle(). searchHelpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("243")) Foreground(lipgloss.Color("243"))
) )
// NewSearch creates a new search component // NewSearch creates a new search component
@@ -91,6 +92,10 @@ func (m *SearchModel) Update(msg tea.Msg) (*SearchModel, tea.Cmd) {
m.input.Reset() m.input.Reset()
m.matchCount = m.totalCount m.matchCount = m.totalCount
return m, nil return m, nil
case "ctrl+u":
m.input.Reset()
m.matchCount = m.totalCount
return m, nil
case "tab": case "tab":
m.cycleFilterType() m.cycleFilterType()
return m, nil return m, nil
@@ -144,7 +149,7 @@ func (m *SearchModel) View() string {
helpText := "" helpText := ""
if m.active { if m.active {
helpText = searchHelpStyle.Render(" | Tab: filter | Esc: clear") helpText = searchHelpStyle.Render(" | Tab: filter | Ctrl+U: clear | Esc: exit")
} else { } else {
helpText = searchHelpStyle.Render(" | /: search") helpText = searchHelpStyle.Render(" | /: search")
} }
@@ -273,7 +278,7 @@ func (m *SearchModel) HighlightMatches(value string) string {
return lipgloss.JoinHorizontal( return lipgloss.JoinHorizontal(
lipgloss.Left, lipgloss.Left,
before, before,
matchStyle.Render(string(match)), matchStyle.Render(match),
after, after,
) )
} }
@@ -299,7 +304,7 @@ func (m *SearchModel) renderCount(count int) string {
Foreground(lipgloss.Color("196")). Foreground(lipgloss.Color("196")).
Render("No matches") Render("No matches")
} }
return searchCountStyle.Render(string(rune('0' + count))) return searchCountStyle.Render(fmt.Sprintf("%d", count))
} }
// ClientData represents client data for filtering // ClientData represents client data for filtering