From 12bc71ba07b0e8a3b78a90a7422119e469cafe73 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@lyda.ie>
Date: Thu, 1 Dec 2022 11:05:47 +0000
Subject: [PATCH] Generate from configs.

---
 bar/run.go       | 25 +++++++++++++++++++++----
 config/config.go |  9 +++++++++
 modules/text.go  | 22 ++++++++++------------
 3 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/bar/run.go b/bar/run.go
index 8593a6a..fe55e25 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 3b54aaa..8401ecb 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 460adea..c3f27eb 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)
 }
-- 
GitLab