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 fileTake 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 back1package filter23import (4 "errors"5 "fmt"6 "os"fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7
line 7why, one step back7 "slices"refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 8–9why, one step back8 "strings"9refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 10why, one step back10 "github.com/charmbracelet/bubbles/help"refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 11–13why, one step back11 "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 back14 "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 back17 "github.com/charmbracelet/gum/internal/tty"feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 18why, one step back18 "github.com/charmbracelet/gum/style"fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 19why, one step back19 "github.com/charmbracelet/x/ansi"refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 20–37why, one step back20 "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 back38 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 back39 o.Options = strings.Split(input, o.InputDelimiter)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 40–48why, one step back40 } 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 }48feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 49–53why, one step back49 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 back54 tea.WithReportFocus(),feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 55–56why, one step back55 tea.WithContext(ctx),56 }refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 57–64why, one step back57 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 back6566 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 back73 switch {74 case o.Value != "" && o.Fuzzy:fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 75why, one step back75 matches = fuzzy.Find(o.Value, filteringChoices)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 76why, one step back76 case o.Value != "" && !o.Fuzzy:fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 77why, one step back77 matches = exactMatches(o.Value, filteringChoices)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 78why, one step back78 default:fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
line 79why, one step back79 matches = matchAll(filteringChoices)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 80–84why, one step back80 }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 back8586 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 back92 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 back96 km.ToggleAll.SetEnabled(true)refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 97why, one step back97 }feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 98why, one step back98 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 back99 m := model{fix(filter): properly handle options with ansi styles (#789)Carlos Alexandro Beckerbefore adoptiond3d20ef
lines 100–101why, one step back100 choices: choices,101 filteringChoices: filteringChoices,refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 102–116why, one step back102 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 back117 padding: []int{top, right, bottom, left},refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 118–121why, one step back118 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 back122 sort: o.Sort && o.FuzzySort,fix(filter): --no-strict not working, also weird behavior (#737)Carlos Alexandro Beckerbefore adoption29250f8
line 123why, one step back123 strict: o.Strict,refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 124–126why, one step back124 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 back127 }refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 128why, one step back128fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7
lines 129–130why, one step back129 isSelectAll := len(o.Selected) == 1 && o.Selected[0] == "*"130 currentSelected := 0feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b
line 131why, one step back131 if len(o.Selected) > 0 {fix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45
lines 132–133why, one step back132 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 back134 continue135 }136 if o.Limit == 1 {feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b
line 137why, one step back137 m.cursor = ifix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45
line 138why, one step back138 m.selected[option.Str] = struct{}{}fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7
lines 139–140why, one step back139 } else {140 currentSelected++fix(filter): take pre-filtering into accountCarlos Alexandro Beckerbefore adoptionbb34c45
line 141why, one step back141 m.selected[option.Str] = struct{}{}feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b
lines 142–146why, one step back142 }143 }144 }145146 tm, err := tea.NewProgram(m, options...).Run()refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 147–150why, one step back147 if err != nil {148 return fmt.Errorf("unable to run filter: %w", err)149 }150feat(filter): allow to pre-select items with --selected (#777)Carlos Alexandro Beckerbefore adoption966237b
line 151why, one step back151 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 back152 if !m.submitted {153 return errors.New("nothing selected")154 }refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 155–159why, one step back155156 // 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 back160 o.checkSelected(m)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 161why, one step back161 } 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 back162 tty.Println(m.matches[m.cursor].Str)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 163–167why, one step back163 }164165 return nil166}167feat(filter): --select-if-one returns if single match (#778)Carlos Alexandro Beckerbefore adoption0e501ea
line 168why, one step back168func (o Options) checkSelected(m model) {ci: fix lintAndrey Neringbefore adoptionb0471e8
line 169why, one step back169 out := make([]string, 0, len(m.selected))refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 170why, one step back170 for k := range m.selected {feat(choose,filter): --input-delimiter --output-delimiter (#779)Carlos Alexandro Beckerbefore adoption6d405c4
line 171why, one step back171 out = append(out, k)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 172why, one step back172 }feat(choose,filter): --input-delimiter --output-delimiter (#779)Carlos Alexandro Beckerbefore adoption6d405c4
line 173why, one step back173 tty.Println(strings.Join(out, o.OutputDelimiter))refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 174why, one step back174}