From 6b7a3c622bff273c1f93b62bb545c90505819fbc Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@lyda.ie>
Date: Thu, 1 Dec 2022 23:02:51 +0000
Subject: [PATCH] Receive clicks...

---
 bar/clicks.go      | 41 +++++++++++++++++++++--------------------
 bar/run.go         | 18 +++++++++++++++++-
 modules/clicks.go  | 15 +++++++++++++++
 modules/date.go    |  4 +++-
 modules/modules.go |  2 ++
 modules/text.go    |  2 ++
 6 files changed, 60 insertions(+), 22 deletions(-)
 create mode 100644 modules/clicks.go

diff --git a/bar/clicks.go b/bar/clicks.go
index 2eea888..d1c5f2f 100644
--- a/bar/clicks.go
+++ b/bar/clicks.go
@@ -3,29 +3,30 @@
 package bar
 
 import (
-	"bufio"
+	"fmt"
 	"os"
-)
 
-type Clicks struct {
-	Button    int64    `json:"button"`
-	Modifiers []string `json:"modifiers"`
-	X         int64    `json:"x"`
-	Y         int64    `json:"y"`
-	RelativeX int64    `json:"relative_x"`
-	RelativeY int64    `json:"relative_y"`
-	Width     int64    `json:"width"`
-	Height    int64    `json:"height"`
-}
+	"gitlab.ie.suberic.net/kevin/i3going-on/modules"
+	"gopkg.in/yaml.v2"
+)
 
 //{"button":1,"modifiers":[],"x":1952,"y":1144,"relative_x":62,"relative_y":14,"width":112,"height":22}
-func handleCommands() {
-	logger, err := os.Create("/tmp/i3going-on.log")
-	if err != nil {
-		panic(err)
-	}
-	scanner := bufio.NewScanner(os.Stdin)
-	for scanner.Scan() {
-		logger.WriteString(scanner.Text())
+func handleCommands(clicks chan modules.Click) {
+	logger, _ := os.Create("/tmp/i3going-on.log")
+
+	for {
+		logger.WriteString("Looking for a click...\n")
+		clickParser := yaml.NewDecoder(os.Stdin)
+		click := modules.Click{}
+		err := clickParser.Decode(&click)
+		if err != nil {
+			logger.WriteString(fmt.Sprintf("Error on click: %s\n", err))
+			clicks <- modules.Click{
+				Name: fmt.Sprintf("ERROR: %s", err),
+			}
+			continue
+		}
+		logger.WriteString(fmt.Sprintf("Error on click: %s\n", err))
+		clicks <- click
 	}
 }
diff --git a/bar/run.go b/bar/run.go
index 53fcd23..7af2830 100644
--- a/bar/run.go
+++ b/bar/run.go
@@ -9,6 +9,7 @@ import (
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
+	"gitlab.ie.suberic.net/kevin/i3going-on/modules"
 	"golang.org/x/sys/unix"
 )
 
@@ -17,7 +18,10 @@ func Run(cmd *cobra.Command, args []string) {
 	viper.BindPFlags(cmd.Flags())
 	cfg, err := readConfig(viper.GetString("config"))
 	cobra.CheckErr(err)
-	go handleCommands()
+
+	clicks := make(chan modules.Click, 1)
+
+	go handleCommands(clicks)
 
 	fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n",
 		unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT"))
@@ -30,6 +34,18 @@ func Run(cmd *cobra.Command, args []string) {
 		fmt.Printf("%s\n[%s]", separator, strings.Join(line, ","))
 		separator = ","
 		select {
+		case click := <-clicks:
+			found := false
+			for _, module := range cfg.Modules {
+				if click.Name == module.Name {
+					found = true
+					fmt.Print(`[{"full_text": "CLICK!"},{"name": "post", "full_text": "post", "color": "#11ff11"},{"name": "date", "full_text": "22-12-01 22:54"}]`)
+				}
+			}
+			if !found {
+				fmt.Print(`[{"full_text": "ERROR!"},{"name": "post", "full_text": "post", "color": "#11ff11"},{"name": "date", "full_text": "22-12-01 22:54"}]`)
+			}
+			time.Sleep(5 * time.Second)
 		case <-time.After(time.Duration(cfg.Refresh) * time.Second):
 		}
 	}
diff --git a/modules/clicks.go b/modules/clicks.go
new file mode 100644
index 0000000..018c331
--- /dev/null
+++ b/modules/clicks.go
@@ -0,0 +1,15 @@
+// Package module implements all the modules.
+// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie>
+package modules
+
+type Click struct {
+	Name      string   `json:"name"`
+	Button    int64    `json:"button"`
+	Modifiers []string `json:"modifiers"`
+	X         int64    `json:"x"`
+	Y         int64    `json:"y"`
+	RelativeX int64    `json:"relative_x"`
+	RelativeY int64    `json:"relative_y"`
+	Width     int64    `json:"width"`
+	Height    int64    `json:"height"`
+}
diff --git a/modules/date.go b/modules/date.go
index 400d629..a846897 100644
--- a/modules/date.go
+++ b/modules/date.go
@@ -1,3 +1,5 @@
+// Package module implements all the modules.
+// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie>
 package modules
 
 import (
@@ -47,5 +49,5 @@ func (d *DateMod) Render() string {
 		}
 		d.blink = !d.blink
 	}
-	return fmt.Sprintf("{\"full_text\": \"%s\"}", now)
+	return fmt.Sprintf(`{"name": "%s", "full_text": "%s"}`, d.name, now)
 }
diff --git a/modules/modules.go b/modules/modules.go
index ea2ff25..a9f2bce 100644
--- a/modules/modules.go
+++ b/modules/modules.go
@@ -1,3 +1,5 @@
+// Package module implements all the modules.
+// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie>
 package modules
 
 import (
diff --git a/modules/text.go b/modules/text.go
index c3f27eb..e5e845e 100644
--- a/modules/text.go
+++ b/modules/text.go
@@ -1,3 +1,5 @@
+// Package module implements all the modules.
+// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie>
 package modules
 
 import (
-- 
GitLab