From 50541ca317e2ae647fc83d25e38825bb5a120296 Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 1 Feb 2026 22:15:14 +0200 Subject: Add initial TUI --- main.go | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index c5008be..268f8f9 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// main.go - Main application entry point +// main.go - Main application package main import ( @@ -6,13 +6,22 @@ import ( "flag" "fmt" "os" + "os/signal" + "syscall" + "time" ) func main() { - location := flag.String("place", "Helsinki", "Location for the forecast") + var ( + location = flag.String("place", "Helsinki", "Location for the forecast") + tuiMode = flag.Bool("tui", true, "Run in TUI mode") + jsonMode = flag.Bool("json", false, "Output as JSON") + days = flag.Int("days", 1, "Number of days to forecast (1-3)") + refresh = flag.Int("refresh", 0, "Refresh interval in minutes (0 for no refresh)") + ) flag.Parse() - // Create client and get forecast + // Get initial forecast client := NewFMIClient() response, err := client.GetForecast(*location) if err != nil { @@ -20,11 +29,36 @@ func main() { os.Exit(1) } - // Output JSON - jsonData, err := json.MarshalIndent(response, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err) - os.Exit(1) + // JSON output mode + if *jsonMode { + jsonData, err := json.MarshalIndent(response, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err) + os.Exit(1) + } + fmt.Println(string(jsonData)) + return + } + + // TUI mode + if *tuiMode { + model := NewModel(response, *location, *days) + if *refresh > 0 { + model.SetRefreshInterval(time.Duration(*refresh) * time.Minute) + } + + // Handle interrupts + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-sigChan + model.Quit() + }() + + // Run the TUI + if err := model.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Error running TUI: %v\n", err) + os.Exit(1) + } } - fmt.Println(string(jsonData)) } -- cgit v1.3-1-g0d28