mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
clean up, support text colors from config file
This commit is contained in:
parent
f90dbdaed7
commit
7e64db26ab
@ -128,6 +128,9 @@
|
||||
<h2 id="configuration">Configuration</h2>
|
||||
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml">ipinfo<span class="p">:</span><span class="w">
|
||||
</span><span class="w"> </span>enabled<span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||||
</span><span class="w"> </span>colors<span class="p">:</span><span class="w">
|
||||
</span><span class="w"> </span>name<span class="p">:</span> red<span class="w">
|
||||
</span><span class="w"> </span>value<span class="p">:</span> white<span class="w">
|
||||
</span><span class="w"> </span>position<span class="p">:</span><span class="w">
|
||||
</span><span class="w"> </span>top<span class="p">:</span><span class="w"> </span><span class="m">1</span><span class="w">
|
||||
</span><span class="w"> </span>left<span class="p">:</span><span class="w"> </span><span class="m">2</span><span class="w">
|
||||
|
@ -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 {
|
||||
@ -34,6 +41,8 @@ func NewWidget() *Widget {
|
||||
|
||||
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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user