readline/history.go

253 lines
4.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-10-04 16:56:34 +03:00
"github.com/chzyer/readline/runes"
2015-09-22 13:16:24 +03:00
)
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 {
2015-10-04 10:23:00 +03:00
cfg *Config
2015-09-22 13:16:24 +03:00
history *list.List
historyVer int64
current *list.Element
fd *os.File
}
2015-10-04 10:23:00 +03:00
func newOpHistory(cfg *Config) (o *opHistory) {
2015-09-22 13:16:24 +03:00
o = &opHistory{
2015-10-04 10:23:00 +03:00
cfg: cfg,
2015-09-22 13:16:24 +03:00
history: list.New(),
}
2015-10-04 10:23:00 +03:00
if o.cfg.HistoryFile != "" {
o.historyUpdatePath(o.cfg.HistoryFile)
2015-09-22 13:16:24 +03:00
}
2015-10-04 10:23:00 +03:00
return
}
// only called by newOpHistory
func (o *opHistory) historyUpdatePath(path string) {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
2015-09-22 13:16:24 +03:00
if err != nil {
return
}
o.fd = f
r := bufio.NewReader(o.fd)
2015-10-04 10:23:00 +03:00
total := 0
for ; ; total++ {
2015-09-22 13:16:24 +03:00
line, err := r.ReadSlice('\n')
if err != nil {
break
}
o.PushHistory([]rune(strings.TrimSpace(string(line))))
2015-10-04 10:23:00 +03:00
o.CompactHistory()
}
if total > o.cfg.HistoryLimit {
o.HistoryRewrite()
2015-09-22 13:16:24 +03:00
}
o.historyVer++
o.PushHistory(nil)
return
}
2015-10-04 10:23:00 +03:00
func (o *opHistory) CompactHistory() {
for o.history.Len() > o.cfg.HistoryLimit {
o.history.Remove(o.history.Front())
}
}
func (o *opHistory) HistoryRewrite() {
if o.cfg.HistoryFile == "" {
return
}
tmpFile := o.cfg.HistoryFile + ".tmp"
fd, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_APPEND, 0666)
if err != nil {
return
}
buf := bufio.NewWriter(fd)
for elem := o.history.Front(); elem != nil; elem = elem.Next() {
buf.WriteString(string(elem.Value.(*hisItem).Source))
}
buf.Flush()
// replace history file
if err = os.Rename(tmpFile, o.cfg.HistoryFile); err != nil {
2015-10-08 13:14:59 +03:00
fd.Close()
return
}
2015-10-04 10:23:00 +03:00
if o.fd != nil {
o.fd.Close()
}
// fd is write only, just satisfy what we need.
o.fd = fd
}
func (o *opHistory) CloseHistory() {
2015-09-22 13:16:24 +03:00
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
}
}
2015-10-04 16:56:34 +03:00
idx := runes.IndexAllBck(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
}
}
2015-10-04 16:56:34 +03:00
idx := runes.IndexAll(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))
2015-10-04 16:56:34 +03:00
if runes.Equal(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
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
}