sampler-fork/component/statusbar.go

76 lines
2.0 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-07-30 02:39:17 +00:00
func NewStatusBar(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)
2019-07-22 03:30:05 +00:00
if license == nil || !license.Valid || license.Type == nil {
2019-05-21 03:18:23 +00:00
text += console.AppLicenseWarning
2019-07-22 03:30:05 +00:00
} else if *license.Type == metadata.TypePersonal {
text += fmt.Sprintf("%s | personal license: %s", configFileName, *license.Username)
2019-05-21 03:18:23 +00:00
} 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-07-28 20:59:51 +00:00
buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorClear, console.GetMenuColorReverse())), s.GetRect())
2019-02-25 00:08:36 +00:00
indent := bindingsIndent
for _, binding := range s.keyBindings {
2019-07-28 20:59:51 +00:00
buffer.SetString(binding, ui.NewStyle(console.GetMenuColor(), console.GetMenuColorReverse()), image.Pt(s.Max.X-len(binding)-indent, s.Min.Y))
2019-02-25 00:08:36 +00:00
indent += bindingsIndent + len(binding)
}
2019-07-28 20:59:51 +00:00
buffer.SetString(s.text, ui.NewStyle(console.GetMenuColor(), console.GetMenuColorReverse()), s.Min)
2019-06-13 03:35:12 +00:00
if s.pause {
2019-07-28 20:59:51 +00:00
buffer.SetString(pauseText, ui.NewStyle(console.GetMenuColorReverse(), console.GetMenuColor()), 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
}