readline/history.go

293 lines
5.4 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"
2016-02-17 17:20:03 +03:00
"fmt"
2015-09-22 13:16:24 +03:00
"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-11-20 15:56:42 +03:00
return o
}
2016-03-31 05:55:53 +03:00
func (o *opHistory) Reset() {
o.history = list.New()
o.current = nil
}
2015-11-20 15:56:42 +03:00
func (o *opHistory) IsHistoryClosed() bool {
return o.fd.Fd() == ^(uintptr(0))
}
func (o *opHistory) Init() {
2015-11-20 15:56:42 +03:00
if o.IsHistoryClosed() {
o.initHistory()
}
}
func (o *opHistory) initHistory() {
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
}
// 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.Push([]rune(strings.TrimSpace(string(line))))
o.Compact()
2015-10-04 10:23:00 +03:00
}
if total > o.cfg.HistoryLimit {
o.Rewrite()
2015-09-22 13:16:24 +03:00
}
o.historyVer++
o.Push(nil)
2015-09-22 13:16:24 +03:00
return
}
func (o *opHistory) Compact() {
2015-11-20 15:56:42 +03:00
for o.history.Len() > o.cfg.HistoryLimit && o.history.Len() > 0 {
2015-10-04 10:23:00 +03:00
o.history.Remove(o.history.Front())
}
}
func (o *opHistory) Rewrite() {
2015-10-04 10:23:00 +03:00
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) Close() {
2015-09-22 13:16:24 +03:00
if o.fd != nil {
o.fd.Close()
}
}
func (o *opHistory) FindBck(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
}
func (o *opHistory) FindFwd(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
}
func (o *opHistory) Prev() []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
2016-02-17 17:20:03 +03:00
return runes.Copy(o.showItem(current.Value))
2015-09-21 08:13:30 +03:00
}
func (o *opHistory) Next() ([]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
2016-02-17 17:20:03 +03:00
return runes.Copy(o.showItem(current.Value)), true
}
func (o *opHistory) debug() {
Debug("-------")
for item := o.history.Front(); item != nil; item = item.Next() {
Debug(fmt.Sprintf("%+v", item.Value))
}
2015-09-21 08:13:30 +03:00
}
// save history
2016-02-18 03:41:35 +03:00
func (o *opHistory) New(current []rune) (err error) {
2016-02-17 17:20:03 +03:00
current = runes.Copy(current)
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++
2016-02-18 03:41:35 +03:00
return nil
2015-09-21 17:27:40 +03:00
}
}
}
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++
2016-02-18 03:41:35 +03:00
return nil
2015-09-21 16:00:48 +03:00
}
}
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
2016-02-17 17:20:03 +03:00
currentItem := o.current.Value.(*hisItem)
// set current to last item
2015-09-21 08:30:10 +03:00
o.current = o.history.Back()
2016-02-17 17:20:03 +03:00
current = runes.Copy(currentItem.Tmp)
2015-09-21 08:13:30 +03:00
}
2016-02-18 03:41:35 +03:00
// err only can be a IO error, just report
err = o.Update(current, true)
2015-09-21 16:00:48 +03:00
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++
o.Push(nil)
2016-02-18 03:41:35 +03:00
return
2015-09-21 08:13:30 +03:00
}
func (o *opHistory) Revert() {
2015-11-20 15:56:42 +03:00
o.historyVer++
o.current = o.history.Back()
}
2016-02-18 03:41:35 +03:00
func (o *opHistory) Update(s []rune, commit bool) (err error) {
2016-02-17 17:20:03 +03:00
s = runes.Copy(s)
2015-09-21 08:30:10 +03:00
if o.current == nil {
o.Push(s)
o.Compact()
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 {
2016-02-17 17:20:03 +03:00
r.Source = s
2015-09-22 13:16:24 +03:00
if o.fd != nil {
2016-02-18 03:41:35 +03:00
// just report the error
_, err = o.fd.Write([]byte(string(r.Source) + "\n"))
2015-09-22 13:16:24 +03:00
}
2015-09-21 16:00:48 +03:00
} else {
r.Tmp = append(r.Tmp[:0], s...)
}
o.current.Value = r
o.Compact()
2016-02-18 03:41:35 +03:00
return
2015-09-21 08:13:30 +03:00
}
func (o *opHistory) Push(s []rune) {
2016-02-17 17:20:03 +03:00
s = runes.Copy(s)
elem := o.history.PushBack(&hisItem{Source: s})
2015-09-21 08:30:10 +03:00
o.current = elem
2015-09-21 08:13:30 +03:00
}