filter.go
This file
Path
filter/filter.go
563 lines · 14.7 KiB · Go
Last changed by
chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
History of this fileTake it
The walk stopped after 200 commits: lines older than 6837ed2 point at it.
feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 1–15why, one step back1// Package filter provides a fuzzy searching text input to allow filtering a2// list of options to select one option.3//4// By default it will list all the files (recursively) in the current directory5// for the user to choose one, but the script (or user) can provide different6// new-line separated options to choose from.7//8// I.e. let's pick from a list of gum flavors:9//10// $ cat flavors.text | gum filter11package filter1213import (14 "strings"15chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 16–21why, one step back16 "charm.land/bubbles/v2/help"17 "charm.land/bubbles/v2/key"18 "charm.land/bubbles/v2/textinput"19 "charm.land/bubbles/v2/viewport"20 tea "charm.land/bubbletea/v2"21 "charm.land/lipgloss/v2"feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 22why, one step back22 "github.com/charmbracelet/x/exp/ordered"fix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655
line 23why, one step back23 "github.com/rivo/uniseg"feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 24–26why, one step back24 "github.com/sahilm/fuzzy"25)26refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 27–34why, one step back27func defaultKeymap() keymap {28 return keymap{29 Down: key.NewBinding(30 key.WithKeys("down", "ctrl+j", "ctrl+n"),31 ),32 Up: key.NewBinding(33 key.WithKeys("up", "ctrl+k", "ctrl+p"),34 ),feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 35–46why, one step back35 NDown: key.NewBinding(36 key.WithKeys("j"),37 ),38 NUp: key.NewBinding(39 key.WithKeys("k"),40 ),41 Home: key.NewBinding(42 key.WithKeys("g", "home"),43 ),44 End: key.NewBinding(45 key.WithKeys("G", "end"),46 ),refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 47–61why, one step back47 ToggleAndNext: key.NewBinding(48 key.WithKeys("tab"),49 key.WithHelp("tab", "toggle"),50 key.WithDisabled(),51 ),52 ToggleAndPrevious: key.NewBinding(53 key.WithKeys("shift+tab"),54 key.WithHelp("shift+tab", "toggle"),55 key.WithDisabled(),56 ),57 Toggle: key.NewBinding(58 key.WithKeys("ctrl+@"),59 key.WithHelp("ctrl+@", "toggle"),60 key.WithDisabled(),61 ),feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079
lines 62–66why, one step back62 ToggleAll: key.NewBinding(63 key.WithKeys("ctrl+a"),64 key.WithHelp("ctrl+a", "select all"),65 key.WithDisabled(),66 ),feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 67–74why, one step back67 FocusInSearch: key.NewBinding(68 key.WithKeys("/"),69 key.WithHelp("/", "search"),70 ),71 FocusOutSearch: key.NewBinding(72 key.WithKeys("esc"),73 key.WithHelp("esc", "blur search"),74 ),feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
lines 75–78why, one step back75 Quit: key.NewBinding(76 key.WithKeys("esc"),77 key.WithHelp("esc", "quit"),78 ),refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 79why, one step back79 Abort: key.NewBinding(feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 80why, one step back80 key.WithKeys("ctrl+c"),refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 81–90why, one step back81 key.WithHelp("ctrl+c", "abort"),82 ),83 Submit: key.NewBinding(84 key.WithKeys("enter", "ctrl+q"),85 key.WithHelp("enter", "submit"),86 ),87 }88}8990type keymap struct {feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 91–92why, one step back91 FocusInSearch,92 FocusOutSearch,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 93–94why, one step back93 Down,94 Up,feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 95–98why, one step back95 NDown,96 NUp,97 Home,98 End,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 99–100why, one step back99 ToggleAndNext,100 ToggleAndPrevious,feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079
line 101why, one step back101 ToggleAll,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 102–103why, one step back102 Toggle,103 Abort,feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 104why, one step back104 Quit,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 105–115why, one step back105 Submit key.Binding106}107108// FullHelp implements help.KeyMap.109func (k keymap) FullHelp() [][]key.Binding { return nil }110111// ShortHelp implements help.KeyMap.112func (k keymap) ShortHelp() []key.Binding {113 return []key.Binding{114 key.NewBinding(115 key.WithKeys("up", "down"),feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 116why, one step back116 key.WithHelp("↓↑", "navigate"),refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 117why, one step back117 ),feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 118–119why, one step back118 k.FocusInSearch,119 k.FocusOutSearch,feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079
lines 120–121why, one step back120 k.ToggleAndNext,121 k.ToggleAll,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 122–125why, one step back122 k.Submit,123 }124}125feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 126–128why, one step back126type model struct {127 textinput textinput.Model128 viewport *viewport.Modelfix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
lines 129–130why, one step back129 choices map[string]string130 filteringChoices []stringfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 131–140why, one step back131 matches []fuzzy.Match132 cursor int133 header string134 selected map[string]struct{}135 limit int136 numSelected int137 indicator string138 selectedPrefix string139 unselectedPrefix string140 height intfeat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 141why, one step back141 padding []intfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 142–152why, one step back142 quitting bool143 headerStyle lipgloss.Style144 matchStyle lipgloss.Style145 textStyle lipgloss.Style146 cursorTextStyle lipgloss.Style147 indicatorStyle lipgloss.Style148 selectedPrefixStyle lipgloss.Style149 unselectedPrefixStyle lipgloss.Style150 reverse bool151 fuzzy bool152 sort boolrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 153–155why, one step back153 showHelp bool154 keymap keymap155 help help.Modelfix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
line 156why, one step back156 strict boolfeat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 157why, one step back157 submitted boolfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 158–159why, one step back158}159feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 160why, one step back160func (m model) Init() tea.Cmd { return textinput.Blink }fix: lint issues (#663)Carlos Alexandro Beckerbefore adoptionb9611e1
line 161why, one step back161chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 162why, one step back162func (m model) View() tea.View {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 163why, one step back163 if m.quitting {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 164why, one step back164 return tea.NewView("")feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 165–172why, one step back165 }166167 var s strings.Builder168 var lineTextStyle lipgloss.Style169170 // For reverse layout, if the number of matches is less than the viewport171 // height, we need to offset the matches so that the first match is at the172 // bottom edge of the viewport instead of in the middle.chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 173–174why, one step back173 if m.reverse && len(m.matches) < m.viewport.Height() {174 s.WriteString(strings.Repeat("\n", m.viewport.Height()-len(m.matches)))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 175–207why, one step back175 }176177 // Since there are matches, display them so that the user can see, in real178 // time, what they are searching for.179 last := len(m.matches) - 1180 for i := range m.matches {181 // For reverse layout, the matches are displayed in reverse order.182 if m.reverse {183 i = last - i184 }185 match := m.matches[i]186187 // If this is the current selected index, we add a small indicator to188 // represent it. Otherwise, simply pad the string.189 // The line's text style is set depending on whether or not the cursor190 // points to this line.191 if i == m.cursor {192 s.WriteString(m.indicatorStyle.Render(m.indicator))193 lineTextStyle = m.cursorTextStyle194 } else {195 s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator)))196 lineTextStyle = m.textStyle197 }198199 // If there are multiple selections mark them, otherwise leave an empty space200 if _, ok := m.selected[match.Str]; ok {201 s.WriteString(m.selectedPrefixStyle.Render(m.selectedPrefix))202 } else if m.limit > 1 {203 s.WriteString(m.unselectedPrefixStyle.Render(m.unselectedPrefix))204 } else {205 s.WriteString(" ")206 }207fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
lines 208–214why, one step back208 styledOption := m.choices[match.Str]209 if len(match.MatchedIndexes) == 0 {210 // No matches, just render the text.211 s.WriteString(lineTextStyle.Render(styledOption))212 s.WriteRune('\n')213 continue214 }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 215why, one step back215fix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655
line 216why, one step back216 var ranges []lipgloss.Rangefix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 217why, one step back217 for _, rng := range matchedRanges(match.MatchedIndexes) {fix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655
lines 218–222why, one step back218 // ansi.Cut is grapheme and ansi sequence aware, we match against a ansi.Stripped string, but we might still have graphemes.219 // all that to say that rng is byte positions, but we need to pass it down to ansi.Cut as char positions.220 // so we need to adjust it here:221 start, stop := bytePosToVisibleCharPos(match.Str, rng)222 ranges = append(ranges, lipgloss.NewRange(start, stop+1, m.matchStyle))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 223why, one step back223 }fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 224why, one step back224fix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655
line 225why, one step back225 s.WriteString(lineTextStyle.Render(lipgloss.StyleRanges(styledOption, ranges...)))fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 226why, one step back226docs: fix filter comment typo (#1030)Rohan Santhosh Kumarbefore adoptionfc8e9a5
line 227why, one step back227 // We have finished displaying the match with all of its matchedfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 228–236why, one step back228 // characters highlighted and the rest filled in.229 // Move on to the next match.230 s.WriteRune('\n')231 }232233 m.viewport.SetContent(s.String())234235 // View the input and the filtered choices236 header := m.headerStyle.Render(m.header)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 237why, one step back237 var content stringfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 238why, one step back238 if m.reverse {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 239–241why, one step back239 content = m.reverseView(header)240 } else {241 content = m.normalView(header)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 242why, one step back242 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 243–249why, one step back243 v := tea.NewView(content)244 v.ReportFocus = true245 if m.height == 0 {246 v.AltScreen = true247 }248 return v249}feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 250why, one step back250chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 251–265why, one step back251func (m model) reverseView(header string) string {252 view := m.viewport.View()253 if m.header != "" {254 view += "\n" + header255 }256 view += "\n" + m.textinput.View()257 if m.showHelp {258 view += m.helpView()259 }260 return lipgloss.NewStyle().261 Padding(m.padding...).262 Render(view)263}264265func (m model) normalView(header string) string {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 266why, one step back266 view := m.textinput.View() + "\n" + m.viewport.View()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 267why, one step back267 if m.showHelp {feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 268why, one step back268 view += m.helpView()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 269why, one step back269 }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 270why, one step back270 if m.header != "" {feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 271–273why, one step back271 return lipgloss.NewStyle().272 Padding(m.padding...).273 Render(header + "\n" + view)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 274why, one step back274 }feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 275–277why, one step back275 return lipgloss.NewStyle().276 Padding(m.padding...).277 Render(view)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 278–279why, one step back278}279refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 280–283why, one step back280func (m model) helpView() string {281 return "\n\n" + m.help.View(m.keymap)282}283feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 284why, one step back284func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
lines 285–286why, one step back285 var cmd, icmd tea.Cmd286 m.textinput, icmd = m.textinput.Update(msg)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 287–289why, one step back287 switch msg := msg.(type) {288 case tea.WindowSizeMsg:289 if m.height == 0 || m.height > msg.Height {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 290why, one step back290 m.viewport.SetHeight(msg.Height - lipgloss.Height(m.textinput.View()))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 291why, one step back291 }refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 292why, one step back292 // Include the header in the height calculation.feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 293why, one step back293 if m.header != "" {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 294why, one step back294 m.viewport.SetHeight(m.viewport.Height() - lipgloss.Height(m.headerStyle.Render(m.header)))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 295why, one step back295 }refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 296–297why, one step back296 // Include the help in the total height calculation.297 if m.showHelp {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 298why, one step back298 m.viewport.SetHeight(m.viewport.Height() - lipgloss.Height(m.helpView()))refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 299why, one step back299 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 300–302why, one step back300 m.viewport.SetHeight(m.viewport.Height() - m.padding[0] - m.padding[2])301 m.viewport.SetWidth(msg.Width - m.padding[1] - m.padding[3])302 m.textinput.SetWidth(msg.Width - m.padding[1] - m.padding[3])feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 303why, one step back303 if m.reverse {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 304why, one step back304 m.viewport.SetYOffset(ordered.Clamp(len(m.matches)-m.viewport.Height(), 0, len(m.matches)))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 305why, one step back305 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 306why, one step back306 case tea.KeyPressMsg:refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 307–308why, one step back307 km := m.keymap308 switch {feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 309–312why, one step back309 case key.Matches(msg, km.FocusInSearch):310 m.textinput.Focus()311 case key.Matches(msg, km.FocusOutSearch):312 m.textinput.Blur()feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
lines 313–315why, one step back313 case key.Matches(msg, km.Quit):314 m.quitting = true315 return m, tea.Quitrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 316why, one step back316 case key.Matches(msg, km.Abort):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 317why, one step back317 m.quitting = truefeat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 318why, one step back318 return m, tea.Interruptrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 319why, one step back319 case key.Matches(msg, km.Submit):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 320why, one step back320 m.quitting = truefeat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 321why, one step back321 m.submitted = truefeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 322why, one step back322 return m, tea.Quitfeat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
line 323why, one step back323 case key.Matches(msg, km.Down, km.NDown):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 324why, one step back324 m.CursorDown()feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
line 325why, one step back325 case key.Matches(msg, km.Up, km.NUp):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 326why, one step back326 m.CursorUp()feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 327–332why, one step back327 case key.Matches(msg, km.Home):328 m.cursor = 0329 m.viewport.GotoTop()330 case key.Matches(msg, km.End):331 m.cursor = len(m.choices) - 1332 m.viewport.GotoBottom()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 333why, one step back333 case key.Matches(msg, km.ToggleAndNext):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 334–338why, one step back334 if m.limit == 1 {335 break // no op336 }337 m.ToggleSelection()338 m.CursorDown()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 339why, one step back339 case key.Matches(msg, km.ToggleAndPrevious):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 340–344why, one step back340 if m.limit == 1 {341 break // no op342 }343 m.ToggleSelection()344 m.CursorUp()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 345why, one step back345 case key.Matches(msg, km.Toggle):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 346–349why, one step back346 if m.limit == 1 {347 break // no op348 }349 m.ToggleSelection()feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079
lines 350–358why, one step back350 case key.Matches(msg, km.ToggleAll):351 if m.limit <= 1 {352 break353 }354 if m.numSelected < len(m.matches) && m.numSelected < m.limit {355 m = m.selectAll()356 } else {357 m = m.deselectAll()358 }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 359–365why, one step back359 default:360 // yOffsetFromBottom is the number of lines from the bottom of the361 // list to the top of the viewport. This is used to keep the viewport362 // at a constant position when the number of matches are reduced363 // in the reverse layout.364 var yOffsetFromBottom int365 if m.reverse {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 366why, one step back366 yOffsetFromBottom = max(0, len(m.matches)-m.viewport.YOffset())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 367–370why, one step back367 }368369 // A character was entered, this likely means that the text input has370 // changed. This suggests that the matches are outdated, so update them.fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
lines 371–374why, one step back371 var choices []string372 if !m.strict {373 choices = append(choices, m.textinput.Value())374 }fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 375why, one step back375 choices = append(choices, m.filteringChoices...)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 376–377why, one step back376 if m.fuzzy {377 if m.sort {fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
line 378why, one step back378 m.matches = fuzzy.Find(m.textinput.Value(), choices)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 379why, one step back379 } else {fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
line 380why, one step back380 m.matches = fuzzy.FindNoSort(m.textinput.Value(), choices)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 381–382why, one step back381 }382 } else {fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
line 383why, one step back383 m.matches = exactMatches(m.textinput.Value(), choices)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 384–388why, one step back384 }385386 // If the search field is empty, let's not display the matches387 // (none), but rather display all possible choices.388 if m.textinput.Value() == "" {fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 389why, one step back389 m.matches = matchAll(m.filteringChoices)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 390–394why, one step back390 }391392 // For reverse layout, we need to offset the viewport so that the393 // it remains at a constant position relative to the cursor.394 if m.reverse {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 395–396why, one step back395 maxYOffset := max(0, len(m.matches)-m.viewport.Height())396 m.viewport.SetYOffset(ordered.Clamp(len(m.matches)-yOffsetFromBottom, 0, maxYOffset))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 397–399why, one step back397 }398 }399 }feat(filter): allow to focus out of filter (#776)Carlos Alexandro Beckerbefore adoptionf230a3d
lines 400–406why, one step back400401 m.keymap.FocusInSearch.SetEnabled(!m.textinput.Focused())402 m.keymap.FocusOutSearch.SetEnabled(m.textinput.Focused())403 m.keymap.NUp.SetEnabled(!m.textinput.Focused())404 m.keymap.NDown.SetEnabled(!m.textinput.Focused())405 m.keymap.Home.SetEnabled(!m.textinput.Focused())406 m.keymap.End.SetEnabled(!m.textinput.Focused())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 407–409why, one step back407408 // It's possible that filtering items have caused fewer matches. So, ensure409 // that the selected index is within the bounds of the number of matches.feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 410why, one step back410 m.cursor = ordered.Clamp(m.cursor, 0, len(m.matches)-1)feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 411why, one step back411 return m, tea.Batch(cmd, icmd)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 412–414why, one step back412}413414func (m *model) CursorUp() {fix(filter): panic if no matchesCarlos Alexandro Beckerbefore adoption3cec9b7
lines 415–417why, one step back415 if len(m.matches) == 0 {416 return417 }fix: lint issues (#663)Carlos Alexandro Beckerbefore adoptionb9611e1
line 418why, one step back418 if m.reverse { //nolint:nestiffeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 419why, one step back419 m.cursor = (m.cursor + 1) % len(m.matches)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 420why, one step back420 if len(m.matches)-m.cursor <= m.viewport.YOffset() {fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd
line 421why, one step back421 m.viewport.ScrollUp(1)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 422why, one step back422 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 423–424why, one step back423 if len(m.matches)-m.cursor > m.viewport.Height()+m.viewport.YOffset() {424 m.viewport.SetYOffset(len(m.matches) - m.viewport.Height())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 425–427why, one step back425 }426 } else {427 m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 428why, one step back428 if m.cursor < m.viewport.YOffset() {fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd
line 429why, one step back429 m.viewport.ScrollUp(1)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 430why, one step back430 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 431–432why, one step back431 if m.cursor >= m.viewport.YOffset()+m.viewport.Height() {432 m.viewport.SetYOffset(len(m.matches) - m.viewport.Height())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 433–437why, one step back433 }434 }435}436437func (m *model) CursorDown() {fix(filter): panic if no matchesCarlos Alexandro Beckerbefore adoption3cec9b7
lines 438–440why, one step back438 if len(m.matches) == 0 {439 return440 }fix: lint issues (#663)Carlos Alexandro Beckerbefore adoptionb9611e1
line 441why, one step back441 if m.reverse { //nolint:nestiffeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 442why, one step back442 m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 443why, one step back443 if len(m.matches)-m.cursor > m.viewport.Height()+m.viewport.YOffset() {fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd
line 444why, one step back444 m.viewport.ScrollDown(1)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 445why, one step back445 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 446why, one step back446 if len(m.matches)-m.cursor <= m.viewport.YOffset() {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 447–450why, one step back447 m.viewport.GotoTop()448 }449 } else {450 m.cursor = (m.cursor + 1) % len(m.matches)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 451why, one step back451 if m.cursor >= m.viewport.YOffset()+m.viewport.Height() {fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd
line 452why, one step back452 m.viewport.ScrollDown(1)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 453why, one step back453 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 454why, one step back454 if m.cursor < m.viewport.YOffset() {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 455–467why, one step back455 m.viewport.GotoTop()456 }457 }458}459460func (m *model) ToggleSelection() {461 if _, ok := m.selected[m.matches[m.cursor].Str]; ok {462 delete(m.selected, m.matches[m.cursor].Str)463 m.numSelected--464 } else if m.numSelected < m.limit {465 m.selected[m.matches[m.cursor].Str] = struct{}{}466 m.numSelected++467 }feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079
lines 468–487why, one step back468}469470func (m model) selectAll() model {471 for i := range m.matches {472 if m.numSelected >= m.limit {473 break // do not exceed given limit474 }475 if _, ok := m.selected[m.matches[i].Str]; ok {476 continue477 }478 m.selected[m.matches[i].Str] = struct{}{}479 m.numSelected++480 }481 return m482}483484func (m model) deselectAll() model {485 m.selected = make(map[string]struct{})486 m.numSelected = 0487 return mfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 488–505why, one step back488}489490func matchAll(options []string) []fuzzy.Match {491 matches := make([]fuzzy.Match, len(options))492 for i, option := range options {493 matches[i] = fuzzy.Match{Str: option}494 }495 return matches496}497498func exactMatches(search string, choices []string) []fuzzy.Match {499 matches := fuzzy.Matches{}500 for i, choice := range choices {501 search = strings.ToLower(search)502 matchedString := strings.ToLower(choice)503504 index := strings.Index(matchedString, search)505 if index >= 0 {ci: fix lintAndrey Neringbefore adoptionb0471e8
line 506why, one step back506 matchedIndexes := make([]int, 0, len(search))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 507–518why, one step back507 for s := range search {508 matchedIndexes = append(matchedIndexes, index+s)509 }510 matches = append(matches, fuzzy.Match{511 Str: choice,512 Index: i,513 MatchedIndexes: matchedIndexes,514 })515 }516 }517518 return matchesfix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
lines 519–539why, one step back519}520521func matchedRanges(in []int) [][2]int {522 if len(in) == 0 {523 return [][2]int{}524 }525 current := [2]int{in[0], in[0]}526 if len(in) == 1 {527 return [][2]int{current}528 }529 var out [][2]int530 for i := 1; i < len(in); i++ {531 if in[i] == current[1]+1 {532 current[1] = in[i]533 } else {534 out = append(out, current)535 current = [2]int{in[i], in[i]}536 }537 }538 out = append(out, current)539 return outfix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655
lines 540–562why, one step back540}541542func bytePosToVisibleCharPos(str string, rng [2]int) (int, int) {543 bytePos, byteStart, byteStop := 0, rng[0], rng[1]544 pos, start, stop := 0, 0, 0545 gr := uniseg.NewGraphemes(str)546 for byteStart > bytePos {547 if !gr.Next() {548 break549 }550 bytePos += len(gr.Str())551 pos += max(1, gr.Width())552 }553 start = pos554 for byteStop > bytePos {555 if !gr.Next() {556 break557 }558 bytePos += len(gr.Str())559 pos += max(1, gr.Width())560 }561 stop = pos562 return start, stopfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 563why, one step back563}