#19 fixed mass history

This commit is contained in:
Cheney 2016-02-17 22:20:03 +08:00
parent 21acaf90fd
commit 00e1a8e95a
2 changed files with 20 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package readline
import ( import (
"bufio" "bufio"
"container/list" "container/list"
"fmt"
"os" "os"
"strings" "strings"
@ -184,7 +185,7 @@ func (o *opHistory) Prev() []rune {
return nil return nil
} }
o.current = current o.current = current
return o.showItem(current.Value) return runes.Copy(o.showItem(current.Value))
} }
func (o *opHistory) Next() ([]rune, bool) { func (o *opHistory) Next() ([]rune, bool) {
@ -197,11 +198,19 @@ func (o *opHistory) Next() ([]rune, bool) {
} }
o.current = current o.current = current
return o.showItem(current.Value), true 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))
}
} }
// save history // save history
func (o *opHistory) New(current []rune) { func (o *opHistory) New(current []rune) {
current = runes.Copy(current)
// if just use last command without modify // if just use last command without modify
// just clean lastest history // just clean lastest history
if back := o.history.Back(); back != nil { if back := o.history.Back(); back != nil {
@ -227,9 +236,11 @@ func (o *opHistory) New(current []rune) {
if o.current != o.history.Back() { if o.current != o.history.Back() {
// move history item to current command // move history item to current command
use := o.current.Value.(*hisItem) currentItem := o.current.Value.(*hisItem)
// set current to last item
o.current = o.history.Back() o.current = o.history.Back()
current = use.Tmp
current = runes.Copy(currentItem.Tmp)
} }
o.Update(current, true) o.Update(current, true)
@ -245,6 +256,7 @@ func (o *opHistory) Revert() {
} }
func (o *opHistory) Update(s []rune, commit bool) { func (o *opHistory) Update(s []rune, commit bool) {
s = runes.Copy(s)
if o.current == nil { if o.current == nil {
o.Push(s) o.Push(s)
o.Compact() o.Compact()
@ -253,8 +265,7 @@ func (o *opHistory) Update(s []rune, commit bool) {
r := o.current.Value.(*hisItem) r := o.current.Value.(*hisItem)
r.Version = o.historyVer r.Version = o.historyVer
if commit { if commit {
r.Source = make([]rune, len(s)) r.Source = s
copy(r.Source, s)
if o.fd != nil { if o.fd != nil {
o.fd.Write([]byte(string(r.Source) + "\n")) o.fd.Write([]byte(string(r.Source) + "\n"))
} }
@ -266,8 +277,7 @@ func (o *opHistory) Update(s []rune, commit bool) {
} }
func (o *opHistory) Push(s []rune) { func (o *opHistory) Push(s []rune) {
newCopy := make([]rune, len(s)) s = runes.Copy(s)
copy(newCopy, s) elem := o.history.PushBack(&hisItem{Source: s})
elem := o.history.PushBack(&hisItem{Source: newCopy})
o.current = elem o.current = elem
} }

View File

@ -360,7 +360,7 @@ func (r *RuneBuffer) output() []byte {
} }
func (r *RuneBuffer) Reset() []rune { func (r *RuneBuffer) Reset() []rune {
ret := r.buf ret := runes.Copy(r.buf)
r.buf = r.buf[:0] r.buf = r.buf[:0]
r.idx = 0 r.idx = 0
return ret return ret