Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PreviousViews stack #159

Merged
merged 6 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/fatih/color v1.7.0
github.com/go-errors/errors v1.0.1
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/google/go-cmp v0.3.0 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/imdario/mergo v0.3.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/confirmation_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt
confirmationView.FgColor = gocui.ColorWhite
}
gui.g.Update(func(g *gocui.Gui) error {
return gui.switchFocus(gui.g, currentView, confirmationView)
return gui.switchFocus(gui.g, currentView, confirmationView, false)
})
return confirmationView, nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gui

import (
"github.com/golang-collections/collections/stack"
"strings"
"sync"

Expand Down Expand Up @@ -117,7 +118,7 @@ type panelStates struct {

type guiState struct {
MenuItemCount int // can't store the actual list because it's of interface{} type
PreviousView string
PreviousViews *stack.Stack
Platform commands.Platform
Panels *panelStates
SubProcessOutput string
Expand All @@ -144,7 +145,8 @@ func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand
},
Project: &projectState{ContextIndex: 0},
},
SessionIndex: 0,
SessionIndex: 0,
PreviousViews: stack.New(),
}

cyclableViews := []string{"project", "containers", "images", "volumes"}
Expand Down
8 changes: 4 additions & 4 deletions pkg/gui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}

currView := gui.g.CurrentView()
currentCyclebleView := gui.State.PreviousView
currentCyclebleView := gui.peekPreviousView()
if currView != nil {
viewName := currView.Name()
usePreviouseView := true
Expand All @@ -106,7 +106,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}
if usePreviouseView {
currentCyclebleView = gui.State.PreviousView
currentCyclebleView = gui.peekPreviousView()
}
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}

if gui.g.CurrentView() == nil {
v, err := gui.g.View(gui.State.PreviousView)
v, err := gui.g.View(gui.peekPreviousView())
if err != nil {
viewName := gui.initiallyFocusedViewName()
v, err = gui.g.View(viewName)
Expand All @@ -282,7 +282,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}

if err := gui.switchFocus(gui.g, nil, v); err != nil {
if err := gui.switchFocus(gui.g, nil, v, false); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/main_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (gui *Gui) handleEnterMain(g *gocui.Gui, v *gocui.View) error {
mainView := gui.getMainView()
mainView.ParentView = v

return gui.switchFocus(gui.g, v, mainView)
return gui.switchFocus(gui.g, v, mainView, false)
}

func (gui *Gui) handleExitMain(g *gocui.Gui, v *gocui.View) error {
Expand All @@ -122,5 +122,5 @@ func (gui *Gui) handleMainClick(g *gocui.Gui, v *gocui.View) error {
v.ParentView = currentView
}

return gui.switchFocus(gui.g, currentView, v)
return gui.switchFocus(gui.g, currentView, v, false)
}
2 changes: 1 addition & 1 deletion pkg/gui/menu_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handl
}
}
currentView := gui.g.CurrentView()
return gui.switchFocus(gui.g, currentView, menuView)
return gui.switchFocus(gui.g, currentView, menuView, false)
})
return nil
}
4 changes: 3 additions & 1 deletion pkg/gui/subprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ func (gui *Gui) RunWithSubprocesses() error {
break
} else if err == gui.Errors.ErrSubProcess {
// preparing the state for when we return
gui.State.PreviousView = gui.currentViewName()
gui.pushPreviousView(gui.currentViewName())
// giving goEvery goroutines time to finish
gui.State.SessionIndex++

if err := gui.runCommand(); err != nil {
return err
}

// pop here so we don't stack up view names
gui.popPreviousView()
// ensuring we render e.g. the logs of the currently selected item upon return
gui.State.Panels.Main.ObjectKey = ""
} else {
Expand Down
37 changes: 30 additions & 7 deletions pkg/gui/view_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (gui *Gui) nextView(g *gocui.Gui, v *gocui.View) error {
panic(err)
}
gui.resetMainView()
return gui.switchFocus(g, v, focusedView)
gui.popPreviousView()
return gui.switchFocus(g, v, focusedView, false)
}

func (gui *Gui) previousView(g *gocui.Gui, v *gocui.View) error {
Expand All @@ -66,7 +67,8 @@ func (gui *Gui) previousView(g *gocui.Gui, v *gocui.View) error {
panic(err)
}
gui.resetMainView()
return gui.switchFocus(g, v, focusedView)
gui.popPreviousView()
return gui.switchFocus(g, v, focusedView, false)
}

func (gui *Gui) resetMainView() {
Expand Down Expand Up @@ -102,25 +104,46 @@ func (gui *Gui) newLineFocused(v *gocui.View) error {
}
}

func (gui *Gui) popPreviousView() string {
if gui.State.PreviousViews.Len() > 0 {
return gui.State.PreviousViews.Pop().(string)
}

return ""
}

func (gui *Gui) peekPreviousView() string {
if gui.State.PreviousViews.Len() > 0 {
return gui.State.PreviousViews.Peek().(string)
}

return ""
}

func (gui *Gui) pushPreviousView(name string) {
gui.State.PreviousViews.Push(name)
}

func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
previousView, err := g.View(gui.State.PreviousView)
previousViewName := gui.popPreviousView()
previousView, err := g.View(previousViewName)
if err != nil {
// always fall back to services view if there's no 'previous' view stored
previousView, err = g.View(gui.initiallyFocusedViewName())
if err != nil {
gui.Log.Error(err)
}
}
return gui.switchFocus(g, v, previousView)
return gui.switchFocus(g, v, previousView, true)
}

// pass in oldView = nil if you don't want to be able to return to your old view
// TODO: move some of this logic into our onFocusLost and onFocus hooks
func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View, returning bool) error {
// we assume we'll never want to return focus to a popup panel i.e.
// we should never stack popup panels
if oldView != nil && !gui.isPopupPanel(oldView.Name()) {
gui.State.PreviousView = oldView.Name()
if oldView != nil && !gui.isPopupPanel(oldView.Name()) && !returning {
gui.pushPreviousView(oldView.Name())
}

gui.Log.Info("setting highlight to true for view " + newView.Name())
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/golang-collections/collections/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/golang-collections/collections/stack/stack.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/fatih/color
github.com/go-errors/errors
# github.com/gogo/protobuf v1.2.1
github.com/gogo/protobuf/proto
# github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/golang-collections/collections/stack
# github.com/imdario/mergo v0.3.7
github.com/imdario/mergo
# github.com/integrii/flaggy v0.0.0-20190517180110-07ea7eb77404
Expand Down