diff --git a/bar/run.go b/bar/run.go index 8593a6a14f0c9ccfc4eb2c76f79643306724b0f9..fe55e259c67d021f3a377f58ce4b6cd8de265816 100644 --- a/bar/run.go +++ b/bar/run.go @@ -28,20 +28,37 @@ func handleCommands() { // Run is essentially the main program. func Run(cmd *cobra.Command, args []string) { + viper.BindPFlags(cmd.Flags()) cfg, err := config.ReadConfig(viper.GetString("config")) - cobra.CheckErr(err) - + if err != nil { + // TODO Make a better default + cfg, err = config.ReadConfigStr(`--- +modules: + - module: text + name: post + params: + text: "post" + color: "#11ff11" + on-click: xdg-open https://mastodon.ie/ + - module: date + params: + format: 06-05-04 15:02 + on-click: xdg-open https://calendar.google.com/ +`) + 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("[") + separator := "[" for { line := make([]string, 0, 5) for _, module := range cfg.Modules { line = append(line, module.Render()) } - fmt.Printf(strings.Join(line, ",")) + fmt.Printf("%s\n[%s]", separator, strings.Join(line, ",")) + separator = "," time.Sleep(5 * time.Second) } } diff --git a/config/config.go b/config/config.go index 3b54aaa9522a014575024449aa03a4f885d8577a..8401ecbaf24ed482f30002b7c7555337c0e87c17 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "os" + "strings" "gitlab.ie.suberic.net/kevin/i3going-on/modules" "gopkg.in/yaml.v3" @@ -12,6 +13,14 @@ type Config struct { Modules []modules.Module `yaml:"modules"` } +// ReadConfigStr reads in the config. +func ReadConfigStr(configStr string) (*Config, error) { + configParser := yaml.NewDecoder(strings.NewReader(configStr)) + cfg := &Config{} + err := configParser.Decode(&cfg) + return cfg, err +} + // ReadConfig reads in the config. func ReadConfig(configFile string) (*Config, error) { config, err := os.Open(configFile) diff --git a/modules/text.go b/modules/text.go index 460adea2a50ce5ad2bef6a273d059a6e7d7b9c9e..c3f27ebf8f3077dbc9c61fd68cc12bb94c51f3a1 100644 --- a/modules/text.go +++ b/modules/text.go @@ -1,5 +1,9 @@ package modules +import ( + "fmt" +) + // TextMod module parameters for the text module. type TextMod struct { name string @@ -26,18 +30,12 @@ func (t *TextMod) Name() string { } // OnClick returns the on-click setting for the module. -func (t *TextMod) Render() string { - - { - "name": "color", - "full_text": "RED", - "color": "#11ff11" - } - +func (t *TextMod) OnClick() string { + return t.onclick +} // Render renders the module. -func (d *DateMod) Render() string { - - now := time.Now().Format(d.Format) - return fmt.Sprintf("{\"full_text\": \"%s\"}", ) +func (t *TextMod) Render() string { + // TODO: Make color and name optional. + return fmt.Sprintf(`{"name": "%s", "full_text": "%s", "color": "%s"}`, t.name, t.Text, t.Color) }