Add Pi-hole blocked percentage and date widgets
This commit is contained in:
parent
2315ac2ce7
commit
7b6a8bbaab
@ -2,7 +2,6 @@ package pihole
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Version struct {
|
||||
@ -38,7 +37,7 @@ func (ph *PiHConnector) Version() Version {
|
||||
|
||||
err := json.Unmarshal(ph.get("info/version"), version)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
version = &Version{}
|
||||
}
|
||||
return *version
|
||||
}
|
||||
|
||||
@ -41,6 +41,9 @@ func (ph *PiHConnector) post(endpoint string, body io.Reader) []byte {
|
||||
}
|
||||
|
||||
func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byte {
|
||||
if ph.Host == "" {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
var requestString = "http://" + ph.Host + "/api/" + endpoint
|
||||
|
||||
client := &http.Client{}
|
||||
@ -67,7 +70,12 @@ func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byt
|
||||
return respBody
|
||||
}
|
||||
|
||||
var connector *PiHConnector
|
||||
|
||||
func Connect() PiHConnector {
|
||||
if connector != nil {
|
||||
return *connector
|
||||
}
|
||||
cfg := &configFile{}
|
||||
config.LoadConfig(cfg)
|
||||
client := &http.Client{}
|
||||
@ -93,8 +101,9 @@ func Connect() PiHConnector {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return PiHConnector{
|
||||
connector = &PiHConnector{
|
||||
Host: cfg.Pihole.Host,
|
||||
Session: s.Session,
|
||||
}
|
||||
return *connector
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ package pihole
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Summary struct {
|
||||
@ -35,7 +34,7 @@ func (ph *PiHConnector) Summary() Summary {
|
||||
|
||||
err := json.Unmarshal(ph.get("stats/summary"), summary)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
summary = &Summary{}
|
||||
}
|
||||
return *summary
|
||||
}
|
||||
|
||||
@ -8,14 +8,14 @@ import (
|
||||
|
||||
func LoadConfig(config interface{}) {
|
||||
if err := toml.Unmarshal(readFile("config.toml"), config); err != nil {
|
||||
panic(err)
|
||||
config = nil
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(path string) []byte {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return make([]byte, 0)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
19
main.go
19
main.go
@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/mum4k/termdash"
|
||||
"github.com/mum4k/termdash/align"
|
||||
"github.com/mum4k/termdash/cell"
|
||||
"github.com/mum4k/termdash/container"
|
||||
"github.com/mum4k/termdash/container/grid"
|
||||
@ -32,10 +33,12 @@ func initWidgets(ctx context.Context, term *tcell.Terminal) {
|
||||
|
||||
widgets.Create(ctx, term,
|
||||
widgets.New("Clock", widgets.Clock()),
|
||||
widgets.New("Date", widgets.Date()),
|
||||
widgets.New("ChangeTitle", widgets.TextInput(titleUpdate, textinput.Label("Update Title: "), textinput.PlaceHolder("New Title"))),
|
||||
widgets.New("Wifi", widgets.WifiQRCode()),
|
||||
widgets.New("Docker", widgets.DockerList()),
|
||||
widgets.New("PiHole", widgets.PiholeStats()),
|
||||
widgets.New("PiHoleBlocked", widgets.PiholeBlocked()),
|
||||
)
|
||||
}
|
||||
|
||||
@ -56,11 +59,20 @@ func layout() []container.Option {
|
||||
grid.ColWidthPerc(20,
|
||||
grid.RowHeightFixed(8,
|
||||
grid.Widget(widgets.Get["Clock"],
|
||||
container.AlignHorizontal(align.HorizontalCenter),
|
||||
container.BorderTitle("Time"),
|
||||
container.Border(linestyle.Light),
|
||||
container.BorderColor(cell.ColorWhite),
|
||||
),
|
||||
),
|
||||
grid.RowHeightFixed(3,
|
||||
grid.Widget(widgets.Get["Date"],
|
||||
container.AlignHorizontal(align.HorizontalCenter),
|
||||
container.BorderTitle("Date"),
|
||||
container.Border(linestyle.Light),
|
||||
container.BorderColor(cell.ColorWhite),
|
||||
),
|
||||
),
|
||||
grid.RowHeightFixed(8,
|
||||
grid.Widget(widgets.Get["PiHole"],
|
||||
container.BorderTitle("pi-hole"),
|
||||
@ -68,6 +80,13 @@ func layout() []container.Option {
|
||||
container.BorderColor(cell.ColorWhite),
|
||||
),
|
||||
),
|
||||
grid.RowHeightFixed(25,
|
||||
grid.Widget(widgets.Get["PiHoleBlocked"],
|
||||
container.BorderTitle("pi-hole (Blocked Percent)"),
|
||||
container.Border(linestyle.Light),
|
||||
container.BorderColor(cell.ColorWhite),
|
||||
),
|
||||
),
|
||||
grid.RowHeightPerc(85,
|
||||
grid.Widget(widgets.Get["empty"]),
|
||||
),
|
||||
|
||||
42
widgets/date.go
Normal file
42
widgets/date.go
Normal file
@ -0,0 +1,42 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"ArinDash/util"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
"github.com/mum4k/termdash/widgets/text"
|
||||
)
|
||||
|
||||
type DateOptions struct {
|
||||
}
|
||||
|
||||
func Date() DateOptions {
|
||||
widgetOptions["DateOptions"] = createDate
|
||||
return DateOptions{}
|
||||
}
|
||||
|
||||
func createDate(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
widget := util.PanicOnErrorWithResult(text.New())
|
||||
|
||||
go util.Periodic(ctx, 1*time.Second, func() error {
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
now := time.Now()
|
||||
if err := widget.Write(now.Format("Monday 2.1.2006"), text.WriteReplace()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return widget
|
||||
}
|
||||
36
widgets/piholeblocked.go
Normal file
36
widgets/piholeblocked.go
Normal file
@ -0,0 +1,36 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"ArinDash/apis/pihole"
|
||||
"ArinDash/util"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/mum4k/termdash/cell"
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
"github.com/mum4k/termdash/widgets/donut"
|
||||
)
|
||||
|
||||
type PiholeBlockedOptions struct {
|
||||
}
|
||||
|
||||
func PiholeBlocked() PiholeBlockedOptions {
|
||||
widgetOptions["PiholeBlockedOptions"] = createPiholeBlocked
|
||||
return PiholeBlockedOptions{}
|
||||
}
|
||||
|
||||
func createPiholeBlocked(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
widget := util.PanicOnErrorWithResult(donut.New(donut.CellOpts(cell.FgColor(cell.ColorRed), cell.Blink()), donut.HolePercent(20), donut.ShowTextProgress()))
|
||||
|
||||
ph := pihole.Connect()
|
||||
go util.Periodic(ctx, 1*time.Minute, func() error {
|
||||
summary := ph.Summary()
|
||||
if err := widget.Percent(int(summary.Queries.PercentBlocked)); err != nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return widget
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user