Add Pi-hole blocked percentage and date widgets

This commit is contained in:
2025-12-22 20:45:55 +01:00
parent 2315ac2ce7
commit 7b6a8bbaab
7 changed files with 111 additions and 7 deletions

42
widgets/date.go Normal file
View 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
View 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
}