Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" stdlog "log" "math/rand" "os" "strings" "time" "github.com/jung-kurt/etc/go/dashboard" ) const ( cnCount int = iota cnName cnLog cnBannerA cnBannerB cnBannerC ) func log(str string) { stdlog.Println(str) if dashboard.Active() { dashboard.UpdateLine(cnLog, str) } } func logf(format string, args ...interface{}) { log(fmt.Sprintf(format, args...)) } func updateName() { names := []string{"Shiawassee", "Prairie", "Natasha", "Tess", "Grant"} length := len(names) for dashboard.Updateable() { dashboard.UpdateKeyVal(cnName, names[rand.Intn(length)]) sleep(750) } } func updateCount() { var buf strings.Builder var format string var j int buf.Grow(1024) for dashboard.Updateable() { if j&1 == 0 { format = "[%d]" } else { format = "%d" } fmt.Fprintf(&buf, format, j) // logf("update %d %s", j, buf.String()) dashboard.UpdateKeyVal(cnCount, buf.String()) buf.Reset() sleep(1000) j++ } } func updateLog() { var buf strings.Builder var j int buf.Grow(256) fillStr := strings.Repeat("x", 256) for dashboard.Updateable() { fmt.Fprintf(&buf, "This is line %s %d", fillStr[:rand.Intn(32)+1], j) log(buf.String()) buf.Reset() sleep(1250) j++ } } func sleep(waitMs int) { time.Sleep(time.Duration(waitMs) * time.Millisecond) } func main() { var fl *os.File var err error fl, err = os.Create("log") if err == nil { stdlog.SetFlags(stdlog.LstdFlags | stdlog.Lmicroseconds) stdlog.SetOutput(fl) dashboard.RegisterHeader(cnBannerA, 0, 0, 0, "\\tDashboard (v 0.2)") dashboard.RegisterKeyVal(cnCount, 1, 1, 40, "Count") dashboard.RegisterLine(cnLog, 1, 2, 5, "2006-Jan-02 15:04:05 ") dashboard.RegisterHeaderLine(cnBannerC, 1, 7, 40, "\\t Dog ") dashboard.RegisterKeyVal(cnName, 1, 8, 40, "Name") dashboard.RegisterHeader(cnBannerB, 0, 9, 0, " This is a banner \\t more work to do \\t Press Q to quit ") go updateCount() go updateName() go updateLog() err = dashboard.Run('Q', 'q', 27) stdlog.SetOutput(os.Stdout) fl.Close() fmt.Println("Success") } if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) } }
Output: Success
Index ¶
- func Active() (active bool)
- func RegisterHeader(id, x, y, wd int, keyStr string)
- func RegisterHeaderLine(id, x, y, wd int, keyStr string)
- func RegisterKeyVal(id, x, y, wd int, keyStr string)
- func RegisterLine(id, x, y, lineCount int, timeFmtStr string)
- func Run(quitRunes ...rune) (err error)
- func UpdateKeyVal(id int, str string)
- func UpdateLine(id int, str string)
- func Updateable() (updateable bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Active ¶
func Active() (active bool)
Active returns true if the dashboard is currently active. It may be called safely from other goroutines. It is typically used in application loops.
func RegisterHeader ¶
RegisterHeader registers a dashboard static line with the identifier specified by id. Its coordinates are specified by x and y. The total field's width is specified by wd. A zero value for wd indicates the full width of the screen, and a negative value indicates the position from the right. The static key is specified by keyStr.
func RegisterHeaderLine ¶
RegisterHeaderLine registers a dashboard static line with the identifier specified by id. Its coordinates are specified by x and y. The total field's width is specified by wd. A zero value for wd indicates the full width of the screen, and a negative value indicates the position from the right. The static key is specified by keyStr. A horizontal line is used between string segments.
func RegisterKeyVal ¶
RegisterKeyVal registers a dashboard key/value pair with the identifier specified by id. Its coordinates are specified by x and y, and the total field's width is specified by wd. The static key is specified by keyStr.
func RegisterLine ¶
RegisterLine registers a dashboard rolling line field with the identifier specified by id. Its coordinates are specified by x and y, and the total number of rows used is specified by lineCount. If timeFmt is empty, no timestamp prefix will be displayed. Otherwise, it will be used to format a leading timestamp.
func Run ¶
Run changes the screen to a dashboard. This function does not return until one of the keys included in the list of quitRunes is pressed. Up until that time, all application logic should be handled in other goroutines that call one or more of the UpdateXXX() functions to update the dashboard.
func UpdateKeyVal ¶
UpdateKeyVal updates the key/value pair specified by id with the value specified by str.
func UpdateLine ¶
UpdateLine updates the rolling line field specified by id with the str.
func Updateable ¶
func Updateable() (updateable bool)
Updateable returns true if the dashboard is currently updateable. It may be called safely from other goroutines. It is typically used in application loops.
Types ¶
This section is empty.