Print selected items in the order they are selected
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 0a83705 · Browse files · 2 files·+48 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+48 added−9 removed
- tests:+13 added−0 removed
- other:+35 added−9 removed
Provenance
- Commit
5c0dc79- Tree
4587e74- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
0a83705
2 files changed+48 added−9 removed
modified:src/terminal.go+35 added-9 removed
28 unmodified lines29 count int30 progress int31 reading bool32 merger *Merger33- selected map[*string]*string33+ selected map[*string]selectedItem34 reqBox *util.EventBox35 eventBox *util.EventBox36 mutex sync.Mutex37 initFunc func()38 suppress bool39 }40 41+type selectedItem struct {42+ at time.Time43+ text *string44+}45+46+type ByTimeOrder []selectedItem47+48+func (a ByTimeOrder) Len() int {49+ return len(a)50+}51+52+func (a ByTimeOrder) Swap(i, j int) {53+ a[i], a[j] = a[j], a[i]54+}55+56+func (a ByTimeOrder) Less(i, j int) bool {57+ return a[i].at.Before(a[j].at)58+}59+60 var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}61 62 const (63 reqPrompt util.EventType = iota24 unmodified lines88 input: input,89 multi: opts.Multi,90 printQuery: opts.PrintQuery,91 merger: EmptyMerger,73- selected: make(map[*string]*string),92+ selected: make(map[*string]selectedItem),93 reqBox: util.NewEventBox(),94 eventBox: eventBox,95 mutex: sync.Mutex{},96 suppress: true,60 unmodified lines157 if cnt > 0 && cnt > t.cy {158 fmt.Println(t.merger.Get(t.listIndex(t.cy)).AsString())159 }160 } else {142- for ptr, orig := range t.selected {143- if orig != nil {144- fmt.Println(*orig)145- } else {146- fmt.Println(*ptr)147- }161+ sels := make([]selectedItem, 0, len(t.selected))162+ for _, sel := range t.selected {163+ sels = append(sels, sel)164 }165+ sort.Sort(ByTimeOrder(sels))166+ for _, sel := range sels {167+ fmt.Println(*sel.text)168+ }169 }170 }171 172 func displayWidth(runes []rune) int {290 unmodified lines463 idx := t.listIndex(t.cy)464 if idx < t.merger.Length() {465 item := t.merger.Get(idx)466 if _, found := t.selected[item.text]; !found {447- t.selected[item.text] = item.origText467+ var strptr *string468+ if item.origText != nil {469+ strptr = item.origText470+ } else {471+ strptr = item.text472+ }473+ t.selected[item.text] = selectedItem{time.Now(), strptr}474 } else {475 delete(t.selected, item.text)476 }477 req(reqInfo)177 unmodified linesmodified:test/test_go.rb+13 added
187 unmodified lines188 tmux.send_keys "C-M"189 tmux.until { |lines| lines.last !~ /^>/ }190 tmux.close191 end192+193+ def test_fzf_multi_order194+ tmux.send_keys "seq 1 10 | fzf --multi > #{tempname}", :Enter195+ tmux.until { |lines| lines.last =~ /^>/ }196+197+ tmux.send_keys :Tab, :Up, :Up, :Tab, :Tab, :Tab, # 3, 2198+ 'C-K', 'C-K', 'C-K', 'C-K', :BTab, :BTab, # 5, 6199+ :PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7200+ tmux.until { |lines| lines[-2].include? '(6)' }201+ tmux.send_keys "C-M"202+ assert_equal %w[3 2 5 6 8 7], File.read(tempname).split($/)203+ tmux.close204+ end205 end206