Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mass history #28

Merged
merged 1 commit into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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