diff --git a/component/layout.go b/component/layout.go index d190edf..a6200f5 100644 --- a/component/layout.go +++ b/component/layout.go @@ -11,8 +11,9 @@ type Layout struct { ui.Block Components []Component ChangeModeEvents chan Mode - mode Mode + statusbar *StatusBar menu *Menu + mode Mode selection int } @@ -32,14 +33,16 @@ const ( rowsCount = 30 ) -func NewLayout(width, height int, menu *Menu) *Layout { +func NewLayout(width, height int, statusline *StatusBar, menu *Menu) *Layout { block := *ui.NewBlock() block.SetRect(0, 0, width, height) + statusline.SetRect(0, height-1, width, height) return &Layout{ Block: block, Components: make([]Component, 0), + statusbar: statusline, menu: menu, mode: ModeDefault, selection: 0, @@ -191,6 +194,7 @@ func (l *Layout) HandleConsoleEvent(e string) { func (l *Layout) ChangeDimensions(width, height int) { l.SetRect(0, 0, width, height) + l.statusbar.SetRect(0, height-1, width, height) } // TODO func to get prev/next component navigating left/right/top/bottom @@ -219,4 +223,5 @@ func (l *Layout) Draw(buffer *ui.Buffer) { } l.menu.Draw(buffer) + l.statusbar.Draw(buffer) } diff --git a/component/menu.go b/component/menu.go index 592baa1..5022e01 100644 --- a/component/menu.go +++ b/component/menu.go @@ -119,7 +119,7 @@ func (m *Menu) renderHighlight(buffer *ui.Buffer) { arrowsText := "Use arrows for selection" arrowsTextPoint := getMiddlePoint(m.Block, arrowsText, 2) - if arrowsTextPoint.In(m.Rectangle) { + if arrowsTextPoint.Y+1 < m.Inner.Max.Y { buffer.SetString( arrowsText, ui.NewStyle(console.ColorDarkGrey), @@ -129,7 +129,7 @@ func (m *Menu) renderHighlight(buffer *ui.Buffer) { optionsText := " to view options" optionsTextPoint := getMiddlePoint(m.Block, optionsText, 3) - if optionsTextPoint.In(m.Rectangle) { + if optionsTextPoint.Y+1 < m.Inner.Max.Y { buffer.SetString( optionsText, ui.NewStyle(console.ColorDarkGrey), @@ -139,7 +139,7 @@ func (m *Menu) renderHighlight(buffer *ui.Buffer) { resumeText := " to resume" resumeTextPoint := getMiddlePoint(m.Block, resumeText, 4) - if resumeTextPoint.In(m.Rectangle) { + if resumeTextPoint.Y+1 < m.Inner.Max.Y { buffer.SetString( resumeText, ui.NewStyle(console.ColorDarkGrey), diff --git a/component/statusbar.go b/component/statusbar.go new file mode 100644 index 0000000..10a38fd --- /dev/null +++ b/component/statusbar.go @@ -0,0 +1,46 @@ +package component + +import ( + "fmt" + "github.com/sqshq/sampler/console" + ui "github.com/sqshq/termui" + "image" +) + +const ( + bindingsIndent = 4 +) + +type StatusBar struct { + ui.Block + keyBindings []string + configFileName string +} + +func NewStatusLine(configFileName string) *StatusBar { + block := *ui.NewBlock() + block.Border = false + return &StatusBar{ + Block: block, + configFileName: configFileName, + keyBindings: []string{ + "(Q) quit", + "(P) pause", + "(<->) selection", + "(ESC) reset alarm", + }, + } +} + +func (s *StatusBar) Draw(buffer *ui.Buffer) { + buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorClear, console.MenuColorBackground)), s.GetRect()) + buffer.SetString(fmt.Sprintf(" %s %s @ %s", console.AppTitle, console.AppVersion, s.configFileName), ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min) + + 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) +} diff --git a/config/config.go b/config/config.go index 71d9cbc..cc48b3e 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,11 @@ type Config struct { AsciiBoxes []AsciiBoxConfig `yaml:"asciiboxes,omitempty"` } +type Flags struct { + ConfigFileName string + Variables map[string]string +} + type ComponentConfig struct { Title string `yaml:"title"` RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"` @@ -75,7 +80,7 @@ type ComponentSettings struct { Position Position } -func Load() *Config { +func Load() (Config, Flags) { if len(os.Args) < 2 { println("Please specify config file location. See www.github.com/sqshq/sampler for the reference") @@ -86,7 +91,9 @@ func Load() *Config { cfg.validate() cfg.setDefaults() - return cfg + flg := Flags{ConfigFileName: os.Args[1]} + + return *cfg, flg } func Update(settings []ComponentSettings) { diff --git a/config/default.go b/config/default.go index fbd5688..5130cd0 100644 --- a/config/default.go +++ b/config/default.go @@ -6,7 +6,7 @@ import ( ) const ( - defaultRefreshRateMs = 200 + defaultRefreshRateMs = 1000 defaultScale = 1 defaultTheme = console.ThemeDark ) diff --git a/console/console.go b/console/console.go index e33cdff..ceaccf2 100644 --- a/console/console.go +++ b/console/console.go @@ -11,6 +11,7 @@ const ( MaxRenderInterval = 1000 * time.Millisecond MinRenderInterval = 100 * time.Millisecond AppTitle = "sampler" + AppVersion = "0.1.0" ) type Console struct{} diff --git a/console/palette.go b/console/palette.go index 7772cfb..8567914 100644 --- a/console/palette.go +++ b/console/palette.go @@ -21,8 +21,15 @@ const ( ColorPurple ui.Color = 129 ColorGreen ui.Color = 64 ColorDarkGrey ui.Color = 240 - ColorWhite ui.Color = 7 + ColorGrey ui.Color = 242 + ColorWhite ui.Color = 15 ColorBlack ui.Color = 0 + ColorClear ui.Color = -1 +) + +const ( + MenuColorBackground ui.Color = 236 + MenuColorText ui.Color = 255 ) type Palette struct { diff --git a/event/handler.go b/event/handler.go index 299ab30..2f37613 100644 --- a/event/handler.go +++ b/event/handler.go @@ -9,7 +9,7 @@ import ( ) const ( - refreshRateToRenderRateRatio = 0.6 + refreshRateToRenderRateRatio = 0.3 ) type Handler struct { diff --git a/main.go b/main.go index 38f9a84..544dc6a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "github.com/sqshq/sampler/asset" "github.com/sqshq/sampler/component" "github.com/sqshq/sampler/component/asciibox" "github.com/sqshq/sampler/component/barchart" @@ -15,13 +14,13 @@ import ( func main() { - cfg := config.Load() + cfg, flg := config.Load() csl := console.Console{} csl.Init() defer csl.Close() width, height := ui.TerminalDimensions() - layout := component.NewLayout(width, height, component.NewMenu()) + layout := component.NewLayout(width, height, component.NewStatusLine(flg.ConfigFileName), component.NewMenu()) for _, c := range cfg.RunCharts {