sampler-fork/component/statusbar.go

74 lines
1.8 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 (
pauseText = " P A U S E D "
2019-06-07 05:39:11 +00:00
bindingsIndent = 3
2019-02-25 00:08:36 +00:00
)
type StatusBar struct {
*ui.Block
2019-05-21 03:18:23 +00:00
keyBindings []string
text string
pause bool
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)
if license.Company != nil {
text += fmt.Sprintf(", %s", *license.Company)
}
2019-05-21 03:18:23 +00:00
} 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())
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)
}
2019-06-13 03:35:12 +00:00
buffer.SetString(s.text, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min)
if s.pause {
buffer.SetString(pauseText, ui.NewStyle(console.MenuColorBackground, console.MenuColorText), image.Pt(s.Max.X-s.Dx()/2-len(pauseText)/2, s.Min.Y))
}
2019-02-25 00:08:36 +00:00
s.Block.Draw(buffer)
}
func (s *StatusBar) TogglePause() {
s.pause = !s.pause
}