diff --git a/application.go b/application.go index b04d96f..bdccf7f 100644 --- a/application.go +++ b/application.go @@ -71,6 +71,7 @@ func (a *Application) Run() error { a.Unlock() return err } + a.Unlock() // We catch panics to clean up because they mess up the terminal. defer func() { @@ -83,7 +84,6 @@ func (a *Application) Run() error { }() // Draw the screen for the first time. - a.Unlock() a.Draw() // Start event loop. diff --git a/box.go b/box.go index 509f0af..3d2d8c7 100644 --- a/box.go +++ b/box.go @@ -129,6 +129,9 @@ func (b *Box) wrapInputHandler(inputHandler func(tcell.Event, func(p Primitive)) } } } +func (b *Box) WrapInputHandler(inputHandler func(tcell.Event, func(p Primitive))) func(tcell.Event, func(p Primitive)) { + return b.wrapInputHandler(inputHandler) +} // InputHandler returns nil. func (b *Box) InputHandler() func(event tcell.Event, setFocus func(p Primitive)) { @@ -272,6 +275,11 @@ func (b *Box) Mount(context map[string]interface{}) error { return nil } +// Mount is called when this primitive is mounted (by the router). +func (b *Box) Refresh(context map[string]interface{}) error { + return nil +} + // Unmount is called when this primitive is unmounted. func (b *Box) Unmount() error { b.isMounted = false diff --git a/pages.go b/pages.go index 367654a..3c74d9e 100644 --- a/pages.go +++ b/pages.go @@ -164,6 +164,10 @@ func (p *Pages) HidePage(name string) *Pages { // visibility to "false". func (p *Pages) SwitchToPage(name string, context map[string]interface{}) *Pages { if p.curr != nil { + if p.curr.Name == name { + p.curr.Item.Refresh(context) + return p + } p.curr.Item.Unmount() } for _, page := range p.pages { diff --git a/primitive.go b/primitive.go index d232c1b..6a41529 100644 --- a/primitive.go +++ b/primitive.go @@ -53,6 +53,9 @@ type Primitive interface { // Mount is a longer term context for bringing a widget into scope Mount(context map[string]interface{}) error + // Refresh is a longer term context for bringing a widget into scope + Refresh(context map[string]interface{}) error + // Unmount is the opposite of mount Unmount() error diff --git a/table.go b/table.go index d33580c..334fe88 100644 --- a/table.go +++ b/table.go @@ -161,6 +161,9 @@ func (c *TableCell) GetLastPosition() (x, y, width int) { type Table struct { *Box + // If the table is smaller than the space, how to position + align int + // Whether or not this table has borders around each cell. borders bool @@ -228,6 +231,13 @@ func (t *Table) Clear() *Table { return t } +// SetAlign sets the table's positioning within the space. +// This only takes effect when the table is smaller than the space provided +func (t *Table) SetAlign(align int) *Table { + t.align = align + return t +} + // SetBorders sets whether or not each cell in the table is surrounded by a // border. func (t *Table) SetBorders(show bool) *Table { @@ -428,7 +438,7 @@ func (t *Table) Draw(screen tcell.Screen) { return t.cells[row][column] } - // If this cell is not selectable, find the next one. + // If this cell is selectable, find the next one. if t.rowsSelectable || t.columnsSelectable { if t.selectedColumn < 0 { t.selectedColumn = 0 @@ -470,10 +480,14 @@ func (t *Table) Draw(screen tcell.Screen) { if t.borders { if 2*(len(t.cells)-t.rowOffset) < height { t.trackEnd = true + } else { + t.trackEnd = false } } else { if len(t.cells)-t.rowOffset < height { t.trackEnd = true + } else { + t.trackEnd = false } } if t.trackEnd { @@ -584,6 +598,16 @@ ColumnLoop: } t.columnOffset = skipped + // If we are smaller than our space and have alignment + if tableWidth < width { + if t.align == AlignCenter { + x += (width - tableWidth) / 2 + } + if t.align == AlignRight { + x += (width - tableWidth) + } + } + // Helper function which draws border runes. borderStyle := tcell.StyleDefault.Background(t.backgroundColor).Foreground(t.bordersColor) drawBorder := func(colX, rowY int, ch rune) {