Skip to content
Myr

command.go

This file

Path

filter/command.go

174 lines · 4.5 KiB · Go

Last changed by

ci: fix lintAndrey Neringbefore adoptionb0471e8

History of this file

Take it

The walk stopped after 200 commits: lines older than 19a93b0 point at it.

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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

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

line 10why, one step back
10 "github.com/charmbracelet/bubbles/help"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 11–13why, one step back
11 "github.com/charmbracelet/bubbles/textinput"
12 "github.com/charmbracelet/bubbles/viewport"13 tea "github.com/charmbracelet/bubbletea"

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

lines 14–16why, one step back
14 "github.com/charmbracelet/gum/internal/files"
15 "github.com/charmbracelet/gum/internal/stdin"16 "github.com/charmbracelet/gum/internal/timeout"

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

line 17why, one step back
17 "github.com/charmbracelet/gum/internal/tty"

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

line 18why, one step back
18 "github.com/charmbracelet/gum/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"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 20–37why, 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.Prompt30 i.PromptStyle = o.PromptStyle.ToLipgloss()31 i.PlaceholderStyle = o.PlaceholderStyle.ToLipgloss()32 i.Placeholder = o.Placeholder33 i.Width = o.Width3435 v := viewport.New(o.Width, o.Height)3637 if len(o.Options) == 0 {

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

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

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

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

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 40–48why, one step back
40 } else {
41 o.Options = files.List()42 }43 }4445 if len(o.Options) == 0 {46 return errors.New("no options provided, see `gum filter --help`")47 }48

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

lines 49–53why, one step back
49 ctx, cancel := timeout.Context(o.Timeout)
50 defer cancel()5152 options := []tea.ProgramOption{53 tea.WithOutput(os.Stderr),

feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65

line 54why, one step back
54 tea.WithReportFocus(),

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

lines 55–56why, one step back
55 tea.WithContext(ctx),
56 }

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 57–64why, one step back
57 if o.Height == 0 {
58 options = append(options, tea.WithAltScreen())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 }

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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,

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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},

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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 }

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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()

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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 }

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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))

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

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))

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 174why, one step back
174}