starter refactoring

This commit is contained in:
sqshq 2019-03-17 19:55:24 -04:00
parent 9eef047c75
commit 61aa7d2e44
4 changed files with 39 additions and 30 deletions

View File

@ -16,11 +16,11 @@ type Component struct {
RefreshRateMs int RefreshRateMs int
} }
func NewComponent(dbl ui.Drawable, cmr *data.Consumer, cfg config.ComponentConfig, ct config.ComponentType) *Component { func NewComponent(dbl ui.Drawable, cmr *data.Consumer, cfg config.ComponentConfig) *Component {
return &Component{ return &Component{
Drawable: dbl, Drawable: dbl,
Consumer: cmr, Consumer: cmr,
Type: ct, Type: cfg.Type,
Title: cfg.Title, Title: cfg.Title,
Position: cfg.Position, Position: cfg.Position,
Size: cfg.Size, Size: cfg.Size,

View File

@ -10,9 +10,10 @@ type ComponentType rune
const ( const (
TypeRunChart ComponentType = 0 TypeRunChart ComponentType = 0
TypeBarChart ComponentType = 1 TypeBarChart ComponentType = 1
TypeTextBox ComponentType = 2 TypeSparkLine ComponentType = 2
TypeAsciiBox ComponentType = 3 TypeTextBox ComponentType = 3
TypeGauge ComponentType = 4 TypeAsciiBox ComponentType = 4
TypeGauge ComponentType = 5
) )
type ComponentConfig struct { type ComponentConfig struct {
@ -21,6 +22,7 @@ type ComponentConfig struct {
Position Position `yaml:"position"` Position Position `yaml:"position"`
Size Size `yaml:"size"` Size Size `yaml:"size"`
Triggers []TriggerConfig `yaml:"triggers,omitempty"` Triggers []TriggerConfig `yaml:"triggers,omitempty"`
Type ComponentType `yaml:",omitempty"`
} }
type TriggerConfig struct { type TriggerConfig struct {

View File

@ -26,6 +26,7 @@ func (c *Config) setDefaultValues() {
for i, chart := range c.RunCharts { for i, chart := range c.RunCharts {
setDefaultTriggersValues(chart.Triggers) setDefaultTriggersValues(chart.Triggers)
chart.ComponentConfig.Type = TypeRunChart
if chart.RefreshRateMs == nil { if chart.RefreshRateMs == nil {
r := defaultRefreshRateMs r := defaultRefreshRateMs
@ -45,6 +46,7 @@ func (c *Config) setDefaultValues() {
for i, chart := range c.BarCharts { for i, chart := range c.BarCharts {
setDefaultTriggersValues(chart.Triggers) setDefaultTriggersValues(chart.Triggers)
chart.ComponentConfig.Type = TypeBarChart
if chart.RefreshRateMs == nil { if chart.RefreshRateMs == nil {
r := defaultRefreshRateMs r := defaultRefreshRateMs
@ -60,6 +62,7 @@ func (c *Config) setDefaultValues() {
for i, g := range c.Gauges { for i, g := range c.Gauges {
setDefaultTriggersValues(g.Triggers) setDefaultTriggersValues(g.Triggers)
g.ComponentConfig.Type = TypeGauge
if g.RefreshRateMs == nil { if g.RefreshRateMs == nil {
r := defaultRefreshRateMs r := defaultRefreshRateMs
@ -81,6 +84,7 @@ func (c *Config) setDefaultValues() {
for i, box := range c.AsciiBoxes { for i, box := range c.AsciiBoxes {
setDefaultTriggersValues(box.Triggers) setDefaultTriggersValues(box.Triggers)
box.ComponentConfig.Type = TypeAsciiBox
if box.RefreshRateMs == nil { if box.RefreshRateMs == nil {
r := defaultRefreshRateMs r := defaultRefreshRateMs

49
main.go
View File

@ -15,6 +15,19 @@ import (
"github.com/sqshq/sampler/event" "github.com/sqshq/sampler/event"
) )
type Starter struct {
lout *layout.Layout
player *asset.AudioPlayer
flags config.Flags
}
func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, conponentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) {
cpt := component.NewComponent(drawable, consumer, conponentConfig)
triggers := data.NewTriggers(triggersConfig, consumer, s.player)
data.NewSampler(consumer, data.NewItems(itemsConfig), triggers, *conponentConfig.RefreshRateMs)
s.lout.AddComponent(cpt)
}
func main() { func main() {
cfg, flg := config.Load() cfg, flg := config.Load()
@ -28,36 +41,26 @@ func main() {
width, height := ui.TerminalDimensions() width, height := ui.TerminalDimensions()
lout := layout.NewLayout(width, height, component.NewStatusLine(flg.ConfigFileName), component.NewMenu()) lout := layout.NewLayout(width, height, component.NewStatusLine(flg.ConfigFileName), component.NewMenu())
starter := &Starter{lout, player, flg}
for _, c := range cfg.RunCharts { for _, c := range cfg.RunCharts {
chart := runchart.NewRunChart(c) cpt := runchart.NewRunChart(c)
cpt := component.NewComponent(chart, chart.Consumer, c.ComponentConfig, config.TypeRunChart) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
triggers := data.NewTriggers(c.Triggers, chart.Consumer, player)
data.NewSampler(chart.Consumer, data.NewItems(c.Items), triggers, *c.RefreshRateMs)
lout.AddComponent(cpt)
} }
for _, a := range cfg.AsciiBoxes { for _, c := range cfg.AsciiBoxes {
box := asciibox.NewAsciiBox(a) cpt := asciibox.NewAsciiBox(c)
cpt := component.NewComponent(box, box.Consumer, a.ComponentConfig, config.TypeAsciiBox) starter.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers)
triggers := data.NewTriggers(a.Triggers, box.Consumer, player)
data.NewSampler(box.Consumer, data.NewItems([]config.Item{a.Item}), triggers, *a.RefreshRateMs)
lout.AddComponent(cpt)
} }
for _, b := range cfg.BarCharts { for _, c := range cfg.BarCharts {
chart := barchart.NewBarChart(b) cpt := barchart.NewBarChart(c)
cpt := component.NewComponent(chart, chart.Consumer, b.ComponentConfig, config.TypeBarChart) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
triggers := data.NewTriggers(b.Triggers, chart.Consumer, player)
data.NewSampler(chart.Consumer, data.NewItems(b.Items), triggers, *b.RefreshRateMs)
lout.AddComponent(cpt)
} }
for _, gc := range cfg.Gauges { for _, c := range cfg.Gauges {
g := gauge.NewGauge(gc) cpt := gauge.NewGauge(c)
cpt := component.NewComponent(g, g.Consumer, gc.ComponentConfig, config.TypeGauge) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
triggers := data.NewTriggers(gc.Triggers, g.Consumer, player)
data.NewSampler(g.Consumer, data.NewItems(gc.Items), triggers, *gc.RefreshRateMs)
lout.AddComponent(cpt)
} }
handler := event.NewHandler(lout) handler := event.NewHandler(lout)