aboutsummaryrefslogtreecommitdiffstats
path: root/termui/termui.go
blob: a82484420f077db4135ea6dd7fe44172137c7db5 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package termui

import (
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/repository"
	"github.com/jroimartin/gocui"
)

type termUI struct {
	cache    cache.RepoCacher
	bugTable *bugTable
}

var ui *termUI

func Run(repo repository.Repo) error {
	c := cache.NewRepoCache(repo)

	ui = &termUI{
		cache:    c,
		bugTable: newBugTable(c),
	}

	g, err := gocui.NewGui(gocui.OutputNormal)

	if err != nil {
		return err
	}

	defer g.Close()

	g.SetManagerFunc(layout)

	err = keybindings(g)

	if err != nil {
		return err
	}

	err = g.MainLoop()

	if err != nil && err != gocui.ErrQuit {
		return err
	}

	return nil
}

func layout(g *gocui.Gui) error {
	//maxX, maxY := g.Size()

	ui.bugTable.layout(g)

	v, err := g.View("table")
	if err != nil {
		return err
	}

	cursorClamp(v)

	return nil
}

func keybindings(g *gocui.Gui) error {
	if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
		return err
	}
	if err := g.SetKeybinding("table", 'j', gocui.ModNone, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("table", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("table", 'k', gocui.ModNone, cursorUp); err != nil {
		return err
	}
	if err := g.SetKeybinding("table", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
		return err
	}

	//err = g.SetKeybinding("table", 'h', gocui.ModNone, popTable)
	//err = g.SetKeybinding("table", gocui.KeyArrowLeft, gocui.ModNone, popTable)
	//err = g.SetKeybinding("table", 'l', gocui.ModNone, pushTable)
	//err = g.SetKeybinding("table", gocui.KeyArrowRight, gocui.ModNone, pushTable)
	//err = g.SetKeybinding("table", 'p', gocui.ModNone, playSelected)
	//err = g.SetKeybinding("table", gocui.KeyEnter, gocui.ModNone, playSelectedAndExit)
	//err = g.SetKeybinding("table", 'm', gocui.ModNone, loadNextRecords)

	return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrQuit
}

func cursorDown(g *gocui.Gui, v *gocui.View) error {
	_, y := v.Cursor()
	y = minInt(y+1, ui.bugTable.getTableLength()-1)

	err := v.SetCursor(0, y)
	if err != nil {
		return err
	}

	return nil
}

func cursorUp(g *gocui.Gui, v *gocui.View) error {
	_, y := v.Cursor()
	y = maxInt(y-1, 0)

	err := v.SetCursor(0, y)
	if err != nil {
		return err
	}

	return nil
}

func cursorClamp(v *gocui.View) error {
	_, y := v.Cursor()

	y = minInt(y, ui.bugTable.getTableLength()-1)
	y = maxInt(y, 0)

	err := v.SetCursor(0, y)
	if err != nil {
		return err
	}

	return nil
}