add command counts

This commit is contained in:
Gokcehan 2016-09-09 00:50:05 +03:00
parent a31ce17b90
commit ee0f931db8
2 changed files with 41 additions and 14 deletions

6
app.go
View File

@ -61,11 +61,13 @@ func (app *App) handleInp() {
return return
} }
e := app.ui.getExpr(app.nav) e, c := app.ui.getExpr(app.nav)
if e == nil { if e == nil {
continue continue
} }
e.eval(app, nil) for i := 0; i < c; i++ {
e.eval(app, nil)
}
if gExitFlag { if gExitFlag {
continue continue
} }

49
ui.go
View File

@ -450,20 +450,27 @@ func findBinds(keys map[string]Expr, prefix string) (binds map[string]Expr, ok b
return return
} }
func (ui *UI) getExpr(nav *Nav) Expr { func (ui *UI) getExpr(nav *Nav) (expr Expr, count int) {
r := &CallExpr{"renew", nil} expr = &CallExpr{"renew", nil}
count = 1
var acc []rune var acc []rune
var cnt []rune
for { for {
switch ev := termbox.PollEvent(); ev.Type { switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey: case termbox.EventKey:
if ev.Ch != 0 { if ev.Ch != 0 {
switch ev.Ch { switch {
case '<': case ev.Ch == '<':
acc = append(acc, '<', 'l', 't', '>') acc = append(acc, '<', 'l', 't', '>')
case '>': case ev.Ch == '>':
acc = append(acc, '<', 'g', 't', '>') acc = append(acc, '<', 'g', 't', '>')
// Interpret digits as command count but only do this for
// digits preceding any non-digit characters
// (e.g. "42y2k" as 42 times "y2k").
case unicode.IsDigit(ev.Ch) && len(acc) == 0:
cnt = append(cnt, ev.Ch)
default: default:
acc = append(acc, ev.Ch) acc = append(acc, ev.Ch)
} }
@ -569,7 +576,7 @@ func (ui *UI) getExpr(nav *Nav) Expr {
acc = append(acc, '<', 'c', '-', 'z', '>') acc = append(acc, '<', 'c', '-', 'z', '>')
case termbox.KeyEsc: // also KeyCtrlLsqBracket and KeyCtrl3 case termbox.KeyEsc: // also KeyCtrlLsqBracket and KeyCtrl3
acc = nil acc = nil
return r return
case termbox.KeyCtrlBackslash: // also KeyCtrl4 case termbox.KeyCtrlBackslash: // also KeyCtrl4
acc = append(acc, '<', 'c', '-', '\\', '>') acc = append(acc, '<', 'c', '-', '\\', '>')
case termbox.KeyCtrlRsqBracket: // also KeyCtrl5 case termbox.KeyCtrlRsqBracket: // also KeyCtrl5
@ -591,23 +598,41 @@ func (ui *UI) getExpr(nav *Nav) Expr {
case 0: case 0:
ui.message = fmt.Sprintf("unknown mapping: %s", string(acc)) ui.message = fmt.Sprintf("unknown mapping: %s", string(acc))
acc = nil acc = nil
return r return
case 1: case 1:
if ok { if ok {
return gOpts.keys[string(acc)] if len(cnt) > 0 {
c, err := strconv.Atoi(string(cnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
}
return gOpts.keys[string(acc)], count
} }
ui.draw(nav) ui.draw(nav)
ui.listBinds(binds) if len(acc) > 0 {
ui.listBinds(binds)
}
default: default:
if ok { if ok {
// TODO: use a delay // TODO: use a delay
return gOpts.keys[string(acc)] if len(cnt) > 0 {
c, err := strconv.Atoi(string(cnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
}
return gOpts.keys[string(acc)], count
} }
ui.draw(nav) ui.draw(nav)
ui.listBinds(binds) if len(acc) > 0 {
ui.listBinds(binds)
}
} }
case termbox.EventResize: case termbox.EventResize:
return r return
default: default:
// TODO: handle other events // TODO: handle other events
} }