From d62785cecdcb66469b626f9c3226610c37492b1e Mon Sep 17 00:00:00 2001 From: Glenn Vriesman Date: Sun, 4 Aug 2019 23:29:13 +0200 Subject: [PATCH] Config: Added a lot of default keybindings * Cleaned up some code * Added missing configs * Added documentation * Implemented config referencing to keybindings.go Signed-off-by: Glenn Vriesman --- pkg/config/app_config.go | 125 ++++++++++++++----------- pkg/gui/keybindings.go | 194 +++++++++++++-------------------------- 2 files changed, 136 insertions(+), 183 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index e75305c5..acfdb5fd 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -269,6 +269,12 @@ type KeybindingsConfig struct { // ScrollDownMain represents all the keys that will scroll down the main view ScrollDownMain []string `yaml:"scrollDownMain,omitempty"` + // ScrollLeftMain represents all the keys that will scroll the main view to the left + ScrollLeftMain []string `yaml:"scrollLeftMain,omitempty"` + + // ScrollRightMain represents all the keys that will scroll the main view to the right + ScrollRightMain []string `yaml:"scrollRightMain,omitempty"` + // AutoScrollMain represents all the keys that will toggle autoscroll AutoScrollMain []string `yaml:"autoScrollMain,omitempty"` @@ -309,6 +315,7 @@ type KeybindingsConfig struct { Main Main `yaml:"main,omitempty"` } +// Project represents all the keys that are applied to the project view type Project struct { // EditConfig represents all the keys that will open the edit view EditConfig []string `yaml:"editConfig,omitempty"` @@ -337,7 +344,6 @@ type Information struct { } type Containers struct { - // Remove represents all the keys that will remove the selected container Remove []string `yaml:"remove,omitempty"` @@ -353,14 +359,14 @@ type Containers struct { // Attach represents all the keys that will attach the selected container Attach []string `yaml:"attach,omitempty"` - // Prune represents all the keys that will prune the containers - Prune []string `yaml:"prune,omitempty"` - // ViewLogs represents all the keys that will show you the logs ViewLogs []string `yaml:"viewLogs,omitempty"` // RunCustomCommand represents all the keys that will run a custom command RunCustomCommand []string `yaml:"runCustomCommand,omitempty"` + + // RunBulkCommands represents all the keys that will show you the bulk commands + RunBulkCommands []string `yaml:"runBulkCommands,omitempty"` } type Services struct { @@ -384,24 +390,40 @@ type Services struct { // RunCustomCommand represents all the keys that will run a custom command RunCustomCommand []string `yaml:"runCustomCommand,omitempty"` + + // RunBulkCommands represents all the keys that will show you the bulk commands + RunBulkCommands []string `yaml:"runBulkCommands,omitempty"` + + // ScrollUpMain represents all the keys that will scroll up the main view + ScrollUpMain []string + + // RunBulkCommands represents all the keys that will scroll down the main view + ScrollDownMain []string } type Images struct { // Remove represents all the keys that will remove the selected image Remove []string `yaml:"remove,omitempty"` - // Prune represents all the keys that will prune the images - Prune []string `yaml:"prune,omitempty"` + // RunCustomCommand represents all the keys that will run a custom command + RunCustomCommand []string `yaml:"runCustomCommand,omitempty"` + + // RunBulkCommand represents all the + RunBulkCommands []string `yaml:"runBulkCommands,omitempty"` } type Volumes struct { // Remove represents all the keys that will remove the selected volume Remove []string `yaml:"remove,omitempty"` - // Prune represents all the keys that will prune the volumes - Prune []string `yaml:"prune,omitempty"` + // RunCustomCommand represents all the keys that will run a custom command + RunCustomCommand []string `yaml:"runCustomCommand,omitempty"` + + // RunBulkCommand represents all the + RunBulkCommands []string `yaml:"runBulkCommands,omitempty"` } +// Main represents all the keys that are applied to the main view type Main struct { // Return represents all the keys that will trigger a return Return []string `yaml:"return,omitempty"` @@ -411,12 +433,6 @@ type Main struct { // ScrollLeft represents all the keys that will scroll the main view to the right ScrollRight []string `yaml:"scrollRight,omitempty"` - - // ScrollLeft represents all the keys that will scroll the main view down - ScrolDown []string `yaml:"scrolDown,omitempty"` - - // ScrollLeft represents all the keys that will scroll the main view up - ScrollUp []string `yaml:"scrollUp,omitempty"` } // CustomCommands contains the custom commands that you might want to use on any @@ -582,60 +598,63 @@ func GetDefaultConfig() UserConfig { }, }, Keybindings: KeybindingsConfig{ - Quit: []string{}, - ScrollUpMain: []string{}, - ScrollDownMain: []string{}, - AutoScrollMain: []string{}, - ShowOptionsMenu: []string{}, - CustomCommand: []string{}, - PreviousContext: []string{}, - NextContext: []string{}, + Quit: []string{"q", "ctrl+c", "escape"}, + ScrollUpMain: []string{"pageup", "ctrl+u", "K"}, + ScrollDownMain: []string{"pagedown", "ctrl+d", "J"}, + ScrollLeftMain: []string{"H"}, + ScrollRightMain: []string{"L"}, + AutoScrollMain: []string{"end"}, + ShowOptionsMenu: []string{"x", "?"}, + CustomCommand: []string{"X"}, + PreviousContext: []string{"["}, + NextContext: []string{"]"}, Project: Project{ - EditConfig: []string{}, - OpenConfig: []string{}, - Click: []string{}, - ViewLogs: []string{}, - Select: []string{}, + EditConfig: []string{"e"}, + OpenConfig: []string{"o"}, + Click: []string{"MouseLeft"}, + ViewLogs: []string{"m"}, + Select: []string{"MouseLeft"}, }, Menu: Menu{ - Close: []string{}, + Close: []string{"Escape", "q"}, }, Information: Information{ - Donate: []string{}, + Donate: []string{"MouseLeft"}, }, Containers: Containers{ - Remove: []string{}, - HideStopped: []string{}, - Stop: []string{}, - Restart: []string{}, - Attach: []string{}, - Prune: []string{}, - ViewLogs: []string{}, - RunCustomCommand: []string{}, + Remove: []string{"d"}, + HideStopped: []string{"e"}, + Stop: []string{"s"}, + Restart: []string{"r"}, + Attach: []string{"a"}, + ViewLogs: []string{"m"}, + RunCustomCommand: []string{"c"}, + RunBulkCommands: []string{"b"}, }, Services: Services{ - Remove: []string{}, - Stop: []string{}, - Restart: []string{}, - Attach: []string{}, - ViewLogs: []string{}, - ViewRestartOptions: []string{}, - RunCustomCommand: []string{}, + Remove: []string{"d"}, + Stop: []string{"s"}, + Restart: []string{"r"}, + Attach: []string{"a"}, + ViewLogs: []string{"m"}, + ViewRestartOptions: []string{"R"}, + RunCustomCommand: []string{"c"}, + RunBulkCommands: []string{"b"}, }, Images: Images{ - Remove: []string{}, - Prune: []string{}, + Remove: []string{"d"}, + RunCustomCommand: []string{"c"}, + RunBulkCommands: []string{"b"}, }, Volumes: Volumes{ - Remove: []string{}, - Prune: []string{}, + Remove: []string{"d"}, + RunCustomCommand: []string{"c"}, + RunBulkCommands: []string{"b"}, }, Main: Main{ - Return: []string{}, - ScrollLeft: []string{}, - ScrollRight: []string{}, - ScrolDown: []string{}, - ScrollUp: []string{}, + Return: []string{"Escape"}, + ScrollLeft: []string{"left", "h"}, + ScrollRight: []string{"right", "l"}, }, }, } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 24fe83d8..6135efe3 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -10,7 +10,7 @@ import ( type Binding struct { ViewName string Handler func(*gocui.Gui, *gocui.View) error - Key interface{} // FIXME: find out how to get `gocui.Key | rune` + Keys []gocui.Key Modifier gocui.Modifier Description string } @@ -61,411 +61,337 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { bindings := []*Binding{ { ViewName: "", - Key: 'q', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Quit), Modifier: gocui.ModNone, Handler: gui.quit, }, { ViewName: "", - Key: gocui.KeyCtrlC, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.ScrollUpMain), Modifier: gocui.ModNone, Handler: gui.quit, }, { ViewName: "", - Key: gocui.KeyEsc, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.ScrollDownMain), Modifier: gocui.ModNone, Handler: gui.quit, }, { ViewName: "", - Key: gocui.KeyPgup, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.ScrollLeftMain), Modifier: gocui.ModNone, - Handler: gui.scrollUpMain, + Handler: gui.scrollLeftMain, }, { ViewName: "", - Key: gocui.KeyPgdn, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.ScrollRightMain), Modifier: gocui.ModNone, - Handler: gui.scrollDownMain, + Handler: gui.scrollRightMain, }, { ViewName: "", - Key: gocui.KeyCtrlU, - Modifier: gocui.ModNone, - Handler: gui.scrollUpMain, - }, - { - ViewName: "", - Key: gocui.KeyCtrlD, - Modifier: gocui.ModNone, - Handler: gui.scrollDownMain, - }, - { - ViewName: "", - Key: gocui.KeyEnd, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.AutoScrollMain), Modifier: gocui.ModNone, Handler: gui.autoScrollMain, }, { ViewName: "", - Key: 'x', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.ShowOptionsMenu), Modifier: gocui.ModNone, Handler: gui.handleCreateOptionsMenu, }, { ViewName: "", - Key: '?', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.CustomCommand), Modifier: gocui.ModNone, - Handler: gui.handleCreateOptionsMenu, - }, - { - ViewName: "", - Key: 'X', - Modifier: gocui.ModNone, - Handler: gui.handleCustomCommand, + Handler: gui.handleCustomCommand, // Might overlap with the other run custom command configs }, { ViewName: "project", - Key: 'e', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Project.EditConfig), Modifier: gocui.ModNone, Handler: gui.handleEditConfig, Description: gui.Tr.EditConfig, }, { ViewName: "project", - Key: 'o', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Project.OpenConfig), Modifier: gocui.ModNone, Handler: gui.handleOpenConfig, Description: gui.Tr.OpenConfig, }, { ViewName: "project", - Key: '[', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.PreviousContext), Modifier: gocui.ModNone, Handler: gui.handleProjectPrevContext, Description: gui.Tr.PreviousContext, }, { ViewName: "project", - Key: ']', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.NextContext), Modifier: gocui.ModNone, Handler: gui.handleProjectNextContext, Description: gui.Tr.NextContext, }, { ViewName: "project", - Key: gocui.MouseLeft, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Project.Click), Modifier: gocui.ModNone, - Handler: gui.handleProjectClick, + Handler: gui.handleProjectClick, // Possbile dub with select }, { ViewName: "project", - Key: 'm', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Project.ViewLogs), Modifier: gocui.ModNone, Handler: gui.handleViewAllLogs, Description: gui.Tr.ViewLogs, }, { ViewName: "project", - Key: gocui.MouseLeft, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Project.Select), Modifier: gocui.ModNone, - Handler: gui.handleProjectSelect, + Handler: gui.handleProjectSelect, // Possible dub with click }, { ViewName: "menu", - Key: gocui.KeyEsc, - Modifier: gocui.ModNone, - Handler: gui.handleMenuClose, - }, - { - ViewName: "menu", - Key: 'q', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Menu.Close), Modifier: gocui.ModNone, Handler: gui.handleMenuClose, }, { ViewName: "information", - Key: gocui.MouseLeft, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Information.Donate), Modifier: gocui.ModNone, Handler: gui.handleDonate, }, { ViewName: "containers", - Key: '[', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.PreviousContext), Modifier: gocui.ModNone, Handler: gui.handleContainersPrevContext, Description: gui.Tr.PreviousContext, }, { ViewName: "containers", - Key: ']', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.NextContext), Modifier: gocui.ModNone, Handler: gui.handleContainersNextContext, Description: gui.Tr.NextContext, }, { ViewName: "containers", - Key: 'd', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.Remove), Modifier: gocui.ModNone, Handler: gui.handleContainersRemoveMenu, Description: gui.Tr.Remove, }, { ViewName: "containers", - Key: 'e', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.HideStopped), Modifier: gocui.ModNone, Handler: gui.handleHideStoppedContainers, Description: gui.Tr.HideStopped, }, { ViewName: "containers", - Key: 's', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.Stop), Modifier: gocui.ModNone, Handler: gui.handleContainerStop, Description: gui.Tr.Stop, }, { ViewName: "containers", - Key: 'r', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.Restart), Modifier: gocui.ModNone, Handler: gui.handleContainerRestart, Description: gui.Tr.Restart, }, { ViewName: "containers", - Key: 'a', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.Attach), Modifier: gocui.ModNone, Handler: gui.handleContainerAttach, Description: gui.Tr.Attach, }, { ViewName: "containers", - Key: 'm', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.ViewLogs), Modifier: gocui.ModNone, Handler: gui.handleContainerViewLogs, Description: gui.Tr.ViewLogs, }, { ViewName: "containers", - Key: 'c', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.RunCustomCommand), Modifier: gocui.ModNone, Handler: gui.handleContainersCustomCommand, Description: gui.Tr.RunCustomCommand, }, { ViewName: "containers", - Key: 'b', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Containers.RunBulkCommands), Modifier: gocui.ModNone, Handler: gui.handleContainersBulkCommand, Description: gui.Tr.ViewBulkCommands, }, { ViewName: "services", - Key: 'd', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.Remove), Modifier: gocui.ModNone, Handler: gui.handleServiceRemoveMenu, Description: gui.Tr.RemoveService, }, { ViewName: "services", - Key: 's', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.Stop), Modifier: gocui.ModNone, Handler: gui.handleServiceStop, Description: gui.Tr.Stop, }, { ViewName: "services", - Key: 'r', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.Restart), Modifier: gocui.ModNone, Handler: gui.handleServiceRestart, Description: gui.Tr.Restart, }, { ViewName: "services", - Key: 'a', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.Attach), Modifier: gocui.ModNone, Handler: gui.handleServiceAttach, Description: gui.Tr.Attach, }, { ViewName: "services", - Key: 'm', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.ViewLogs), Modifier: gocui.ModNone, Handler: gui.handleServiceViewLogs, Description: gui.Tr.ViewLogs, }, { ViewName: "services", - Key: '[', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.PreviousContext), Modifier: gocui.ModNone, Handler: gui.handleServicesPrevContext, Description: gui.Tr.PreviousContext, }, { ViewName: "services", - Key: ']', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.NextContext), Modifier: gocui.ModNone, Handler: gui.handleServicesNextContext, Description: gui.Tr.NextContext, }, { ViewName: "services", - Key: 'R', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.ViewRestartOptions), Modifier: gocui.ModNone, Handler: gui.handleServiceRestartMenu, Description: gui.Tr.ViewRestartOptions, }, { ViewName: "services", - Key: 'c', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.RunCustomCommand), Modifier: gocui.ModNone, Handler: gui.handleServicesCustomCommand, Description: gui.Tr.RunCustomCommand, }, { ViewName: "services", - Key: 'b', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.Services.RunBulkCommands), Modifier: gocui.ModNone, Handler: gui.handleServicesBulkCommand, Description: gui.Tr.ViewBulkCommands, }, { ViewName: "images", - Key: '[', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.PreviousContext), Modifier: gocui.ModNone, Handler: gui.handleImagesPrevContext, Description: gui.Tr.PreviousContext, }, { ViewName: "images", - Key: ']', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.NextContext), Modifier: gocui.ModNone, Handler: gui.handleImagesNextContext, Description: gui.Tr.NextContext, }, { ViewName: "images", - Key: 'c', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Images.RunCustomCommand), Modifier: gocui.ModNone, Handler: gui.handleImagesCustomCommand, Description: gui.Tr.RunCustomCommand, }, { ViewName: "images", - Key: 'd', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Images.Remove), Modifier: gocui.ModNone, Handler: gui.handleImagesRemoveMenu, Description: gui.Tr.RemoveImage, }, { ViewName: "images", - Key: 'b', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Images.RunBulkCommands), Modifier: gocui.ModNone, Handler: gui.handleImagesBulkCommand, Description: gui.Tr.ViewBulkCommands, }, - { - ViewName: "images", - Key: 'c', - Modifier: gocui.ModNone, - Handler: gui.handleImagesCustomCommand, - Description: gui.Tr.RunCustomCommand, - }, { ViewName: "volumes", - Key: '[', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.PreviousContext), Modifier: gocui.ModNone, Handler: gui.handleVolumesPrevContext, Description: gui.Tr.PreviousContext, }, { ViewName: "volumes", - Key: ']', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybinding.NextContext), Modifier: gocui.ModNone, Handler: gui.handleVolumesNextContext, Description: gui.Tr.NextContext, }, { ViewName: "volumes", - Key: 'c', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Volumes.RunCustomCommand), Modifier: gocui.ModNone, Handler: gui.handleVolumesCustomCommand, Description: gui.Tr.RunCustomCommand, }, { ViewName: "volumes", - Key: 'd', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Volumes.Remove), Modifier: gocui.ModNone, Handler: gui.handleVolumesRemoveMenu, Description: gui.Tr.RemoveVolume, }, { ViewName: "volumes", - Key: 'b', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Volumes.RunBulkCommands), Modifier: gocui.ModNone, Handler: gui.handleVolumesBulkCommand, Description: gui.Tr.ViewBulkCommands, }, - { - ViewName: "volumes", - Key: 'c', - Modifier: gocui.ModNone, - Handler: gui.handleVolumesCustomCommand, - Description: gui.Tr.RunCustomCommand, - }, { ViewName: "main", - Key: gocui.KeyEsc, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Main.Return), Modifier: gocui.ModNone, Handler: gui.handleExitMain, Description: gui.Tr.Return, }, { ViewName: "main", - Key: gocui.KeyArrowLeft, + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Main.ScrollLeft), Modifier: gocui.ModNone, Handler: gui.scrollLeftMain, }, { ViewName: "main", - Key: gocui.KeyArrowRight, - Modifier: gocui.ModNone, - Handler: gui.scrollRightMain, - }, - { - ViewName: "main", - Key: 'h', - Modifier: gocui.ModNone, - Handler: gui.scrollLeftMain, - }, - { - ViewName: "main", - Key: 'l', - Modifier: gocui.ModNone, - Handler: gui.scrollRightMain, - }, - { - ViewName: "", - Key: 'J', - Modifier: gocui.ModNone, - Handler: gui.scrollDownMain, - }, - { - ViewName: "", - Key: 'K', - Modifier: gocui.ModNone, - Handler: gui.scrollUpMain, - }, - { - ViewName: "", - Key: 'H', - Modifier: gocui.ModNone, - Handler: gui.scrollLeftMain, - }, - { - ViewName: "", - Key: 'L', + Keys: parsedKeybindings(gui.Config.UserConfig.Keybindings.Main.ScrollRight), Modifier: gocui.ModNone, Handler: gui.scrollRightMain, }, @@ -534,3 +460,11 @@ func (gui *Gui) keybindings(g *gocui.Gui) error { return nil } + +func parsedKeybindings(binds []string) []gocui.Key { + var keys []gocui.Key + for _, bind := range binds { + keys = append(keys, keybindings.MustParse(bind)) // MustParse panics on failure + } + return keys +}