move maxrows method out of grid

This commit is contained in:
Bradley Cicenas
2017-02-25 07:16:00 +00:00
parent c3bb7c6b96
commit 381f1da602

17
grid.go
View File

@@ -8,10 +8,13 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
func maxRows() int {
return ui.TermHeight() - widgets.CompactHeader.Height - ui.Body.Y
}
type Grid struct { type Grid struct {
cursorID string // id of currently selected container cursorID string // id of currently selected container
cmap *ContainerMap cmap *ContainerMap
maxRows int
containers Containers // sorted slice of containers containers Containers // sorted slice of containers
header *widgets.CTopHeader header *widgets.CTopHeader
} }
@@ -26,10 +29,6 @@ func NewGrid() *Grid {
return g return g
} }
func (g *Grid) calcMaxRows() {
g.maxRows = ui.TermHeight() - widgets.CompactHeader.Height - ui.Body.Y
}
// Set an initial cursor position, if possible // Set an initial cursor position, if possible
func (g *Grid) cursorReset() { func (g *Grid) cursorReset() {
if len(g.containers) > 0 { if len(g.containers) > 0 {
@@ -70,7 +69,7 @@ func (g *Grid) cursorDown() {
if idx >= (len(g.containers) - 1) { if idx >= (len(g.containers) - 1) {
return return
} }
if idx >= g.maxRows-1 { if idx >= maxRows()-1 {
return return
} }
active := g.containers[idx] active := g.containers[idx]
@@ -84,7 +83,6 @@ func (g *Grid) cursorDown() {
func (g *Grid) redrawRows() { func (g *Grid) redrawRows() {
// reinit body rows // reinit body rows
g.calcMaxRows()
ui.Body.Rows = []*ui.Row{} ui.Body.Rows = []*ui.Row{}
ui.Clear() ui.Clear()
@@ -101,8 +99,9 @@ func (g *Grid) redrawRows() {
ui.Body.AddRows(widgets.CompactHeader) ui.Body.AddRows(widgets.CompactHeader)
var cursorVisible bool var cursorVisible bool
max := maxRows()
for n, c := range g.containers.Filter() { for n, c := range g.containers.Filter() {
if n >= g.maxRows { if n >= max {
break break
} }
if c.id == g.cursorID { if c.id == g.cursorID {
@@ -216,7 +215,7 @@ func Display(g *Grid) bool {
ui.Handle("/sys/wnd/resize", func(e ui.Event) { ui.Handle("/sys/wnd/resize", func(e ui.Event) {
g.header.Align() g.header.Align()
ui.Body.Width = ui.TermWidth() ui.Body.Width = ui.TermWidth()
log.Infof("resize: width=%v max-rows=%v", ui.Body.Width, g.maxRows) log.Infof("resize: width=%v max-rows=%v", ui.Body.Width, maxRows())
g.redrawRows() g.redrawRows()
}) })