readline/history.go

57 lines
1.1 KiB
Go
Raw Normal View History

2015-09-21 08:13:30 +03:00
package readline
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 08:13:30 +03:00
return current.Value.([]rune)
}
2015-09-21 08:30:10 +03:00
func (o *Operation) NextHistory() []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.Next()
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 08:13:30 +03:00
return current.Value.([]rune)
}
2015-09-21 08:30:10 +03:00
func (o *Operation) NewHistory(current []rune) {
o.UpdateHistory(current)
if o.current != o.history.Back() {
2015-09-21 08:13:30 +03:00
// move history item to current command
2015-09-21 08:30:10 +03:00
o.history.Remove(o.current)
use := o.current.Value.([]rune)
o.current = o.history.Back()
o.UpdateHistory(use)
2015-09-21 08:13:30 +03:00
}
// push a new one to commit current command
2015-09-21 08:30:10 +03:00
o.PushHistory(nil)
2015-09-21 08:13:30 +03:00
}
2015-09-21 08:30:10 +03:00
func (o *Operation) UpdateHistory(s []rune) {
if o.current == nil {
o.PushHistory(s)
2015-09-21 08:13:30 +03:00
return
}
2015-09-21 08:30:10 +03:00
r := o.current.Value.([]rune)
o.current.Value = append(r[:0], s...)
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 08:30:10 +03:00
elem := o.history.PushBack(newCopy)
o.current = elem
2015-09-21 08:13:30 +03:00
}