Skip to content
Myr

command.go

This file

Path

filter/command.go

174 lines · 4.6 KiB · Go

Last changed by

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

History of this file

Take 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–6why, one step back
1package filter
23import (4 "errors"5 "fmt"6 "os"

fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7

line 7why, one step back
7 "slices"

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 8–9why, one step back
8 "strings"
9

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 10–13why, one step back
10 "charm.land/bubbles/v2/help"
11 "charm.land/bubbles/v2/textinput"12 "charm.land/bubbles/v2/viewport"13 tea "charm.land/bubbletea/v2"

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

lines 14–18why, one step back
14 "charm.land/gum/v2/internal/files"
15 "charm.land/gum/v2/internal/stdin"16 "charm.land/gum/v2/internal/timeout"17 "charm.land/gum/v2/internal/tty"18 "charm.land/gum/v2/style"

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

line 19why, one step back
19 "github.com/charmbracelet/x/ansi"

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 20–29why, one step back
20 "github.com/sahilm/fuzzy"
21)2223// Run provides a shell script interface for filtering through options, powered24// by the textinput bubble.25func (o Options) Run() error {26 i := textinput.New()27 i.Focus()2829 i.Prompt = o.Prompt

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 30–35why, one step back
30 styles := i.Styles()
31 styles.Focused.Prompt = o.PromptStyle.ToLipgloss()32 styles.Blurred.Prompt = o.PromptStyle.ToLipgloss()33 styles.Focused.Placeholder = o.PlaceholderStyle.ToLipgloss()34 styles.Blurred.Placeholder = o.PlaceholderStyle.ToLipgloss()35 i.SetStyles(styles)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 36why, one step back
36 i.Placeholder = o.Placeholder

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 37why, one step back
37 i.SetWidth(o.Width)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 38why, one step back
38

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 39why, one step back
39 v := viewport.New(viewport.WithWidth(o.Width), viewport.WithHeight(o.Height))

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 40–41why, one step back
40
41 if len(o.Options) == 0 {

feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f

line 42why, one step back
42 if input, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); input != "" {

feat(choose,filter): --input-delimiter --output-delimiter (#779)Carlos Alexandro Beckerbefore adoption6d405c4

line 43why, one step back
43 o.Options = strings.Split(input, o.InputDelimiter)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 44–52why, one step back
44 } else {
45 o.Options = files.List()46 }47 }4849 if len(o.Options) == 0 {50 return errors.New("no options provided, see `gum filter --help`")51 }52

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

lines 53–58why, one step back
53 ctx, cancel := timeout.Context(o.Timeout)
54 defer cancel()5556 options := []tea.ProgramOption{57 tea.WithOutput(os.Stderr),58 tea.WithContext(ctx),

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 59–64why, one step back
59 }
6061 var matches []fuzzy.Match62 if o.Value != "" {63 i.SetValue(o.Value)64 }

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

lines 65–72why, one step back
65
66 choices := map[string]string{}67 filteringChoices := []string{}68 for _, opt := range o.Options {69 s := ansi.Strip(opt)70 choices[s] = opt71 filteringChoices = append(filteringChoices, s)72 }

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 73–74why, one step back
73 switch {
74 case o.Value != "" && o.Fuzzy:

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

line 75why, one step back
75 matches = fuzzy.Find(o.Value, filteringChoices)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 76why, one step back
76 case o.Value != "" && !o.Fuzzy:

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

line 77why, one step back
77 matches = exactMatches(o.Value, filteringChoices)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 78why, one step back
78 default:

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

line 79why, one step back
79 matches = matchAll(filteringChoices)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 80–84why, one step back
80 }
8182 if o.NoLimit {83 o.Limit = len(o.Options)84 }

feat(filter): --select-if-one returns if single match (#778)Carlos Alexandro Beckerbefore adoption0e501ea

lines 85–91why, one step back
85
86 if o.SelectIfOne && len(matches) == 1 {87 tty.Println(matches[0].Str)88 return nil89 }9091 km := defaultKeymap()

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 92–95why, one step back
92 if o.NoLimit || o.Limit > 1 {
93 km.Toggle.SetEnabled(true)94 km.ToggleAndPrevious.SetEnabled(true)95 km.ToggleAndNext.SetEnabled(true)

feat(filter): ctrl+a to toggle select all (#770)Carlos Alexandro Beckerbefore adoption74c1079

line 96why, one step back
96 km.ToggleAll.SetEnabled(true)

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

line 97why, one step back
97 }

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 98why, one step back
98 top, right, bottom, left := style.ParsePadding(o.Padding)

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

line 99why, one step back
99 m := model{

fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef

lines 100–101why, one step back
100 choices: choices,
101 filteringChoices: filteringChoices,

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 102–116why, one step back
102 indicator: o.Indicator,
103 matches: matches,104 header: o.Header,105 textinput: i,106 viewport: &v,107 indicatorStyle: o.IndicatorStyle.ToLipgloss(),108 selectedPrefixStyle: o.SelectedPrefixStyle.ToLipgloss(),109 selectedPrefix: o.SelectedPrefix,110 unselectedPrefixStyle: o.UnselectedPrefixStyle.ToLipgloss(),111 unselectedPrefix: o.UnselectedPrefix,112 matchStyle: o.MatchStyle.ToLipgloss(),113 headerStyle: o.HeaderStyle.ToLipgloss(),114 textStyle: o.TextStyle.ToLipgloss(),115 cursorTextStyle: o.CursorTextStyle.ToLipgloss(),116 height: o.Height,

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 117why, one step back
117 padding: []int{top, right, bottom, left},

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 118–121why, one step back
118 selected: make(map[string]struct{}),
119 limit: o.Limit,120 reverse: o.Reverse,121 fuzzy: o.Fuzzy,

fix: clarify filter --sort flag (#738)Carlos Alexandro Beckerbefore adoptionc422e76

line 122why, one step back
122 sort: o.Sort && o.FuzzySort,

fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8

line 123why, one step back
123 strict: o.Strict,

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 124–126why, one step back
124 showHelp: o.ShowHelp,
125 keymap: km,126 help: help.New(),

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

line 127why, one step back
127 }

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 128why, one step back
128

fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7

lines 129–130why, one step back
129 isSelectAll := len(o.Selected) == 1 && o.Selected[0] == "*"
130 currentSelected := 0

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

line 131why, one step back
131 if len(o.Selected) > 0 {

fix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45

lines 132–133why, one step back
132 for i, option := range matches {
133 if currentSelected >= o.Limit || (!isSelectAll && !slices.Contains(o.Selected, option.Str)) {

fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7

lines 134–136why, one step back
134 continue
135 }136 if o.Limit == 1 {

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

line 137why, one step back
137 m.cursor = i

fix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45

line 138why, one step back
138 m.selected[option.Str] = struct{}{}

fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7

lines 139–140why, one step back
139 } else {
140 currentSelected++

fix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45

line 141why, one step back
141 m.selected[option.Str] = struct{}{}

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

lines 142–146why, one step back
142 }
143 }144 }145146 tm, err := tea.NewProgram(m, options...).Run()

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 147–150why, one step back
147 if err != nil {
148 return fmt.Errorf("unable to run filter: %w", err)149 }150

feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b

line 151why, one step back
151 m = tm.(model)

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

lines 152–154why, one step back
152 if !m.submitted {
153 return errors.New("nothing selected")154 }

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 155–159why, one step back
155
156 // allSelections contains values only if limit is greater157 // than 1 or if flag --no-limit is passed, hence there is158 // no need to further checks159 if len(m.selected) > 0 {

feat(filter): --select-if-one returns if single match (#778)Carlos Alexandro Beckerbefore adoption0e501ea

line 160why, one step back
160 o.checkSelected(m)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 161why, one step back
161 } else if len(m.matches) > m.cursor && m.cursor >= 0 {

feat(filter): --select-if-one returns if single match (#778)Carlos Alexandro Beckerbefore adoption0e501ea

line 162why, one step back
162 tty.Println(m.matches[m.cursor].Str)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 163–167why, one step back
163 }
164165 return nil166}167

feat(filter): --select-if-one returns if single match (#778)Carlos Alexandro Beckerbefore adoption0e501ea

line 168why, one step back
168func (o Options) checkSelected(m model) {

ci: fix lintAndrey Neringbefore adoptionb0471e8

line 169why, one step back
169 out := make([]string, 0, len(m.selected))

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 170why, one step back
170 for k := range m.selected {

feat(choose,filter): --input-delimiter --output-delimiter (#779)Carlos Alexandro Beckerbefore adoption6d405c4

line 171why, one step back
171 out = append(out, k)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 172why, one step back
172 }

feat(choose,filter): --input-delimiter --output-delimiter (#779)Carlos Alexandro Beckerbefore adoption6d405c4

line 173why, one step back
173 tty.Println(strings.Join(out, o.OutputDelimiter))

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 174why, one step back
174}