more keybindings

This commit is contained in:
Jesse Duffield 2019-05-18 22:34:28 +10:00
parent 52ec56289a
commit 4a76f7b65e
4 changed files with 66 additions and 2 deletions

View file

@ -49,7 +49,7 @@ func (c *Container) GetColor() color.Attribute {
// MustStopContainer tells us that we must stop the container before removing it // MustStopContainer tells us that we must stop the container before removing it
const MustStopContainer = iota const MustStopContainer = iota
// Remove removes a container // Remove removes the container
func (c *Container) Remove(options types.ContainerRemoveOptions) error { func (c *Container) Remove(options types.ContainerRemoveOptions) error {
if err := c.Client.ContainerRemove(context.Background(), c.ID, options); err != nil { 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") { 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 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)
}

View file

@ -202,6 +202,9 @@ func (gui *Gui) refreshContainers() error {
if len(gui.State.Containers) > 0 && gui.State.Panels.Containers.SelectedLine == -1 { if len(gui.State.Containers) > 0 && gui.State.Panels.Containers.SelectedLine == -1 {
gui.State.Panels.Containers.SelectedLine = 0 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 { 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) 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()
})
}

View file

@ -148,12 +148,25 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Key: ']', Key: ']',
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: gui.handleContainersNextContext, Handler: gui.handleContainersNextContext,
}, { },
{
ViewName: "containers", ViewName: "containers",
Key: 'd', Key: 'd',
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: gui.handleContainersRemoveMenu, 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 // TODO: add more views here

View file

@ -778,5 +778,13 @@ func addEnglish(i18nObject *i18n.Bundle) error {
ID: "Confirm", ID: "Confirm",
Other: "Confirm", Other: "Confirm",
}, },
&i18n.Message{
ID: "StopContainer",
Other: "Are you sure you want to stop this container?",
},
&i18n.Message{
ID: "RestartingStatus",
Other: "restarting",
},
) )
} }