Skip to content

Commit

Permalink
Merge pull request #3 from eumel8/feat/nightmode
Browse files Browse the repository at this point in the history
set nightmode and fix error rate
  • Loading branch information
eumel8 authored Sep 14, 2024
2 parents 443f472 + 7de9151 commit 321eaad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module carbonapp

go 1.22.1
go 1.23.1

require (
fyne.io/fyne/v2 v2.4.5
Expand Down
75 changes: 60 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
// "fyne.io/fyne/v2/widget"
)

const (
defaultPullTime = 60
nightStart = 21
nightEnd = 6
labelTextSize = 120 // or 96
ecoMetricLow = 40
ecoMetricHigh = 80
modeFullScreen = true
)

type Config struct {
Expand All @@ -31,10 +40,16 @@ type myTheme struct{}

var _ fyne.Theme = (*myTheme)(nil)

// collect display size to set font size
// "github.com/kbinani/screenshot"
// bounds := screenshot.GetDisplayBounds(0)
// screenWidth := bounds.Dx()
// screenHeight := bounds.Dy()

func GetConfig() (Config, error) {
pullTime, err := strconv.Atoi(os.Getenv("PULL_DURATION"))
if err != nil {
pullTime = 60
pullTime = defaultPullTime
}
return Config{
prometheusURL: os.Getenv("PROMETHEUS_URL"),
Expand Down Expand Up @@ -70,7 +85,6 @@ func (m myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
}

func (c *Config) GetCarbonMetric() (int, error) {

if c.prometheusURL == "" {
return 0, fmt.Errorf("PROMETHEUS_URL environment variable is not set")
}
Expand Down Expand Up @@ -113,42 +127,73 @@ func (c *Config) GetCarbonMetric() (int, error) {

func (c *Config) CarbonColor() (color.Color, error) {

carbonColor := color.RGBA{0, 255, 0, 255}
// default color grey
carbonColor := color.RGBA{125, 125, 125, 255}
carbonMetric, err := c.GetCarbonMetric()

if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
return color.RGBA{}, err
}

if carbonMetric <= 40 {
carbonColor = color.RGBA{255, 0, 0, 255}
} else if carbonMetric > 40 && carbonMetric <= 80 {
carbonColor = color.RGBA{255, 255, 0, 255}
if carbonMetric <= ecoMetricLow && carbonMetric > 0 {
if isNight() {
// dark red/brown
carbonColor = color.RGBA{140, 0, 0, 255}
} else {
// red
carbonColor = color.RGBA{255, 0, 0, 255}
}
} else if carbonMetric > ecoMetricLow && carbonMetric <= ecoMetricHigh {
if isNight() {
// dark yellow
carbonColor = color.RGBA{175, 175, 0, 200}
} else {
// light yellow
carbonColor = color.RGBA{255, 255, 0, 255}
}
} else {
if isNight() {
// dark green
carbonColor = color.RGBA{0, 190, 0, 255}
} else {
// light green
carbonColor = color.RGBA{0, 255, 0, 255}
}
}

return carbonColor, nil
}

func main() {
// find out if it is night to dim the display
func isNight() bool {
now := time.Now()
hour := now.Hour()
return hour >= nightStart || hour < nightEnd
}

func main() {
c, err := GetConfig()
if err != nil {
fmt.Printf("Error reading config: %v\n", err)
return
}
iconResource, err := fyne.LoadResourceFromPath("icon.png")
if err != nil {
fmt.Printf("Failed to load icon", err)
return
}

carbonApp := app.New()
carbonApp.SetIcon(iconResource)
carbonWindow := carbonApp.NewWindow("Carbon-App")
carbonWindow.SetFullScreen(true)
carbonWindow.SetFullScreen(modeFullScreen)

mainLabel := canvas.NewText("Show the current carbon emission", color.White)
mainContent := container.NewVBox(mainLabel)

go func() {
for {
time.Sleep(c.pullPeriod)
carbonApp.Settings().SetTheme(&myTheme{})

carbonMetric, err := c.GetCarbonMetric()
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
Expand All @@ -158,19 +203,19 @@ func main() {
timeLabel.Alignment = fyne.TextAlignCenter
carbonLabel := canvas.NewText(fmt.Sprintf("%d ", carbonMetric), color.Black)
carbonLabel.TextStyle.Bold = true
carbonLabel.TextSize = 96
carbonLabel.TextSize = labelTextSize
carbonLabel.Alignment = fyne.TextAlignCenter
content := container.NewVBox(timeLabel, carbonLabel)
carbonLabel.Refresh()
timeLabel.Refresh()

carbonWindow.SetContent(content)
carbonWindow.Canvas().Refresh(content)
carbonWindow.Canvas().SetOnTypedKey(func(keyEvent *fyne.KeyEvent) {
if keyEvent.Name == fyne.KeyEscape {
carbonApp.Quit()
}
})
time.Sleep(c.pullPeriod)
}
}()
carbonWindow.SetContent(mainContent)
Expand Down

0 comments on commit 321eaad

Please sign in to comment.