Skip to content

Commit

Permalink
chzyer#19 fixed mass history
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Feb 17, 2016
1 parent 21acaf9 commit 00e1a8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package readline
import (
"bufio"
"container/list"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -184,7 +185,7 @@ func (o *opHistory) Prev() []rune {
return nil
}
o.current = current
return o.showItem(current.Value)
return runes.Copy(o.showItem(current.Value))
}

func (o *opHistory) Next() ([]rune, bool) {
Expand All @@ -197,11 +198,19 @@ func (o *opHistory) Next() ([]rune, bool) {
}

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
func (o *opHistory) New(current []rune) {
current = runes.Copy(current)
// if just use last command without modify
// just clean lastest history
if back := o.history.Back(); back != nil {
Expand All @@ -227,9 +236,11 @@ func (o *opHistory) New(current []rune) {

if o.current != o.history.Back() {
// 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()
current = use.Tmp

current = runes.Copy(currentItem.Tmp)
}

o.Update(current, true)
Expand All @@ -245,6 +256,7 @@ func (o *opHistory) Revert() {
}

func (o *opHistory) Update(s []rune, commit bool) {
s = runes.Copy(s)
if o.current == nil {
o.Push(s)
o.Compact()
Expand All @@ -253,8 +265,7 @@ func (o *opHistory) Update(s []rune, commit bool) {
r := o.current.Value.(*hisItem)
r.Version = o.historyVer
if commit {
r.Source = make([]rune, len(s))
copy(r.Source, s)
r.Source = s
if o.fd != nil {
o.fd.Write([]byte(string(r.Source) + "\n"))
}
Expand All @@ -266,8 +277,7 @@ func (o *opHistory) Update(s []rune, commit bool) {
}

func (o *opHistory) Push(s []rune) {
newCopy := make([]rune, len(s))
copy(newCopy, s)
elem := o.history.PushBack(&hisItem{Source: newCopy})
s = runes.Copy(s)
elem := o.history.PushBack(&hisItem{Source: s})
o.current = elem
}
2 changes: 1 addition & 1 deletion runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (r *RuneBuffer) output() []byte {
}

func (r *RuneBuffer) Reset() []rune {
ret := r.buf
ret := runes.Copy(r.buf)
r.buf = r.buf[:0]
r.idx = 0
return ret
Expand Down

0 comments on commit 00e1a8e

Please sign in to comment.