From 24554f1235db166bfaa4045e7692c9b52844ceef Mon Sep 17 00:00:00 2001 From: Daisuke Maki Date: Thu, 3 Jul 2014 05:51:52 +0900 Subject: [PATCH] Document and rename stuff --- action.go | 73 ++++++++++++++++++++++++++++++++----------------------- keymap.go | 2 +- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/action.go b/action.go index f3df83d..0501d57 100644 --- a/action.go +++ b/action.go @@ -6,76 +6,89 @@ import ( "github.com/nsf/termbox-go" ) -var handlers map[string]Action -var defaultKeyBinding map[termbox.Key]Action - - +// Action describes an action that can be executed upon receiving user +// input. It's an interface so you can create any kind of Action you need, +// but the most everything is implemented in terms of ActionFunc, which is +// callback based Action type Action interface { + Register(string, ...termbox.Key) Execute(*Input, termbox.Event) } +// ActionFunc is a type of Action that is basically just a callback. type ActionFunc func(*Input, termbox.Event) +// This is the global map of canonical action name to actions +var nameToActions map[string]Action + +// This is the default keybinding used by NewKeymap() +var defaultKeyBinding map[termbox.Key]Action + +// Execute fulfills the Action interface for AfterFunc func (a ActionFunc) Execute(i *Input, e termbox.Event) { a(i, e) } -func (a ActionFunc) register(name string, defaultKeys ...termbox.Key) { - handlers["peco."+name] = a +// Register fulfills the Actin interface for AfterFunc. Registers `a` +// into the global action registry by the name `name`, and maps to +// default keys via `defaultKeys` +func (a ActionFunc) Register(name string, defaultKeys ...termbox.Key) { + nameToActions["peco."+name] = a for _, k := range defaultKeys { defaultKeyBinding[k] = a } } func init() { - handlers = map[string]Action{} + // Build the global maps + nameToActions = map[string]Action{} defaultKeyBinding = map[termbox.Key]Action{} - ActionFunc(doBeginningOfLine).register("BeginningOfLine", termbox.KeyCtrlA) - ActionFunc(doBackwardChar).register("BackwardChar", termbox.KeyCtrlB) - ActionFunc(doBackwardWord).register("BackwardWord") - ActionFunc(doCancel).register("Cancel", termbox.KeyCtrlC, termbox.KeyEsc) - ActionFunc(doDeleteAll).register("DeleteAll") - ActionFunc(doDeleteBackwardChar).register( + ActionFunc(doBeginningOfLine).Register("BeginningOfLine", termbox.KeyCtrlA) + ActionFunc(doBackwardChar).Register("BackwardChar", termbox.KeyCtrlB) + ActionFunc(doBackwardWord).Register("BackwardWord") + ActionFunc(doCancel).Register("Cancel", termbox.KeyCtrlC, termbox.KeyEsc) + ActionFunc(doDeleteAll).Register("DeleteAll") + ActionFunc(doDeleteBackwardChar).Register( "DeleteBackwardChar", termbox.KeyBackspace, termbox.KeyBackspace2, ) - ActionFunc(doDeleteBackwardWord).register( + ActionFunc(doDeleteBackwardWord).Register( "DeleteBackwardWord", termbox.KeyCtrlW, ) - ActionFunc(doDeleteForwardChar).register("DeletedForwardChar", termbox.KeyCtrlD) - ActionFunc(doDeleteForwardWord).register("DeleteForwardWord") - ActionFunc(doEndOfFile).register("EndOfFile") - ActionFunc(doEndOfLine).register("EndOfLine", termbox.KeyCtrlE) - ActionFunc(doFinish).register("Finish", termbox.KeyEnter) - ActionFunc(doForwardChar).register("ForwardChar", termbox.KeyCtrlF) - ActionFunc(doForwardWord).register("ForwardWord") - ActionFunc(doKillEndOfLine).register("KillEndOfLine", termbox.KeyCtrlK) - ActionFunc(doKillBeginningOfLine).register("KillBeginningOfLine", termbox.KeyCtrlU) - ActionFunc(doRotateMatcher).register("RotateMatcher", termbox.KeyCtrlR) - ActionFunc(doSelectNext).register( + ActionFunc(doDeleteForwardChar).Register("DeletedForwardChar", termbox.KeyCtrlD) + ActionFunc(doDeleteForwardWord).Register("DeleteForwardWord") + ActionFunc(doEndOfFile).Register("EndOfFile") + ActionFunc(doEndOfLine).Register("EndOfLine", termbox.KeyCtrlE) + ActionFunc(doFinish).Register("Finish", termbox.KeyEnter) + ActionFunc(doForwardChar).Register("ForwardChar", termbox.KeyCtrlF) + ActionFunc(doForwardWord).Register("ForwardWord") + ActionFunc(doKillEndOfLine).Register("KillEndOfLine", termbox.KeyCtrlK) + ActionFunc(doKillBeginningOfLine).Register("KillBeginningOfLine", termbox.KeyCtrlU) + ActionFunc(doRotateMatcher).Register("RotateMatcher", termbox.KeyCtrlR) + ActionFunc(doSelectNext).Register( "SelectNext", termbox.KeyArrowDown, termbox.KeyCtrlN, ) - ActionFunc(doSelectNextPage).register( + ActionFunc(doSelectNextPage).Register( "SelectNextPage", termbox.KeyArrowRight, ) - ActionFunc(doSelectPrevious).register( + ActionFunc(doSelectPrevious).Register( "SelectPrevious", termbox.KeyArrowUp, termbox.KeyCtrlP, ) - ActionFunc(doSelectPreviousPage).register( + ActionFunc(doSelectPreviousPage).Register( "SelectPreviousPage", termbox.KeyArrowLeft, ) - ActionFunc(doToggleSelection).register("ToggleSelection") - ActionFunc(doToggleSelectionAndSelectNext).register( + ActionFunc(doToggleSelection).Register("ToggleSelection") + ActionFunc(doToggleSelectionAndSelectNext).Register( "ToggleSelectionAndSelectNext", termbox.KeyCtrlSpace, ) diff --git a/keymap.go b/keymap.go index 7b4d183..0049ace 100644 --- a/keymap.go +++ b/keymap.go @@ -220,7 +220,7 @@ func (km Keymap) UnmarshalJSON(buf []byte) error { continue } - v, ok := handlers[vs] + v, ok := nameToActions[vs] if !ok { fmt.Fprintf(os.Stderr, "Unknown handler %s", vs) continue