diff --git a/bar/run.go b/bar/run.go index dff0ec7c971eae675eb555651feecd8ac678ddae..8593a6a14f0c9ccfc4eb2c76f79643306724b0f9 100644 --- a/bar/run.go +++ b/bar/run.go @@ -6,9 +6,12 @@ import ( "bufio" "fmt" "os" + "strings" "time" "github.com/spf13/cobra" + "github.com/spf13/viper" + "gitlab.ie.suberic.net/kevin/i3going-on/config" "golang.org/x/sys/unix" ) @@ -25,24 +28,20 @@ func handleCommands() { // Run is essentially the main program. func Run(cmd *cobra.Command, args []string) { + cfg, err := config.ReadConfig(viper.GetString("config")) + cobra.CheckErr(err) + go handleCommands() fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n", unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT")) fmt.Println("[") for { - fmt.Printf(` [ - { - "name": "color", - "full_text": "RED", - "color": "#11ff11" - }, - { - "name": "bland", - "full_text": "default" - } - ], -`) + line := make([]string, 0, 5) + for _, module := range cfg.Modules { + line = append(line, module.Render()) + } + fmt.Printf(strings.Join(line, ",")) time.Sleep(5 * time.Second) } } diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..3b54aaa9522a014575024449aa03a4f885d8577a --- /dev/null +++ b/config/config.go @@ -0,0 +1,26 @@ +package config + +import ( + "os" + + "gitlab.ie.suberic.net/kevin/i3going-on/modules" + "gopkg.in/yaml.v3" +) + +// Config is the structure for the config file. +type Config struct { + Modules []modules.Module `yaml:"modules"` +} + +// ReadConfig reads in the config. +func ReadConfig(configFile string) (*Config, error) { + config, err := os.Open(configFile) + if err != nil { + return nil, err + } + defer config.Close() + configParser := yaml.NewDecoder(config) + cfg := &Config{} + err = configParser.Decode(&cfg) + return cfg, err +} diff --git a/config/main.go b/config/main.go deleted file mode 100644 index bd3050ef659e66b6c16efe8d474abdbef8bff9e8..0000000000000000000000000000000000000000 --- a/config/main.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "gopkg.in/yaml.v3" -) - -type ParamsInterface interface { - Name() string - OnClick() string -} - -type Module struct { - Module string `yaml:"module"` - Name string `yaml:"name"` - OnClick string `yaml:"on-click"` - Params ParamsInterface `yaml:"-"` -} - -type M Module - -type Params struct { - *M `yaml:",inline"` - Params yaml.Node `yaml:"params"` -} - -type DateMod struct { - name string - onclick string - Format string `yaml:"format"` -} - -type TextMod struct { - name string - onclick string - //Name string `yaml:"-"` - //OnClick string `yaml:"-"` - Text string `yaml:"text"` - Color string `yaml:"color"` -} - -func NewDate(m *Module) *DateMod { - d := &DateMod{ - name: m.Name, - onclick: m.OnClick, - } - if d.name == "" { - d.name = "date" - } - return d -} - -func (d *DateMod) Name() string { - return d.name -} - -func (d *DateMod) OnClick() string { - return d.onclick -} - -func NewText(m *Module) *TextMod { - t := &TextMod{ - name: m.Name, - onclick: m.OnClick, - } - if t.name == "" { - t.name = "text" - } - return t -} - -func (t *TextMod) Name() string { - return t.name -} - -func (t *TextMod) OnClick() string { - return t.onclick -} - -type Modules struct { - Modules []Module `yaml:"modules"` -} - -func (m *Module) UnmarshalYAML(node *yaml.Node) error { - params := &Params{M: (*M)(m)} - if err := node.Decode(params); err != nil { - return err - } - - switch params.Module { - case "date": - m.Params = NewDate(m) - case "text": - m.Params = NewText(m) - default: - panic("module unknown") - } - return params.Params.Decode(m.Params) -} - -func main() { - config, err := os.Open(os.Args[1]) - if err != nil { - panic(err) - } - defer config.Close() - configParser := yaml.NewDecoder(config) - cfg := &Modules{} - err = configParser.Decode(&cfg) - if err != nil { - panic(err) - } - for _, m := range cfg.Modules { - fmt.Printf("%s:%s:%s\n", m.Module, m.Name, m.Params.Name()) - } -} diff --git a/modules/date.go b/modules/date.go new file mode 100644 index 0000000000000000000000000000000000000000..00352631544e177d67d1c9e1b6b9204be9db1449 --- /dev/null +++ b/modules/date.go @@ -0,0 +1,42 @@ +package modules + +import ( + "fmt" + "time" +) + +// DateMod module parameters for the text module. +type DateMod struct { + name string + onclick string + Format string `yaml:"format"` +} + +// NewDate creates a DateMod instance. +func NewDate(m *Module) *DateMod { + d := &DateMod{ + name: m.Name, + onclick: m.OnClick, + } + if d.name == "" { + d.name = "date" + } + return d +} + +// Name returns the name setting for the module. +func (d *DateMod) Name() string { + return d.name +} + +// OnClick returns the on-click setting for the module. +func (d *DateMod) OnClick() string { + return d.onclick +} + +// Render renders the module. +func (d *DateMod) Render() string { + + now := time.Now().Format(d.Format) + return fmt.Sprintf("{\"full_text\": \"%s\"}", now) +} diff --git a/modules/modules.go b/modules/modules.go new file mode 100644 index 0000000000000000000000000000000000000000..216064ad0f3b54089e011b3dd3ad48d6e0ffa2b0 --- /dev/null +++ b/modules/modules.go @@ -0,0 +1,51 @@ +package modules + +import ( + "gopkg.in/yaml.v3" +) + +// ParamsInterface is the interface for module paramaters. +type ParamsInterface interface { + Name() string + OnClick() string + Render() string +} + +// Module defines a module. +type Module struct { + Module string `yaml:"module"` + Name string `yaml:"name"` + OnClick string `yaml:"on-click"` + Params ParamsInterface `yaml:"-"` +} + +// M is an intermidiate type to avoid name collisions. +type M Module + +// Params is the structure for module specific params. +type Params struct { + *M `yaml:",inline"` + Params yaml.Node `yaml:"params"` +} + +// UnmarshalYAML is a helper program to unmarshall the specific params. +func (m *Module) UnmarshalYAML(node *yaml.Node) error { + params := &Params{M: (*M)(m)} + if err := node.Decode(params); err != nil { + return err + } + + switch params.Module { + case "date": + m.Params = NewDate(m) + case "text": + m.Params = NewText(m) + default: + panic("module unknown") + } + return params.Params.Decode(m.Params) +} + +func (m *Module) Render() string { + return m.Params.Render() +} diff --git a/modules/text.go b/modules/text.go new file mode 100644 index 0000000000000000000000000000000000000000..460adea2a50ce5ad2bef6a273d059a6e7d7b9c9e --- /dev/null +++ b/modules/text.go @@ -0,0 +1,43 @@ +package modules + +// TextMod module parameters for the text module. +type TextMod struct { + name string + onclick string + Text string `yaml:"text"` + Color string `yaml:"color"` +} + +// NewText creates a TextMod instance. +func NewText(m *Module) *TextMod { + t := &TextMod{ + name: m.Name, + onclick: m.OnClick, + } + if t.name == "" { + t.name = "text" + } + return t +} + +// Name returns the name setting for the module. +func (t *TextMod) Name() string { + return t.name +} + +// OnClick returns the on-click setting for the module. +func (t *TextMod) Render() string { + + { + "name": "color", + "full_text": "RED", + "color": "#11ff11" + } + + +// Render renders the module. +func (d *DateMod) Render() string { + + now := time.Now().Format(d.Format) + return fmt.Sprintf("{\"full_text\": \"%s\"}", ) +}