add commands history

Mentioned in #81.
This commit is contained in:
Gokcehan 2017-05-15 12:30:50 +03:00
parent 33c55f0f51
commit 9962b378a8
5 changed files with 45 additions and 5 deletions

13
app.go
View File

@ -9,10 +9,17 @@ import (
"strings"
)
type cmdItem struct {
pref string
s string
}
type app struct {
ui *ui
nav *nav
quit chan bool
ui *ui
nav *nav
quit chan bool
cmdHist []cmdItem
cmdHind int
}
func newApp() *app {

2
doc.go
View File

@ -52,6 +52,8 @@ keybindings:
cmd-escape (default "<esc>")
cmd-comp (default "<tab>")
cmd-enter (default "<c-j>" and "<enter>")
cmd-hist-next (default "<c-n>")
cmd-hist-prev (default "<c-p>")
cmd-delete (default "<c-d>" and "<delete>")
cmd-delete-back (default "<bs>" and "<bs2>")
cmd-left (default "<c-b>" and "<left>")

View File

@ -56,6 +56,8 @@ keybindings:
cmd-escape (default "<esc>")
cmd-comp (default "<tab>")
cmd-enter (default "<c-j>" and "<enter>")
cmd-hist-next (default "<c-n>")
cmd-hist-prev (default "<c-p>")
cmd-delete (default "<c-d>" and "<delete>")
cmd-delete-back (default "<bs>" and "<bs2>")
cmd-left (default "<c-b>" and "<left>")

29
eval.go
View File

@ -395,7 +395,6 @@ func (e *callExpr) eval(app *app, args []string) {
}
case "cmd-escape":
app.ui.menubuf = nil
app.ui.cmdbuf = nil
app.ui.cmdlacc = nil
app.ui.cmdracc = nil
app.ui.cmdpref = ""
@ -418,7 +417,6 @@ func (e *callExpr) eval(app *app, args []string) {
return
}
app.ui.menubuf = nil
app.ui.cmdbuf = nil
app.ui.cmdlacc = nil
app.ui.cmdracc = nil
switch app.ui.cmdpref {
@ -456,7 +454,34 @@ func (e *callExpr) eval(app *app, args []string) {
default:
log.Printf("entering unknown execution prefix: %q", app.ui.cmdpref)
}
app.cmdHist = append(app.cmdHist, cmdItem{app.ui.cmdpref, s})
app.ui.cmdpref = ""
case "cmd-hist-prev":
if app.cmdHind == len(app.cmdHist) {
return
}
app.cmdHind++
cmd := app.cmdHist[len(app.cmdHist)-app.cmdHind]
app.ui.cmdpref = cmd.pref
app.ui.cmdlacc = []rune(cmd.s)
app.ui.cmdracc = nil
app.ui.menubuf = nil
case "cmd-hist-next":
if app.cmdHind > 0 {
app.cmdHind--
}
if app.cmdHind == 0 {
app.ui.menubuf = nil
app.ui.cmdlacc = nil
app.ui.cmdracc = nil
app.ui.cmdpref = ""
return
}
cmd := app.cmdHist[len(app.cmdHist)-app.cmdHind]
app.ui.cmdpref = cmd.pref
app.ui.cmdlacc = []rune(cmd.s)
app.ui.cmdracc = nil
app.ui.menubuf = nil
case "cmd-delete-back":
if len(app.ui.cmdlacc) > 0 {
app.ui.cmdlacc = app.ui.cmdlacc[:len(app.ui.cmdlacc)-1]

View File

@ -59,6 +59,8 @@ func init() {
gOpts.keys["$"] = &callExpr{"read-shell", nil}
gOpts.keys["!"] = &callExpr{"read-shell-wait", nil}
gOpts.keys["&"] = &callExpr{"read-shell-async", nil}
gOpts.keys["<c-n>"] = &callExpr{"cmd-hist-next", nil}
gOpts.keys["<c-p>"] = &callExpr{"cmd-hist-prev", nil}
gOpts.keys["/"] = &callExpr{"search", nil}
gOpts.keys["?"] = &callExpr{"search-back", nil}
gOpts.keys["n"] = &callExpr{"search-next", nil}
@ -79,6 +81,8 @@ func init() {
gOpts.cmdkeys["<tab>"] = &callExpr{"cmd-comp", nil}
gOpts.cmdkeys["<enter>"] = &callExpr{"cmd-enter", nil}
gOpts.cmdkeys["<c-j>"] = &callExpr{"cmd-enter", nil}
gOpts.cmdkeys["<c-n>"] = &callExpr{"cmd-hist-next", nil}
gOpts.cmdkeys["<c-p>"] = &callExpr{"cmd-hist-prev", nil}
gOpts.cmdkeys["<bs>"] = &callExpr{"cmd-delete-back", nil}
gOpts.cmdkeys["<bs2>"] = &callExpr{"cmd-delete-back", nil}
gOpts.cmdkeys["<delete>"] = &callExpr{"cmd-delete", nil}