2015-09-21 08:13:30 +03:00
|
|
|
package readline
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) showItem(obj interface{}) []rune {
|
|
|
|
item := obj.(*HisItem)
|
|
|
|
if item.Version == o.historyVer {
|
|
|
|
return item.Tmp
|
|
|
|
}
|
|
|
|
return item.Source
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
func (o *Operation) PrevHistory() []rune {
|
|
|
|
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-21 16:00:48 +03:00
|
|
|
func (o *Operation) 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-21 08:30:10 +03:00
|
|
|
func (o *Operation) 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-21 16:00:48 +03:00
|
|
|
func (o *Operation) 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)
|
|
|
|
} else {
|
|
|
|
r.Tmp = append(r.Tmp[:0], s...)
|
|
|
|
}
|
|
|
|
o.current.Value = r
|
2015-09-21 08:13:30 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
func (o *Operation) 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
|
|
|
}
|