readline/history.go

206 lines
3.7 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-23 08:52:26 +03:00
type hisItem struct {
2015-09-21 16:00:48 +03:00
Source []rune
Version int64
Tmp []rune
}
2015-09-23 08:52:26 +03:00
func (h *hisItem) Clean() {
2015-09-21 16:00:48 +03:00
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()
}
}
2015-09-23 06:10:36 +03:00
func (o *opHistory) FindHistoryBck(isNewSearch bool, rs []rune, start int) (int, *list.Element) {
2015-09-22 18:01:15 +03:00
for elem := o.current; elem != nil; elem = elem.Prev() {
2015-09-23 06:10:36 +03:00
item := o.showItem(elem.Value)
if isNewSearch {
start += len(rs)
}
if elem == o.current {
2015-09-23 06:26:20 +03:00
if len(item) >= start {
item = item[:start]
2015-09-23 06:10:36 +03:00
}
}
idx := RunesIndexBck(item, rs)
2015-09-22 18:01:15 +03:00
if idx < 0 {
continue
}
return idx, elem
}
return -1, nil
}
2015-09-23 06:10:36 +03:00
func (o *opHistory) FindHistoryFwd(isNewSearch bool, rs []rune, start int) (int, *list.Element) {
2015-09-22 18:01:15 +03:00
for elem := o.current; elem != nil; elem = elem.Next() {
2015-09-23 06:10:36 +03:00
item := o.showItem(elem.Value)
if isNewSearch {
start -= len(rs)
2015-09-23 06:26:20 +03:00
if start < 0 {
start = 0
}
2015-09-23 06:10:36 +03:00
}
if elem == o.current {
2015-09-23 06:26:20 +03:00
if len(item)-1 >= start {
item = item[start:]
2015-09-23 09:22:22 +03:00
} else {
continue
2015-09-23 06:10:36 +03:00
}
}
idx := RunesIndex(item, rs)
2015-09-22 18:01:15 +03:00
if idx < 0 {
continue
}
2015-09-23 06:10:36 +03:00
if elem == o.current {
idx += start
}
2015-09-22 18:01:15 +03:00
return idx, elem
}
return -1, nil
}
2015-09-22 13:16:24 +03:00
func (o *opHistory) showItem(obj interface{}) []rune {
2015-09-23 08:52:26 +03:00
item := obj.(*hisItem)
2015-09-21 16:00:48 +03:00
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 {
2015-09-23 08:52:26 +03:00
use := o.showItem(o.current.Value.(*hisItem))
if equalRunes(use, prev.Value.(*hisItem).Source) {
2015-09-21 17:27:40 +03:00
o.current = o.history.Back()
2015-09-23 08:52:26 +03:00
o.current.Value.(*hisItem).Clean()
2015-09-21 17:27:40 +03:00
o.historyVer++
return
}
}
}
if len(current) == 0 {
o.current = o.history.Back()
if o.current != nil {
2015-09-23 08:52:26 +03:00
o.current.Value.(*hisItem).Clean()
2015-09-21 16:00:48 +03:00
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-23 08:52:26 +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-23 08:52:26 +03:00
r := o.current.Value.(*hisItem)
2015-09-21 16:00:48 +03:00
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-23 08:52:26 +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
}