This commit is contained in:
Cheney 2015-09-23 13:52:26 +08:00
parent 87df30271f
commit 3c4b5dd53d
3 changed files with 76 additions and 76 deletions

View File

@ -7,13 +7,13 @@ import (
"strings"
)
type HisItem struct {
type hisItem struct {
Source []rune
Version int64
Tmp []rune
}
func (h *HisItem) Clean() {
func (h *hisItem) Clean() {
h.Source = nil
h.Tmp = nil
}
@ -105,7 +105,7 @@ func (o *opHistory) FindHistoryFwd(isNewSearch bool, rs []rune, start int) (int,
}
func (o *opHistory) showItem(obj interface{}) []rune {
item := obj.(*HisItem)
item := obj.(*hisItem)
if item.Version == o.historyVer {
return item.Tmp
}
@ -143,10 +143,10 @@ func (o *opHistory) NewHistory(current []rune) {
if back := o.history.Back(); back != nil {
prev := back.Prev()
if prev != nil {
use := o.showItem(o.current.Value.(*HisItem))
if equalRunes(use, prev.Value.(*HisItem).Source) {
use := o.showItem(o.current.Value.(*hisItem))
if equalRunes(use, prev.Value.(*hisItem).Source) {
o.current = o.history.Back()
o.current.Value.(*HisItem).Clean()
o.current.Value.(*hisItem).Clean()
o.historyVer++
return
}
@ -155,7 +155,7 @@ func (o *opHistory) NewHistory(current []rune) {
if len(current) == 0 {
o.current = o.history.Back()
if o.current != nil {
o.current.Value.(*HisItem).Clean()
o.current.Value.(*hisItem).Clean()
o.historyVer++
return
}
@ -163,7 +163,7 @@ func (o *opHistory) NewHistory(current []rune) {
if o.current != o.history.Back() {
// move history item to current command
use := o.current.Value.(*HisItem)
use := o.current.Value.(*hisItem)
o.current = o.history.Back()
current = use.Tmp
}
@ -180,7 +180,7 @@ func (o *opHistory) UpdateHistory(s []rune, commit bool) {
o.PushHistory(s)
return
}
r := o.current.Value.(*HisItem)
r := o.current.Value.(*hisItem)
r.Version = o.historyVer
if commit {
r.Source = make([]rune, len(s))
@ -198,6 +198,6 @@ func (o *opHistory) PushHistory(s []rune) {
// copy
newCopy := make([]rune, len(s))
copy(newCopy, s)
elem := o.history.PushBack(&HisItem{Source: newCopy})
elem := o.history.PushBack(&hisItem{Source: newCopy})
o.current = elem
}

View File

@ -40,126 +40,126 @@ func NewOperation(t *Terminal, cfg *Config) *Operation {
return op
}
func (l *Operation) ioloop() {
func (o *Operation) ioloop() {
for {
keepInSearchMode := false
r := l.t.ReadRune()
r := o.t.ReadRune()
switch r {
case CharCannel:
if l.IsSearchMode() {
l.ExitSearchMode(true)
l.buf.Refresh()
if o.IsSearchMode() {
o.ExitSearchMode(true)
o.buf.Refresh()
}
case CharBckSearch:
l.SearchMode(S_DIR_BCK)
o.SearchMode(S_DIR_BCK)
keepInSearchMode = true
case CharFwdSearch:
l.SearchMode(S_DIR_FWD)
o.SearchMode(S_DIR_FWD)
keepInSearchMode = true
case CharKill:
l.buf.Kill()
o.buf.Kill()
case MetaNext:
l.buf.MoveToNextWord()
o.buf.MoveToNextWord()
case CharTranspose:
l.buf.Transpose()
o.buf.Transpose()
case MetaPrev:
l.buf.MoveToPrevWord()
o.buf.MoveToPrevWord()
case MetaDelete:
l.buf.DeleteWord()
o.buf.DeleteWord()
case CharLineStart:
l.buf.MoveToLineStart()
o.buf.MoveToLineStart()
case CharLineEnd:
l.buf.MoveToLineEnd()
o.buf.MoveToLineEnd()
case CharDelete:
l.buf.Delete()
o.buf.Delete()
case CharBackspace, CharCtrlH:
if l.IsSearchMode() {
l.SearchBackspace()
if o.IsSearchMode() {
o.SearchBackspace()
keepInSearchMode = true
} else {
l.buf.Backspace()
o.buf.Backspace()
}
case MetaBackspace, CharCtrlW:
l.buf.BackEscapeWord()
o.buf.BackEscapeWord()
case CharEnter, CharCtrlJ:
if l.IsSearchMode() {
l.ExitSearchMode(false)
if o.IsSearchMode() {
o.ExitSearchMode(false)
}
l.buf.MoveToLineEnd()
l.buf.WriteRune('\n')
data := l.buf.Reset()
o.buf.MoveToLineEnd()
o.buf.WriteRune('\n')
data := o.buf.Reset()
data = data[:len(data)-1] // trim \n
l.outchan <- data
l.NewHistory(data)
o.outchan <- data
o.NewHistory(data)
case CharBackward:
l.buf.MoveBackward()
o.buf.MoveBackward()
case CharForward:
l.buf.MoveForward()
o.buf.MoveForward()
case CharPrev:
buf := l.PrevHistory()
buf := o.PrevHistory()
if buf != nil {
l.buf.Set(buf)
o.buf.Set(buf)
}
case CharNext:
buf, ok := l.NextHistory()
buf, ok := o.NextHistory()
if ok {
l.buf.Set(buf)
o.buf.Set(buf)
}
case CharInterrupt:
if l.IsSearchMode() {
l.ExitSearchMode(false)
if o.IsSearchMode() {
o.ExitSearchMode(false)
}
l.buf.MoveToLineEnd()
l.buf.Refresh()
l.buf.WriteString("^C\n")
l.outchan <- nil
o.buf.MoveToLineEnd()
o.buf.Refresh()
o.buf.WriteString("^C\n")
o.outchan <- nil
default:
if l.IsSearchMode() {
l.SearchChar(r)
if o.IsSearchMode() {
o.SearchChar(r)
keepInSearchMode = true
} else {
l.buf.WriteRune(r)
o.buf.WriteRune(r)
}
}
if !keepInSearchMode && l.IsSearchMode() {
l.ExitSearchMode(false)
l.buf.Refresh()
if !keepInSearchMode && o.IsSearchMode() {
o.ExitSearchMode(false)
o.buf.Refresh()
}
if !l.IsSearchMode() {
l.UpdateHistory(l.buf.Runes(), false)
if !o.IsSearchMode() {
o.UpdateHistory(o.buf.Runes(), false)
}
}
}
func (l *Operation) Stderr() io.Writer {
return &wrapWriter{target: os.Stderr, r: l}
func (o *Operation) Stderr() io.Writer {
return &wrapWriter{target: os.Stderr, r: o}
}
func (l *Operation) String() (string, error) {
r, err := l.Runes()
func (o *Operation) String() (string, error) {
r, err := o.Runes()
if err != nil {
return "", err
}
return string(r), nil
}
func (l *Operation) Runes() ([]rune, error) {
l.buf.Refresh() // print prompt
r := <-l.outchan
func (o *Operation) Runes() ([]rune, error) {
o.buf.Refresh() // print prompt
r := <-o.outchan
if r == nil {
return nil, io.EOF
}
return r, nil
}
func (l *Operation) Slice() ([]byte, error) {
r, err := l.Runes()
func (o *Operation) Slice() ([]byte, error) {
r, err := o.Runes()
if err != nil {
return nil, err
}
return []byte(string(r)), nil
}
func (l *Operation) Close() {
l.opHistory.Close()
func (o *Operation) Close() {
o.opHistory.Close()
}

View File

@ -48,19 +48,19 @@ func (r *RuneBuffer) MoveBackward() {
r.Refresh()
}
func (rb *RuneBuffer) WriteString(s string) {
rb.WriteRunes([]rune(s))
func (r *RuneBuffer) WriteString(s string) {
r.WriteRunes([]rune(s))
}
func (rb *RuneBuffer) WriteRune(r rune) {
rb.WriteRunes([]rune{r})
func (r *RuneBuffer) WriteRune(s rune) {
r.WriteRunes([]rune{s})
}
func (rb *RuneBuffer) WriteRunes(r []rune) {
tail := append(r, rb.buf[rb.idx:]...)
rb.buf = append(rb.buf[:rb.idx], tail...)
rb.idx++
rb.Refresh()
func (r *RuneBuffer) WriteRunes(s []rune) {
tail := append(s, r.buf[r.idx:]...)
r.buf = append(r.buf[:r.idx], tail...)
r.idx++
r.Refresh()
}
func (r *RuneBuffer) MoveForward() {