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 (
|
2019-06-07 05:09:37 +00:00
|
|
|
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 {
|
2019-03-24 00:16:59 +00:00
|
|
|
*ui.Block
|
2019-05-21 03:18:23 +00:00
|
|
|
keyBindings []string
|
|
|
|
text string
|
2019-06-07 05:09:37 +00:00
|
|
|
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-05-28 01:44:06 +00:00
|
|
|
|
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)
|
2019-06-07 05:09:37 +00:00
|
|
|
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-14 03:24:29 +00:00
|
|
|
|
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)
|
|
|
|
|
2019-06-07 05:09:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-06-07 05:09:37 +00:00
|
|
|
|
|
|
|
func (s *StatusBar) TogglePause() {
|
|
|
|
s.pause = !s.pause
|
|
|
|
}
|