readline/history.go

158 lines
2.8 KiB
Go
Raw Normal View History

2015-09-21 08:13:30 +03:00
package readline
2015-09-22 13:16:24 +03:00
import (
"bufio"
"container/list"
"os"
"strings"
)
2015-09-21 16:00:48 +03:00
type HisItem struct {
Source []rune
Version int64
Tmp []rune
}
func (h *HisItem) Clean() {
h.Source = nil
h.Tmp = nil
}
2015-09-22 13:16:24 +03:00
type opHistory struct {
path string
history *list.List
historyVer int64
current *list.Element
fd *os.File
}
func newOpHistory(path string) (o *opHistory) {
o = &opHistory{
path: path,
history: list.New(),
}
if o.path == "" {
return
}
f, err := os.OpenFile(o.path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return
}
o.fd = f
r := bufio.NewReader(o.fd)
for {
line, err := r.ReadSlice('\n')
if err != nil {
break
}
o.PushHistory([]rune(strings.TrimSpace(string(line))))
}
o.historyVer++
o.PushHistory(nil)
return
}
func (o *opHistory) Close() {
if o.fd != nil {
o.fd.Close()
}
}
func (o *opHistory) showItem(obj interface{}) []rune {
2015-09-21 16:00:48 +03:00
item := obj.(*HisItem)
if item.Version == o.historyVer {
return item.Tmp
}
return item.Source
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) PrevHistory() []rune {
2015-09-21 08:30:10 +03:00
if o.current == nil {
2015-09-21 08:13:30 +03:00
return nil
}
2015-09-21 08:30:10 +03:00
current := o.current.Prev()
2015-09-21 08:13:30 +03:00
if current == nil {
return nil
}
2015-09-21 08:30:10 +03:00
o.current = current
2015-09-21 16:00:48 +03:00
return o.showItem(current.Value)
2015-09-21 08:13:30 +03:00
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) NextHistory() ([]rune, bool) {
2015-09-21 08:30:10 +03:00
if o.current == nil {
2015-09-21 16:00:48 +03:00
return nil, false
2015-09-21 08:13:30 +03:00
}
2015-09-21 08:30:10 +03:00
current := o.current.Next()
2015-09-21 08:13:30 +03:00
if current == nil {
2015-09-21 16:00:48 +03:00
return nil, false
2015-09-21 08:13:30 +03:00
}
2015-09-21 16:00:48 +03:00
2015-09-21 08:30:10 +03:00
o.current = current
2015-09-21 16:00:48 +03:00
return o.showItem(current.Value), true
2015-09-21 08:13:30 +03:00
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) NewHistory(current []rune) {
2015-09-21 16:00:48 +03:00
// if just use last command without modify
// just clean lastest history
2015-09-21 17:27:40 +03:00
if back := o.history.Back(); back != nil {
prev := back.Prev()
if prev != nil {
use := o.current.Value.(*HisItem)
if equalRunes(use.Tmp, prev.Value.(*HisItem).Source) {
o.current = o.history.Back()
o.current.Value.(*HisItem).Clean()
o.historyVer++
return
}
}
}
if len(current) == 0 {
o.current = o.history.Back()
if o.current != nil {
2015-09-21 16:00:48 +03:00
o.current.Value.(*HisItem).Clean()
o.historyVer++
return
}
}
2015-09-21 08:30:10 +03:00
if o.current != o.history.Back() {
2015-09-21 08:13:30 +03:00
// move history item to current command
2015-09-21 16:00:48 +03:00
use := o.current.Value.(*HisItem)
2015-09-21 08:30:10 +03:00
o.current = o.history.Back()
2015-09-21 16:00:48 +03:00
current = use.Tmp
2015-09-21 08:13:30 +03:00
}
2015-09-21 16:00:48 +03:00
o.UpdateHistory(current, true)
2015-09-21 08:13:30 +03:00
// push a new one to commit current command
2015-09-21 16:00:48 +03:00
o.historyVer++
2015-09-21 08:30:10 +03:00
o.PushHistory(nil)
2015-09-21 08:13:30 +03:00
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) UpdateHistory(s []rune, commit bool) {
2015-09-21 08:30:10 +03:00
if o.current == nil {
o.PushHistory(s)
2015-09-21 08:13:30 +03:00
return
}
2015-09-21 16:00:48 +03:00
r := o.current.Value.(*HisItem)
r.Version = o.historyVer
if commit {
r.Source = make([]rune, len(s))
copy(r.Source, s)
2015-09-22 13:16:24 +03:00
if o.fd != nil {
o.fd.Write([]byte(string(r.Source) + "\n"))
}
2015-09-21 16:00:48 +03:00
} else {
r.Tmp = append(r.Tmp[:0], s...)
}
o.current.Value = r
2015-09-21 08:13:30 +03:00
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) PushHistory(s []rune) {
2015-09-21 08:13:30 +03:00
// copy
newCopy := make([]rune, len(s))
copy(newCopy, s)
2015-09-21 16:00:48 +03:00
elem := o.history.PushBack(&HisItem{Source: newCopy})
2015-09-21 08:30:10 +03:00
o.current = elem
2015-09-21 08:13:30 +03:00
}