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 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–6why, one step back1package filter23import (4 "errors"5 "fmt"6 "os"fix(filter): select allCarlos Alexandro Beckerbefore adoptionb0ec3a7
line 7why, one step back7 "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 back8 "strings"9chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 10–13why, one step back10 "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 back14 "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 back19 "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 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.Promptchore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 30–35why, one step back30 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 back36 i.Placeholder = o.Placeholderchore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 37why, one step back37 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 back38chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 39why, one step back39 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 back4041 if len(o.Options) == 0 {feat: --no-strip-ansi (#784)Carlos Alexandro Beckerbefore adoption4cedf9f
line 42why, one step back42 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 back43 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 back44 } 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 }52feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 53–58why, one step back53 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 back59 }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 }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 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)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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)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 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,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 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},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 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 }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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()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 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 }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 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)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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)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 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))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
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))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 174why, one step back174}