summaryrefslogtreecommitdiff
path: root/pkg/gui/gui-navigate.go
blob: 5565d1335c5241f1d659f928f9fa8623e70519d2 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gui

import (
	log "github.com/sirupsen/logrus"
	"github.com/jroimartin/gocui"
)

var cyclableViews = []string{mainViewFeature.Name,
	remoteViewFeature.Name,
	remoteBranchViewFeature.Name,
	branchViewFeature.Name,
	commitViewFeature.Name}

func (gui *Gui) nextView(g *gocui.Gui, v *gocui.View) error {
	var focusedViewName string
	if v == nil || v.Name() == cyclableViews[len(cyclableViews)-1] {
		focusedViewName = cyclableViews[0]
	} else {
		for i := range cyclableViews {
			if v.Name() == cyclableViews[i] {
				focusedViewName = cyclableViews[i+1]
				break
			}
			if i == len(cyclableViews)-1 {
				return nil
			}
		}
	}
	if _, err := g.SetCurrentView(focusedViewName); err != nil {
			log.Warn("Loading view cannot be focused.")
			return nil
	}
	return nil
}

func (gui *Gui) previousView(g *gocui.Gui, v *gocui.View) error {
	var focusedViewName string
	if v == nil || v.Name() == cyclableViews[0] {
		focusedViewName = cyclableViews[len(cyclableViews)-1]
	} else {
		for i := range cyclableViews {
			if v.Name() == cyclableViews[i] {
				focusedViewName = cyclableViews[i-1] // TODO: make this work properly
				break
			}
			if i == len(cyclableViews)-1 {
				return nil
			}
		}
	}
	if _, err := g.SetCurrentView(focusedViewName); err != nil {
			log.Warn("Loading view cannot be focused.")
			return nil
	}
	return nil
}