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
}
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{
Drawable: dbl,
Consumer: cmr,
Type: ct,
Type: cfg.Type,
Title: cfg.Title,
Position: cfg.Position,
Size: cfg.Size,

View File

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

View File

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

49
main.go
View File

@ -15,6 +15,19 @@ import (
"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() {
cfg, flg := config.Load()
@ -28,36 +41,26 @@ func main() {
width, height := ui.TerminalDimensions()
lout := layout.NewLayout(width, height, component.NewStatusLine(flg.ConfigFileName), component.NewMenu())
starter := &Starter{lout, player, flg}
for _, c := range cfg.RunCharts {
chart := runchart.NewRunChart(c)
cpt := component.NewComponent(chart, chart.Consumer, c.ComponentConfig, config.TypeRunChart)
triggers := data.NewTriggers(c.Triggers, chart.Consumer, player)
data.NewSampler(chart.Consumer, data.NewItems(c.Items), triggers, *c.RefreshRateMs)
lout.AddComponent(cpt)
cpt := runchart.NewRunChart(c)
starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
}
for _, a := range cfg.AsciiBoxes {
box := asciibox.NewAsciiBox(a)
cpt := component.NewComponent(box, box.Consumer, a.ComponentConfig, config.TypeAsciiBox)
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 _, c := range cfg.AsciiBoxes {
cpt := asciibox.NewAsciiBox(c)
starter.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers)
}
for _, b := range cfg.BarCharts {
chart := barchart.NewBarChart(b)
cpt := component.NewComponent(chart, chart.Consumer, b.ComponentConfig, config.TypeBarChart)
triggers := data.NewTriggers(b.Triggers, chart.Consumer, player)
data.NewSampler(chart.Consumer, data.NewItems(b.Items), triggers, *b.RefreshRateMs)
lout.AddComponent(cpt)
for _, c := range cfg.BarCharts {
cpt := barchart.NewBarChart(c)
starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
}
for _, gc := range cfg.Gauges {
g := gauge.NewGauge(gc)
cpt := component.NewComponent(g, g.Consumer, gc.ComponentConfig, config.TypeGauge)
triggers := data.NewTriggers(gc.Triggers, g.Consumer, player)
data.NewSampler(g.Consumer, data.NewItems(gc.Items), triggers, *gc.RefreshRateMs)
lout.AddComponent(cpt)
for _, c := range cfg.Gauges {
cpt := gauge.NewGauge(c)
starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)
}
handler := event.NewHandler(lout)