diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-17 11:07:04 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-17 11:07:04 +0900 |
commit | 5c0dc79ffae4c2da24ff77c4aea370287491693d (patch) | |
tree | 4587e74be1aab1598cabe8409e32002aeae55163 /src | |
parent | 0a83705d210ced8056a5ce503db50ebae2c2d44e (diff) |
Print selected items in the order they are selected
Diffstat (limited to 'src')
-rw-r--r-- | src/terminal.go | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/src/terminal.go b/src/terminal.go index abd70690..49083f7d 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -30,7 +30,7 @@ type Terminal struct { progress int reading bool merger *Merger - selected map[*string]*string + selected map[*string]selectedItem reqBox *util.EventBox eventBox *util.EventBox mutex sync.Mutex @@ -38,6 +38,25 @@ type Terminal struct { suppress bool } +type selectedItem struct { + at time.Time + text *string +} + +type ByTimeOrder []selectedItem + +func (a ByTimeOrder) Len() int { + return len(a) +} + +func (a ByTimeOrder) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +func (a ByTimeOrder) Less(i, j int) bool { + return a[i].at.Before(a[j].at) +} + var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`} const ( @@ -70,7 +89,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal { multi: opts.Multi, printQuery: opts.PrintQuery, merger: EmptyMerger, - selected: make(map[*string]*string), + selected: make(map[*string]selectedItem), reqBox: util.NewEventBox(), eventBox: eventBox, mutex: sync.Mutex{}, @@ -139,12 +158,13 @@ func (t *Terminal) output() { fmt.Println(t.merger.Get(t.listIndex(t.cy)).AsString()) } } else { - for ptr, orig := range t.selected { - if orig != nil { - fmt.Println(*orig) - } else { - fmt.Println(*ptr) - } + sels := make([]selectedItem, 0, len(t.selected)) + for _, sel := range t.selected { + sels = append(sels, sel) + } + sort.Sort(ByTimeOrder(sels)) + for _, sel := range sels { + fmt.Println(*sel.text) } } } @@ -444,7 +464,13 @@ func (t *Terminal) Loop() { if idx < t.merger.Length() { item := t.merger.Get(idx) if _, found := t.selected[item.text]; !found { - t.selected[item.text] = item.origText + var strptr *string + if item.origText != nil { + strptr = item.origText + } else { + strptr = item.text + } + t.selected[item.text] = selectedItem{time.Now(), strptr} } else { delete(t.selected, item.text) } |