sampler-fork/component/statusbar.go

60 lines
1.5 KiB
Go
Raw Normal View History

2019-02-25 00:08:36 +00:00
package component
import (
"fmt"
2019-03-14 03:01:44 +00:00
ui "github.com/gizak/termui/v3"
2019-02-25 00:08:36 +00:00
"github.com/sqshq/sampler/console"
2019-05-24 02:58:46 +00:00
"github.com/sqshq/sampler/metadata"
2019-02-25 00:08:36 +00:00
"image"
)
const (
bindingsIndent = 4
)
type StatusBar struct {
*ui.Block
2019-05-21 03:18:23 +00:00
keyBindings []string
text string
2019-02-25 00:08:36 +00:00
}
2019-05-24 02:58:46 +00:00
func NewStatusLine(configFileName string, palette console.Palette, license *metadata.License) *StatusBar {
2019-02-25 00:08:36 +00:00
block := *ui.NewBlock()
block.Border = false
2019-05-21 03:18:23 +00:00
text := fmt.Sprintf(" %s %s | ", console.AppTitle, console.AppVersion)
if license == nil || !license.Valid {
2019-05-21 03:18:23 +00:00
text += console.AppLicenseWarning
} else if license.Username != nil {
text += fmt.Sprintf("%s | licensed to %s", configFileName, *license.Username)
} else {
text += fmt.Sprintf("%s | licensed to %s", configFileName, *license.Company)
}
2019-02-25 00:08:36 +00:00
return &StatusBar{
2019-05-21 03:18:23 +00:00
Block: NewBlock("", false, palette),
text: text,
2019-02-25 00:08:36 +00:00
keyBindings: []string{
2019-05-21 03:18:23 +00:00
"(q) quit",
"(p) pause",
2019-02-25 00:08:36 +00:00
"(<->) selection",
2019-03-08 04:04:46 +00:00
"(ESC) reset alerts",
2019-02-25 00:08:36 +00:00
},
}
}
func (s *StatusBar) Draw(buffer *ui.Buffer) {
2019-05-21 03:18:23 +00:00
buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorClear, console.MenuColorBackground)), s.GetRect())
buffer.SetString(s.text, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min)
2019-02-25 00:08:36 +00:00
indent := bindingsIndent
for _, binding := range s.keyBindings {
buffer.SetString(binding, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), image.Pt(s.Max.X-len(binding)-indent, s.Min.Y))
indent += bindingsIndent + len(binding)
}
s.Block.Draw(buffer)
}