aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go268
1 files changed, 138 insertions, 130 deletions
diff --git a/main.go b/main.go
index 213aed6..b6954a3 100644
--- a/main.go
+++ b/main.go
@@ -4,8 +4,8 @@ import (
"context"
"deepcool-display-linux/modules"
"embed"
- "flag"
"fmt"
+ flag "github.com/spf13/pflag"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
@@ -16,16 +16,17 @@ import (
//go:embed all:frontend/dist
var assets embed.FS
+const (
+ version = "0.1.0"
+)
+
var (
- daemonFlag bool
- err error
- filename string
- tempFlag bool
- celsiusFlag bool
- usageFlag bool
- helpFlag bool
- versionFlag bool
- version = "0.1.0"
+ daemonFlag = flag.BoolP("daemon", "d", false, "Run in daemon mode")
+ modeFlag = flag.StringP("mode", "m", "", "Specify the mode (temp, usage or custom)")
+ celsiusFlag = flag.BoolP("celsius", "c", false, "Show CPU temperature in Celsius")
+ helpFlag = flag.BoolP("help", "h", false, "Show help message")
+ versionFlag = flag.BoolP("version", "v", false, "Show app version")
+ filename = flag.StringP("file", "f", "", "Specify CSV file path for pattern")
)
func printHelp() {
@@ -37,10 +38,9 @@ Usage:
Options:
-h, --help Show this help message
-d, --daemon Run in daemon mode
+ -m, --mode Specify the mode (temp, usage or custom)
-f, --file Specify CSV file path for pattern
- -t, --temp Show CPU temperature
-c, --celsius Show CPU temperature in Celsius
- -u, --usage Show CPU usage
-v, --version Show the version of the app
Modes:
@@ -56,136 +56,144 @@ For more information, visit: https://github.com/blaster4385/deepcool-display-lin
`, os.Args[0], os.Args[0], os.Args[0])
}
+func handleError(err error) {
+ fmt.Printf("Error: %v\n", err)
+ os.Exit(1)
+}
+
func main() {
- flag.BoolVar(&daemonFlag, "daemon", false, "Run as daemon")
- flag.BoolVar(&daemonFlag, "d", false, "Run as daemon")
- flag.StringVar(&filename, "file", "", "CSV file")
- flag.StringVar(&filename, "f", "", "CSV file")
- flag.BoolVar(&tempFlag, "temp", false, "Show CPU temperature")
- flag.BoolVar(&tempFlag, "t", false, "Show CPU temperature")
- flag.BoolVar(&celsiusFlag, "celsius", false, "Show CPU temperature in Celsius")
- flag.BoolVar(&celsiusFlag, "c", false, "Show CPU temperature in Celsius")
- flag.BoolVar(&usageFlag, "usage", false, "Show CPU usage")
- flag.BoolVar(&usageFlag, "u", false, "Show CPU usage")
- flag.BoolVar(&helpFlag, "help", false, "Show help message")
- flag.BoolVar(&helpFlag, "h", false, "Show help message")
- flag.BoolVar(&versionFlag, "version", false, "Show app version")
- flag.BoolVar(&versionFlag, "v", false, "Show app version")
flag.Parse()
- if helpFlag {
+ if *helpFlag {
printHelp()
- os.Exit(0)
+ return
}
-
- if versionFlag {
+ if *versionFlag {
fmt.Printf("Version: %s\n", version)
- os.Exit(0)
+ return
}
app := NewApp()
- if daemonFlag {
- ctx := context.Background()
- app.startup(ctx)
- if filename != "" {
- grid, err := app.ParseCSV(filename)
- if err != nil {
- fmt.Printf("Error parsing CSV file: %v\n", err)
- os.Exit(1)
- }
- err = app.SendPattern(grid)
- if err != nil {
- fmt.Printf("Error sending pattern: %v\n", err)
- os.Exit(1)
- }
- } else if tempFlag {
- ticker := time.NewTicker(3 * time.Second)
- defer ticker.Stop()
-
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
-
- go func() {
- for {
- select {
- case <-ctx.Done():
- return
- case <-ticker.C:
- var temp float64
- if celsiusFlag {
- temp, err = modules.GetCPUTemperature(false)
- grid, err := modules.CreateNumberGrid(int(temp), "celsius", 5)
- if err != nil {
- fmt.Printf("Error creating number grid: %v\n", err)
- os.Exit(1)
- }
- err = app.SendPattern(grid)
- if err != nil {
- fmt.Printf("Error sending pattern: %v\n", err)
- os.Exit(1)
- }
- } else {
- temp, err = modules.GetCPUTemperature(true)
- grid, err := modules.CreateNumberGrid(int(temp), "fahrenheit", 5)
- if err != nil {
- fmt.Printf("Error creating number grid: %v\n", err)
- os.Exit(1)
- }
- err = app.SendPattern(grid)
- }
- }
+
+ if *daemonFlag {
+ daemonMode(app)
+ } else {
+ guiMode(app)
+ }
+}
+
+func daemonMode(app *App) {
+ contextBackground, contextCancel := context.WithCancel(context.Background())
+ defer contextCancel()
+
+ if *modeFlag == "custom" {
+ if *filename == "" {
+ handleError(fmt.Errorf("filename is required for custom mode"))
+ }
+ handleDaemonPattern(app, *filename, contextBackground)
+ return
+ }
+ if *modeFlag == "temp" {
+ daemonTemperatureDisplay(app, contextBackground)
+ return
+ }
+ if *modeFlag == "usage" {
+ daemonUsageDisplay(app, contextBackground)
+ return
+ }
+
+ select {}
+}
+
+func handleDaemonPattern(app *App, filename string, ctx context.Context) {
+ grid, err := app.ParseCSV(filename)
+ if err != nil {
+ handleError(fmt.Errorf("parsing CSV file: %w", err))
+ }
+ err = app.SendPattern(grid)
+ if err != nil {
+ handleError(fmt.Errorf("sending pattern: %w", err))
+ }
+ select {}
+}
+
+func daemonTemperatureDisplay(app *App, ctx context.Context) {
+ ticker := time.NewTicker(3 * time.Second)
+ defer ticker.Stop()
+
+ go func() {
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ temp, err := modules.GetCPUTemperature(!*celsiusFlag)
+ if err != nil {
+ handleError(fmt.Errorf("getting temperature: %w", err))
+ }
+ var tempSymbol string
+ if *celsiusFlag {
+ tempSymbol = "celsius"
+ } else {
+ tempSymbol = "fahrenheit"
}
- }()
- } else if usageFlag {
- ticker := time.NewTicker(3 * time.Second)
- defer ticker.Stop()
-
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
-
- go func() {
- for {
- select {
- case <-ctx.Done():
- return
- case <-ticker.C:
- usage, err := modules.GetCPUUsage()
- if err != nil {
- fmt.Printf("Error getting CPU usage: %v\n", err)
- os.Exit(1)
- }
- grid, err := modules.CreateNumberGrid(int(usage), "percent", 5)
- if err != nil {
- fmt.Printf("Error creating number grid: %v\n", err)
- os.Exit(1)
- }
- err = app.SendPattern(grid)
- if err != nil {
- fmt.Printf("Error sending pattern: %v\n", err)
- os.Exit(1)
- }
- }
+ grid, err := modules.CreateNumberGrid(int(temp), tempSymbol, 5)
+ if err != nil {
+ handleError(fmt.Errorf("creating temperature grid: %w", err))
}
- }()
+ err = app.SendPattern(grid)
+ if err != nil {
+ handleError(fmt.Errorf("sending pattern: %w", err))
+ }
+ }
}
- select {}
- } else {
- err := wails.Run(&options.App{
- Title: "deepcool-display-linux",
- Width: 1024,
- Height: 768,
- AssetServer: &assetserver.Options{
- Assets: assets,
- },
- BackgroundColour: &options.RGBA{R: 40, G: 40, B: 40, A: 1},
- OnStartup: app.startup,
- Bind: []interface{}{
- app,
- },
- })
- if err != nil {
- fmt.Printf("Error starting application: %v\n", err)
- os.Exit(1)
+ }()
+ select {}
+}
+
+func daemonUsageDisplay(app *App, ctx context.Context) {
+ ticker := time.NewTicker(3 * time.Second)
+ defer ticker.Stop()
+
+ go func() {
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ usage, err := modules.GetCPUUsage()
+ if err != nil {
+ handleError(fmt.Errorf("getting CPU usage: %w", err))
+ }
+ grid, err := modules.CreateNumberGrid(int(usage), "percent", 5)
+ if err != nil {
+ handleError(fmt.Errorf("creating usage grid: %w", err))
+ }
+ err = app.SendPattern(grid)
+ if err != nil {
+ handleError(fmt.Errorf("sending pattern: %w", err))
+ }
+ }
}
+ }()
+ select {}
+}
+
+func guiMode(app *App) {
+ err := wails.Run(&options.App{
+ Title: "deepcool-display-linux",
+ Width: 1024,
+ Height: 768,
+ AssetServer: &assetserver.Options{
+ Assets: assets,
+ },
+ BackgroundColour: &options.RGBA{R: 40, G: 40, B: 40, A: 1},
+ OnStartup: app.startup,
+ Bind: []interface{}{
+ app,
+ },
+ })
+ if err != nil {
+ handleError(fmt.Errorf("starting application: %w", err))
}
}