github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/edit/histlist.go (about) 1 package edit 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/elves/elvish/store" 9 ) 10 11 // Command history listing mode. 12 13 var ErrStoreOffline = errors.New("store offline") 14 15 type histlist struct { 16 listing 17 all []string 18 filtered []string 19 } 20 21 func (hl *histlist) Len() int { 22 return len(hl.filtered) 23 } 24 25 func (hl *histlist) Show(i, width int) styled { 26 entry := hl.filtered[i] 27 return unstyled(TrimEachLineWcWidth(entry, width)) 28 } 29 30 func (hl *histlist) Filter(filter string) int { 31 hl.filtered = nil 32 for _, item := range hl.all { 33 if strings.Contains(item, filter) { 34 hl.filtered = append(hl.filtered, item) 35 } 36 } 37 // Select the last entry. 38 return len(hl.filtered) - 1 39 } 40 41 func (hl *histlist) Accept(i int, ed *Editor) { 42 line := hl.filtered[i] 43 if len(ed.line) > 0 { 44 line = "\n" + line 45 } 46 ed.insertAtDot(line) 47 } 48 49 func (hl *histlist) ModeTitle(i int) string { 50 return fmt.Sprintf(" HISTORY #%d ", i) 51 } 52 53 func startHistlist(ed *Editor) { 54 hl, err := newHistlist(ed.store) 55 if err != nil { 56 ed.notify("%v", err) 57 return 58 } 59 60 ed.histlist = hl 61 // ed.histlist = newListing(modeHistoryListing, hl) 62 ed.mode = ed.histlist 63 } 64 65 func newHistlist(s *store.Store) (*histlist, error) { 66 if s == nil { 67 return nil, ErrStoreOffline 68 } 69 seq, err := s.NextCmdSeq() 70 if err != nil { 71 return nil, err 72 } 73 all, err := s.Cmds(0, seq) 74 if err != nil { 75 return nil, err 76 } 77 hl := &histlist{all: all} 78 hl.listing = newListing(modeHistoryListing, hl) 79 return hl, nil 80 }