From 7e64db26abb05ec56427fc556f743e3d7f903f4d Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Sun, 3 Jun 2018 07:17:13 +0430 Subject: [PATCH 1/3] clean up, support text colors from config file --- docs/posts/modules/ipinfo/index.html | 3 ++ ipinfo/widget.go | 66 +++++++++++++++++++++------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/docs/posts/modules/ipinfo/index.html b/docs/posts/modules/ipinfo/index.html index f6f6bde9..703d1c35 100644 --- a/docs/posts/modules/ipinfo/index.html +++ b/docs/posts/modules/ipinfo/index.html @@ -128,6 +128,9 @@

Configuration

ipinfo:
   enabled: true
+  colors:
+   name: red
+   value: white
   position:
     top: 1
     left: 2
diff --git a/ipinfo/widget.go b/ipinfo/widget.go
index a0bd3002..ff3999a2 100644
--- a/ipinfo/widget.go
+++ b/ipinfo/widget.go
@@ -3,9 +3,13 @@ package ipinfo
 import (
 	"encoding/json"
 	"fmt"
+	"net/http"
+	"text/template"
+
+	"bytes"
+
 	"github.com/olebedev/config"
 	"github.com/senorprogrammer/wtf/wtf"
-	"net/http"
 )
 
 // Config is a pointer to the global config object
@@ -14,6 +18,9 @@ var Config *config.Config
 type Widget struct {
 	wtf.TextWidget
 	result string
+	colors struct {
+		name, value string
+	}
 }
 
 type ipinfo struct {
@@ -29,11 +36,13 @@ type ipinfo struct {
 
 func NewWidget() *Widget {
 	widget := Widget{
-		TextWidget: wtf.NewTextWidget("IPInfo", "ipinfo", false),
+		TextWidget: wtf.NewTextWidget(" IPInfo ", "ipinfo", false),
 	}
 
 	widget.View.SetWrap(true)
 
+	widget.config()
+
 	return &widget
 }
 
@@ -45,9 +54,8 @@ func (widget *Widget) Refresh() {
 	widget.UpdateRefreshedAt()
 	widget.ipinfo()
 	widget.View.Clear()
-	widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name))
 
-	fmt.Fprintf(widget.View, "%s", widget.result)
+	widget.View.SetText(widget.result)
 }
 
 //this method reads the config and calls ipinfo for ip information
@@ -75,15 +83,43 @@ func (widget *Widget) ipinfo() {
 		widget.result = fmt.Sprintf("%s", err.Error())
 		return
 	}
-	widget.result = fmt.Sprintf(
-		"[red]IP Address:[white] %s\n[red]Hostname:[white] %v\n[red]City:[white] %s\n[red]Region:[white] %s\n[red]Country:[white] %s\n[red]Coordinates:[white] %v\n[red]Postal Code:[white] %s\n[red]Organization:[white] %v",
-		info.Ip,
-		info.Hostname,
-		info.City,
-		info.Region,
-		info.Country,
-		info.Coordinates,
-		info.PostalCode,
-		info.Organization,
-	)
+
+	widget.setResult(&info)
+}
+
+// read module configs
+func (widget *Widget) config() {
+	nameColor, valueColor := Config.UString("wtf.mods.ipinfo.colors.name", "red"), Config.UString("wtf.mods.ipinfo.colors.value", "white")
+	widget.colors.name = nameColor
+	widget.colors.value = valueColor
+}
+
+func (widget *Widget) setResult(info *ipinfo) {
+	resultTemplate, _ := template.New("ipinfo_result").Parse(
+		"[{{.nameColor}}]IP Address: [{{.valueColor}}]{{.Ip}}\n" +
+			"[{{.nameColor}}]Hostname: [{{.valueColor}}]{{.Hostname}}\n" +
+			"[{{.nameColor}}]City: [{{.valueColor}}]{{.City}}\n" +
+			"[{{.nameColor}}]Region: [{{.valueColor}}]{{.Region}}\n" +
+			"[{{.nameColor}}]Country: [{{.valueColor}}]{{.Country}}\n" +
+			"[{{.nameColor}}]Coordinates: [{{.valueColor}}]{{.Coordinates}}\n" +
+			"[{{.nameColor}}]Postal Code: [{{.valueColor}}]{{.PostalCode}}\n" +
+			"[{{.nameColor}}]Organization: [{{.valueColor}}]{{.Organization}}\n",
+	)
+
+	resultBuffer := new(bytes.Buffer)
+
+	resultTemplate.Execute(resultBuffer, map[string]string{
+		"nameColor":    widget.colors.name,
+		"valueColor":   widget.colors.value,
+		"Ip":           info.Ip,
+		"Hostname":     info.Hostname,
+		"City":         info.City,
+		"Region":       info.Region,
+		"Country":      info.Country,
+		"Coordinates":  info.Coordinates,
+		"PostalCode":   info.PostalCode,
+		"Organization": info.Organization,
+	})
+
+	widget.result = resultBuffer.String()
 }

From 3f92a2f7eec14753fdc0bf4fdc96b3d9b6a2af1b Mon Sep 17 00:00:00 2001
From: Hossein Mehrabi 
Date: Sun, 3 Jun 2018 07:21:34 +0430
Subject: [PATCH 2/3] clean up

---
 ipinfo/widget.go | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/ipinfo/widget.go b/ipinfo/widget.go
index ff3999a2..8b18c0a4 100644
--- a/ipinfo/widget.go
+++ b/ipinfo/widget.go
@@ -96,14 +96,14 @@ func (widget *Widget) config() {
 
 func (widget *Widget) setResult(info *ipinfo) {
 	resultTemplate, _ := template.New("ipinfo_result").Parse(
-		"[{{.nameColor}}]IP Address: [{{.valueColor}}]{{.Ip}}\n" +
-			"[{{.nameColor}}]Hostname: [{{.valueColor}}]{{.Hostname}}\n" +
-			"[{{.nameColor}}]City: [{{.valueColor}}]{{.City}}\n" +
-			"[{{.nameColor}}]Region: [{{.valueColor}}]{{.Region}}\n" +
-			"[{{.nameColor}}]Country: [{{.valueColor}}]{{.Country}}\n" +
-			"[{{.nameColor}}]Coordinates: [{{.valueColor}}]{{.Coordinates}}\n" +
-			"[{{.nameColor}}]Postal Code: [{{.valueColor}}]{{.PostalCode}}\n" +
-			"[{{.nameColor}}]Organization: [{{.valueColor}}]{{.Organization}}\n",
+		formatableText("IP Address", "Ip") +
+			formatableText("Hostname", "Hostname") +
+			formatableText("City", "City") +
+			formatableText("Region", "Region") +
+			formatableText("Country", "Country") +
+			formatableText("Coordinates", "Coordinates") +
+			formatableText("Postal Code", "PostalCode") +
+			formatableText("Organization", "Organization"),
 	)
 
 	resultBuffer := new(bytes.Buffer)
@@ -123,3 +123,7 @@ func (widget *Widget) setResult(info *ipinfo) {
 
 	widget.result = resultBuffer.String()
 }
+
+func formatableText(key, value string) string {
+	return fmt.Sprintf("[{{.nameColor}}]%s: [{{.valueColor}}]{{.%s}}\n", key, value)
+}

From c7e0eaa3becd19a125b27f0d34ba315d5281fd4e Mon Sep 17 00:00:00 2001
From: Chris Cummer 
Date: Sat, 2 Jun 2018 21:06:23 -0700
Subject: [PATCH 3/3] Add documentatin for IPInfo colors

---
 _site/content/posts/modules/ipinfo.md | 11 +++++++++++
 docs/index.xml                        |  6 ++++--
 docs/posts/index.xml                  |  6 ++++--
 docs/posts/modules/ipinfo/index.html  | 14 +++++++++++---
 4 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/_site/content/posts/modules/ipinfo.md b/_site/content/posts/modules/ipinfo.md
index 4894706a..f1c40c79 100644
--- a/_site/content/posts/modules/ipinfo.md
+++ b/_site/content/posts/modules/ipinfo.md
@@ -26,6 +26,9 @@ None.
 
 ```yaml
 ipinfo:
+  colors:
+    name: red
+    value: white
   enabled: true
   position:
     top: 1
@@ -37,6 +40,14 @@ ipinfo:
 
 ### Attributes
 
+`colors.name` 
+The default colour for the row names.
+Values: Any X11 color name. + +`colors.value`
+The default colour for the row values.
+Values: Any X11 color name. + `enabled`
Determines whether or not this module is executed and if its data displayed onscreen.
Values: `true`, `false`. diff --git a/docs/index.xml b/docs/index.xml index ccf66010..cdae0707 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -36,8 +36,10 @@ position Defines where in the grid this module’s widget will be displa Displays your current IP address information, from ipinfo.io. Source Code wtf/ipinfo/ Required ENV Variables None. Keyboard Commands None. -Configuration ipinfo:enabled:trueposition:top:1left:2height:1width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false. -position Defines where in the grid this module’s widget will be displayed. refreshInterval How often, in seconds, this module will update its data. Values: A positive integer, 0..n. +Configuration ipinfo:colors:name:redvalue:whiteenabled:trueposition:top:1left:2height:1width:1refreshInterval:15 Attributes colors.name The default colour for the row names. Values: Any X11 color name. +colors.value The default colour for the row values. Values: Any X11 color name. +enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false. +position Defines where in the grid this module’s widget will be displayed. diff --git a/docs/posts/index.xml b/docs/posts/index.xml index 5626986c..ed7cb0bc 100644 --- a/docs/posts/index.xml +++ b/docs/posts/index.xml @@ -36,8 +36,10 @@ position Defines where in the grid this module’s widget will be displa Displays your current IP address information, from ipinfo.io. Source Code wtf/ipinfo/ Required ENV Variables None. Keyboard Commands None. -Configuration ipinfo:enabled:trueposition:top:1left:2height:1width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false. -position Defines where in the grid this module’s widget will be displayed. refreshInterval How often, in seconds, this module will update its data. Values: A positive integer, 0..n. +Configuration ipinfo:colors:name:redvalue:whiteenabled:trueposition:top:1left:2height:1width:1refreshInterval:15 Attributes colors.name The default colour for the row names. Values: Any X11 color name. +colors.value The default colour for the row values. Values: Any X11 color name. +enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false. +position Defines where in the grid this module’s widget will be displayed. diff --git a/docs/posts/modules/ipinfo/index.html b/docs/posts/modules/ipinfo/index.html index 703d1c35..82387b46 100644 --- a/docs/posts/modules/ipinfo/index.html +++ b/docs/posts/modules/ipinfo/index.html @@ -127,10 +127,10 @@

Configuration

ipinfo:
-  enabled: true
   colors:
-   name: red
-   value: white
+    name: red
+    value: white
+  enabled: true
   position:
     top: 1
     left: 2
@@ -139,6 +139,14 @@
   refreshInterval: 15

Attributes

+

colors.name
+The default colour for the row names.
+Values: Any X11 color name.

+ +

colors.value
+The default colour for the row values.
+Values: Any X11 color name.

+

enabled
Determines whether or not this module is executed and if its data displayed onscreen.
Values: true, false.