From 4a76f7b65e48107c3f6e0821e76f4c1757bfee87 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 18 May 2019 22:34:28 +1000 Subject: [PATCH] more keybindings --- pkg/commands/container.go | 12 +++++++++++- pkg/gui/containers_panel.go | 33 +++++++++++++++++++++++++++++++++ pkg/gui/keybindings.go | 15 ++++++++++++++- pkg/i18n/english.go | 8 ++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pkg/commands/container.go b/pkg/commands/container.go index 480d65ec..f3f300e5 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -49,7 +49,7 @@ func (c *Container) GetColor() color.Attribute { // MustStopContainer tells us that we must stop the container before removing it const MustStopContainer = iota -// Remove removes a container +// Remove removes the container func (c *Container) Remove(options types.ContainerRemoveOptions) error { if err := c.Client.ContainerRemove(context.Background(), c.ID, options); err != nil { if strings.Contains(err.Error(), "Stop the container before attempting removal or force remove") { @@ -64,3 +64,13 @@ func (c *Container) Remove(options types.ContainerRemoveOptions) error { return nil } + +// Stop stops the container +func (c *Container) Stop() error { + return c.Client.ContainerStop(context.Background(), c.ID, nil) +} + +// Restart restarts the container +func (c *Container) Restart() error { + return c.Client.ContainerRestart(context.Background(), c.ID, nil) +} diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 2a66a4cc..a8638ac7 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -202,6 +202,9 @@ func (gui *Gui) refreshContainers() error { if len(gui.State.Containers) > 0 && gui.State.Panels.Containers.SelectedLine == -1 { gui.State.Panels.Containers.SelectedLine = 0 } + if len(gui.State.Containers)-1 < gui.State.Panels.Containers.SelectedLine { + gui.State.Panels.Containers.SelectedLine = len(gui.State.Containers) - 1 + } gui.g.Update(func(g *gocui.Gui) error { @@ -351,3 +354,33 @@ func (gui *Gui) handleContainersRemoveMenu(g *gocui.Gui, v *gocui.View) error { return gui.createMenu("", options, len(options), handleMenuPress) } + +func (gui *Gui) handleContainerStop(g *gocui.Gui, v *gocui.View) error { + container, err := gui.getSelectedContainer(g) + if err != nil { + return nil + } + + return gui.createConfirmationPanel(gui.g, v, gui.Tr.SLocalize("Confirm"), gui.Tr.SLocalize("StopContainer"), func(g *gocui.Gui, v *gocui.View) error { + if err := container.Stop(); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + return gui.refreshContainers() + }, nil) +} + +func (gui *Gui) handleContainerRestart(g *gocui.Gui, v *gocui.View) error { + container, err := gui.getSelectedContainer(g) + if err != nil { + return nil + } + + return gui.WithWaitingStatus(gui.Tr.SLocalize("RestartingStatus"), func() error { + if err := container.Restart(); err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + return gui.refreshContainers() + }) +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index aff9bc8f..d8ccde71 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -148,12 +148,25 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Key: ']', Modifier: gocui.ModNone, Handler: gui.handleContainersNextContext, - }, { + }, + { ViewName: "containers", Key: 'd', Modifier: gocui.ModNone, Handler: gui.handleContainersRemoveMenu, }, + { + ViewName: "containers", + Key: 's', + Modifier: gocui.ModNone, + Handler: gui.handleContainerStop, + }, + { + ViewName: "containers", + Key: 'r', + Modifier: gocui.ModNone, + Handler: gui.handleContainerRestart, + }, } // TODO: add more views here diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index d0f36b49..c1e8d6d8 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -778,5 +778,13 @@ func addEnglish(i18nObject *i18n.Bundle) error { ID: "Confirm", Other: "Confirm", }, + &i18n.Message{ + ID: "StopContainer", + Other: "Are you sure you want to stop this container?", + }, + &i18n.Message{ + ID: "RestartingStatus", + Other: "restarting", + }, ) }