cli: Integrate lastcmd mode.

This commit is contained in:
Qi Xiao 2019-05-02 01:14:01 +01:00
parent e9c62dedff
commit 7c46021a09
6 changed files with 44 additions and 4 deletions

View File

@ -33,6 +33,7 @@ func main() {
Binding: cli.MapBinding(map[ui.Key]cli.KeyHandler{
ui.K('D', ui.Ctrl): cli.CommitEOF,
ui.K('R', ui.Ctrl): cli.StartHistlist,
ui.K(',', ui.Alt): cli.StartLastcmd,
ui.Default: cli.DefaultInsert,
}),
},

View File

@ -6,14 +6,17 @@ import (
"github.com/elves/elvish/cli/clicore"
"github.com/elves/elvish/cli/histlist"
"github.com/elves/elvish/cli/histutil"
"github.com/elves/elvish/cli/lastcmd"
"github.com/elves/elvish/cli/listing"
)
// App represents a CLI app.
type App struct {
core *clicore.App
cfg *AppConfig
core *clicore.App
cfg *AppConfig
histlist *histlist.Mode
lastcmd *lastcmd.Mode
}
// AppConfig is a struct containing configurations for initializing an App.
@ -23,16 +26,23 @@ type AppConfig struct {
BeforeReadline []func()
AfterReadline []func(string)
Highlighter Highlighter
Highlighter Highlighter
Prompt, RPrompt Prompt
RPromptPersistent bool
HistoryStore histutil.Store
Wordifier Wordifier
InsertModeConfig InsertModeConfig
HistlistModeConfig HistlistModeConfig
LastcmdModeConfig LastcmdModeConfig
}
// Wordifier is the type of a function that turns code into words.
type Wordifier func(code string) []string
// NewAppFromStdIO creates a new App that reads from stdin and writes to stderr.
func NewAppFromStdIO(cfg *AppConfig) *App {
return NewAppFromFiles(cfg, os.Stdin, os.Stderr)
@ -78,6 +88,10 @@ func NewApp(cfg *AppConfig, t clicore.TTY, sigs clicore.SignalSource) *App {
Mode: lsMode,
KeyHandler: adaptBinding(cfg.HistlistModeConfig.Binding, app),
}
app.lastcmd = &lastcmd.Mode{
Mode: lsMode,
KeyHandler: adaptBinding(cfg.LastcmdModeConfig.Binding, app),
}
return app
}

25
cli/lastcmd_mode.go Normal file
View File

@ -0,0 +1,25 @@
package cli
import "strings"
// LastcmdModeConfig is a struct containing configuration for the lastcmd mode.
type LastcmdModeConfig struct {
Binding Binding
}
// StartLastcmd starts the lastcmd mode.
func StartLastcmd(ev KeyEvent) {
app := ev.App()
cmd, err := app.cfg.HistoryStore.LastCmd()
if err != nil {
ev.State().AddNote("db error: " + err.Error())
return
}
wordifier := app.cfg.Wordifier
if wordifier == nil {
wordifier = strings.Fields
}
words := wordifier(cmd.Text)
app.lastcmd.Start(cmd.Text, words)
ev.State().SetMode(app.lastcmd)
}

View File

@ -1,9 +1,9 @@
package newedit
import (
"github.com/elves/elvish/cli/lastcmd"
"github.com/elves/elvish/cli/listing"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/newedit/lastcmd"
"github.com/elves/elvish/parse/parseutil"
"github.com/elves/elvish/store/storedefs"
)