Skip to content

Commit

Permalink
report error if it save history fail
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Feb 18, 2016
1 parent 0106614 commit 6368045
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
16 changes: 10 additions & 6 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (o *opHistory) debug() {
}

// save history
func (o *opHistory) New(current []rune) {
func (o *opHistory) New(current []rune) (err error) {
current = runes.Copy(current)
// if just use last command without modify
// just clean lastest history
Expand All @@ -221,7 +221,7 @@ func (o *opHistory) New(current []rune) {
o.current = o.history.Back()
o.current.Value.(*hisItem).Clean()
o.historyVer++
return
return nil
}
}
}
Expand All @@ -230,7 +230,7 @@ func (o *opHistory) New(current []rune) {
if o.current != nil {
o.current.Value.(*hisItem).Clean()
o.historyVer++
return
return nil
}
}

Expand All @@ -243,19 +243,21 @@ func (o *opHistory) New(current []rune) {
current = runes.Copy(currentItem.Tmp)
}

o.Update(current, true)
// err only can be a IO error, just report
err = o.Update(current, true)

// push a new one to commit current command
o.historyVer++
o.Push(nil)
return
}

func (o *opHistory) Revert() {
o.historyVer++
o.current = o.history.Back()
}

func (o *opHistory) Update(s []rune, commit bool) {
func (o *opHistory) Update(s []rune, commit bool) (err error) {
s = runes.Copy(s)
if o.current == nil {
o.Push(s)
Expand All @@ -267,13 +269,15 @@ func (o *opHistory) Update(s []rune, commit bool) {
if commit {
r.Source = s
if o.fd != nil {
o.fd.Write([]byte(string(r.Source) + "\n"))
// just report the error
_, err = o.fd.Write([]byte(string(r.Source) + "\n"))
}
} else {
r.Tmp = append(r.Tmp[:0], s...)
}
o.current.Value = r
o.Compact()
return
}

func (o *opHistory) Push(s []rune) {
Expand Down
9 changes: 6 additions & 3 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ func (o *Operation) ioloop() {
}
o.outchan <- data
if !o.cfg.DisableAutoSaveHistory {
o.history.New(data)
// ignore IO error
_ = o.history.New(data)
} else {
isUpdateHistory = false
}
Expand Down Expand Up @@ -407,8 +408,10 @@ func (op *Operation) SetConfig(cfg *Config) (*Config, error) {
return old, nil
}

func (o *Operation) SaveHistory(content string) {
o.history.New([]rune(content))
// if err is not nil, it just mean it fail to write to file
// other things goes fine.
func (o *Operation) SaveHistory(content string) error {
return o.history.New([]rune(content))
}

func (o *Operation) Refresh() {
Expand Down

0 comments on commit 6368045

Please sign in to comment.