Skip to content

Instantly share code, notes, and snippets.

@christophberger
Last active April 4, 2017 09:39
Show Gist options
  • Save christophberger/8ef0da1368a4048a70381e6cb3d5f0e4 to your computer and use it in GitHub Desktop.
Save christophberger/8ef0da1368a4048a70381e6cb3d5f0e4 to your computer and use it in GitHub Desktop.
Code snippet from github.com/appliedgo/tui: termui
func runTermui() {
// Initialize termui.
err := t.Init()
if err != nil {
log.Fatalln("Cannot initialize termui")
}
// `termui` needs some cleanup when terminating.
defer t.Close()
// Get the height of the terminal.
th := t.TermHeight()
// The list block
lb := t.NewList()
lb.Height = th
lb.BorderLabel = "List"
lb.BorderLabelFg = t.ColorGreen
lb.BorderFg = t.ColorGreen
lb.ItemFgColor = t.ColorWhite
lb.Items = listItems
// The input block. termui has no edit box yet, but at the time of
// this writing, there is an open [pull request](https://github.com/gizak/termui/pull/129) for adding
// a text input widget.
ib := t.NewPar("")
ib.Height = ih
ib.BorderLabel = "Input"
ib.BorderLabelFg = t.ColorYellow
ib.BorderFg = t.ColorYellow
ib.TextFgColor = t.ColorWhite
// The Output block.
ob := t.NewPar("\nPress Ctrl-C to quit")
ob.Height = th - ih
ob.BorderLabel = "Output"
ob.BorderLabelFg = t.ColorCyan
ob.BorderFg = t.ColorCyan
ob.TextFgColor = t.ColorWhite
// Now we need to create the layout. The blocks have gotten a size
// but no position. A grid layout puts everything into place.
// t.Body is a pre-defined grid. We add one row that contains
// two columns.
//
// The grid uses a 12-column system, so we have to give a "span"
// parameter to each column that specifies how many grid column
// each column occupies.
t.Body.AddRows(
t.NewRow(
t.NewCol(3, 0, lb),
t.NewCol(9, 0, ob, ib)))
// Render the grid.
t.Body.Align()
t.Render(t.Body)
// When the window resizes, the grid must adopt to the new size.
// We use a hander func for this.
t.Handle("/sys/wnd/resize", func(t.Event) {
// Update the heights of list box and output box.
lb.Height = t.TermHeight()
ob.Height = t.TermHeight() - ih
t.Body.Width = t.TermWidth()
t.Body.Align()
t.Render(t.Body)
})
//
// We need a way out. Ctrl-C shall stop the event loop.
t.Handle("/sys/kbd/C-c", func(t.Event) {
t.StopLoop()
})
// start the event loop.
t.Loop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment