-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from pnx/keybinds
Keybind support.
- Loading branch information
Showing
9 changed files
with
315 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
. "github.com/jorgerojas26/lazysql/commands" | ||
. "github.com/jorgerojas26/lazysql/keymap" | ||
) | ||
|
||
// KeymapSystem is the actual key mapping system. | ||
// | ||
// A map can have several groups. But it always has a "Global" one. | ||
type KeymapSystem struct { | ||
Global Map | ||
Groups map[string]Map | ||
} | ||
|
||
func (c KeymapSystem) Group(name string) Map { | ||
// Global is special. | ||
if name == "global" { | ||
return c.Global | ||
} | ||
|
||
// Lookup the group | ||
if group, ok := c.Groups[name]; ok { | ||
return group | ||
} | ||
|
||
// Did not find any maps. Return a empty one | ||
return Map{} | ||
} | ||
|
||
// Resolve translates a tcell.EventKey into a command based on the mappings in | ||
// the global group | ||
func (c KeymapSystem) Resolve(event *tcell.EventKey) Command { | ||
return c.Global.Resolve(event) | ||
} | ||
|
||
// Define a global KeymapSystem object with default keybinds | ||
var Keymaps KeymapSystem = KeymapSystem{ | ||
Global: Map{ | ||
Bind{Key: Key{Char: 'L'}, Cmd: MoveRight}, | ||
Bind{Key: Key{Char: 'H'}, Cmd: MoveLeft}, | ||
Bind{Key: Key{Code: tcell.KeyCtrlE}, Cmd: SwitchToEditorView}, | ||
Bind{Key: Key{Code: tcell.KeyCtrlS}, Cmd: Save}, | ||
Bind{Key: Key{Char: 'q'}, Cmd: Quit}, | ||
Bind{Key: Key{Code: tcell.KeyBackspace2}, Cmd: SwitchToConnectionsView}, | ||
}, | ||
Groups: map[string]Map{ | ||
"tree": { | ||
Bind{Key: Key{Char: 'g'}, Cmd: GotoTop}, | ||
Bind{Key: Key{Char: 'G'}, Cmd: GotoBottom}, | ||
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: Execute}, | ||
Bind{Key: Key{Char: 'j'}, Cmd: MoveDown}, | ||
Bind{Key: Key{Code: tcell.KeyDown}, Cmd: MoveDown}, | ||
Bind{Key: Key{Char: 'k'}, Cmd: MoveUp}, | ||
Bind{Key: Key{Code: tcell.KeyUp}, Cmd: MoveUp}, | ||
}, | ||
"table": { | ||
Bind{Key: Key{Char: '/'}, Cmd: Search}, | ||
Bind{Key: Key{Char: 'c'}, Cmd: Edit}, | ||
Bind{Key: Key{Char: 'd'}, Cmd: Delete}, | ||
Bind{Key: Key{Char: 'w'}, Cmd: GotoNext}, | ||
Bind{Key: Key{Char: 'b'}, Cmd: GotoPrev}, | ||
Bind{Key: Key{Char: '$'}, Cmd: GotoEnd}, | ||
Bind{Key: Key{Char: '0'}, Cmd: GotoStart}, | ||
Bind{Key: Key{Char: 'y'}, Cmd: Copy}, | ||
// Tabs | ||
Bind{Key: Key{Char: '['}, Cmd: TabPrev}, | ||
Bind{Key: Key{Char: ']'}, Cmd: TabNext}, | ||
Bind{Key: Key{Char: '{'}, Cmd: TabFirst}, | ||
Bind{Key: Key{Char: '}'}, Cmd: TabLast}, | ||
Bind{Key: Key{Char: 'X'}, Cmd: TabClose}, | ||
// Pages | ||
Bind{Key: Key{Char: '>'}, Cmd: PageNext}, | ||
Bind{Key: Key{Char: '<'}, Cmd: PagePrev}, | ||
}, | ||
"editor": { | ||
Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: Execute}, | ||
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: Quit}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package commands | ||
|
||
type Command uint8 | ||
|
||
const ( | ||
Noop Command = iota | ||
|
||
// Views | ||
SwitchToEditorView | ||
SwitchToConnectionsView | ||
|
||
// Movement: Basic | ||
MoveUp | ||
MoveDown | ||
MoveLeft | ||
MoveRight | ||
// Movement: Jumps | ||
GotoNext | ||
GotoPrev | ||
GotoStart | ||
GotoEnd | ||
GotoTop | ||
GotoBottom | ||
|
||
// Movement: Page | ||
PageNext | ||
PagePrev | ||
|
||
// Tabs | ||
TabNext | ||
TabPrev | ||
TabFirst | ||
TabLast | ||
TabClose | ||
|
||
// Operations | ||
Copy | ||
Edit | ||
Save | ||
Delete | ||
Search | ||
Quit | ||
Execute | ||
) | ||
|
||
func (c Command) String() string { | ||
switch c { | ||
case Noop: | ||
return "Noop" | ||
// Views | ||
case SwitchToEditorView: | ||
return "SwitchToEditorView" | ||
case SwitchToConnectionsView: | ||
return "SwitchToConnectionsView" | ||
|
||
// Movement: Basic | ||
case MoveUp: | ||
return "MoveUp" | ||
case MoveDown: | ||
return "MoveDown" | ||
case MoveLeft: | ||
return "MoveRight" | ||
case MoveRight: | ||
return "MoveRight" | ||
// Movement: Jumps | ||
case GotoNext: | ||
return "GotoNext" | ||
case GotoPrev: | ||
return "GotoPrev" | ||
case GotoStart: | ||
return "GotoStart" | ||
case GotoEnd: | ||
return "GotoEnd" | ||
case GotoTop: | ||
return "GotoTop" | ||
case GotoBottom: | ||
return "GotoBottom" | ||
|
||
// Movement: Page | ||
case PageNext: | ||
return "PageNext" | ||
case PagePrev: | ||
return "PagePrev" | ||
|
||
// Tabs | ||
case TabNext: | ||
return "TabNext" | ||
case TabPrev: | ||
return "TabPrev" | ||
case TabFirst: | ||
return "TabFirst" | ||
case TabLast: | ||
return "TabLast" | ||
case TabClose: | ||
return "TabClose" | ||
|
||
// Operations | ||
case Copy: | ||
return "Copy" | ||
case Edit: | ||
return "Edit" | ||
case Save: | ||
return "Save" | ||
case Delete: | ||
return "Delete" | ||
case Search: | ||
return "Search" | ||
case Quit: | ||
return "Quit" | ||
case Execute: | ||
return "Execute" | ||
} | ||
return "Unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.