Created
June 7, 2016 00:04
-
-
Save yasarix/0f6f1322e55e48c6c2cd34d0dd77552e to your computer and use it in GitHub Desktop.
termui Dashboard sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
ui "github.com/gizak/termui" | |
"log" | |
"sync" | |
"time" | |
) | |
type UiElements struct { | |
sync.Mutex | |
TextBoxText string | |
} | |
var ( | |
TopHits *ui.List | |
TextWidget *ui.Par | |
Elements = &UiElements{} | |
) | |
func main() { | |
go func() { | |
for { | |
time.Sleep(1 * time.Second) | |
now := time.Now() | |
Elements.Lock() | |
Elements.TextBoxText = "Date/Time: " + now.Format(time.RFC3339) | |
Elements.Unlock() | |
} | |
}() | |
if err := setupUi(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func setupUi() error { | |
if err := ui.Init(); err != nil { | |
return err | |
} | |
defer ui.Close() | |
TopHits = ui.NewList() | |
TopHits.BorderLabel = "Top hits" | |
TopHits.Items = []string{ | |
"[1] Downloading File 1", | |
"[2] Downloading File 2", | |
"[3] Uploading File 3", | |
"[4] Uploading File 3", | |
"[5] Uploading File 3", | |
"[6] Uploading File 3", | |
"[7] Uploading File 3", | |
} | |
TopHits.Height = 8 | |
TextWidget = ui.NewPar("") | |
TextWidget.Height = 8 | |
TextWidget.BorderLabel = "Demonstration" | |
// build layout | |
ui.Body.AddRows( | |
ui.NewRow( | |
ui.NewCol(6, 0, TopHits), | |
ui.NewCol(6, 0, TextWidget))) | |
// calculate layout | |
ui.Body.Align() | |
ui.Render(ui.Body) | |
ui.Handle("/sys/kbd/q", func(ui.Event) { | |
ui.StopLoop() | |
}) | |
ui.Handle("/timer/1s", func(e ui.Event) { | |
Elements.Lock() | |
TextWidget.Text = Elements.TextBoxText | |
Elements.Unlock() | |
ui.Render(ui.Body) | |
}) | |
ui.Handle("/sys/wnd/resize", func(e ui.Event) { | |
ui.Body.Width = ui.TermWidth() | |
ui.Body.Align() | |
ui.Render(ui.Body) | |
}) | |
// Start Ui | |
ui.Loop() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment