102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package output
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.leaktechnologies.dev/Leak_Technologies/Skyfeed/internal/weather"
|
|
)
|
|
|
|
// Theme defines simple ANSI colour codes for terminal output.
|
|
var Theme = struct {
|
|
Reset string
|
|
White string
|
|
Cyan string
|
|
Yellow string
|
|
Blue string
|
|
Green string
|
|
Red string
|
|
Magenta string
|
|
}{
|
|
Reset: "\033[0m",
|
|
White: "\033[97m",
|
|
Cyan: "\033[96m",
|
|
Yellow: "\033[93m",
|
|
Blue: "\033[94m",
|
|
Green: "\033[92m",
|
|
Red: "\033[91m",
|
|
Magenta: "\033[95m",
|
|
}
|
|
|
|
// WeatherIcon maps normalized condition keywords to simple glyphs.
|
|
func WeatherIcon(condition string) string {
|
|
condition = strings.ToLower(condition)
|
|
switch {
|
|
case strings.Contains(condition, "sunny"):
|
|
return "☀"
|
|
case strings.Contains(condition, "clear"):
|
|
return "🌙"
|
|
case strings.Contains(condition, "partly"):
|
|
return "⛅"
|
|
case strings.Contains(condition, "cloud"):
|
|
return "☁"
|
|
case strings.Contains(condition, "rain"):
|
|
return "🌧"
|
|
case strings.Contains(condition, "snow"):
|
|
return "❄"
|
|
case strings.Contains(condition, "mixed"):
|
|
return "🌨"
|
|
case strings.Contains(condition, "fog"):
|
|
return "🌫"
|
|
case strings.Contains(condition, "thunder"):
|
|
return "⛈"
|
|
default:
|
|
return "❔"
|
|
}
|
|
}
|
|
|
|
// FormatWeatherCLI returns a full formatted string for terminal output.
|
|
func FormatWeatherCLI(data weather.WeatherData, colored bool) string {
|
|
icon := WeatherIcon(data.Condition)
|
|
temp := fmt.Sprintf("%.1f°C", data.Temperature)
|
|
ts := parseTimestamp(data.Timestamp)
|
|
|
|
if colored {
|
|
return fmt.Sprintf("%s%s %s%s %s%s%s\n%sCondition:%s %s\n%sHumidity:%s %s%% %sWind:%s %s %s\n%sPressure:%s %s kPa %sUpdated:%s %s%s",
|
|
Theme.Cyan, icon, Theme.White, data.Station,
|
|
Theme.Yellow, temp, Theme.Reset,
|
|
Theme.Blue, Theme.Reset, data.Condition,
|
|
Theme.Blue, Theme.Reset, data.Humidity,
|
|
Theme.Blue, Theme.Reset, data.WindDir, data.WindSpeed,
|
|
Theme.Blue, Theme.Reset, data.Pressure,
|
|
Theme.Blue, Theme.Reset, ts.Format("15:04 MST"), Theme.Reset,
|
|
)
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s %s\nCondition: %s\nHumidity: %s%% Wind: %s %s\nPressure: %s kPa Updated: %s",
|
|
icon, data.Station, temp,
|
|
data.Condition,
|
|
data.Humidity, data.WindDir, data.WindSpeed,
|
|
data.Pressure, ts.Format("15:04 MST"),
|
|
)
|
|
}
|
|
|
|
// parseTimestamp safely parses an RFC3339 or other timestamp format.
|
|
func parseTimestamp(ts string) time.Time {
|
|
if ts == "" {
|
|
return time.Now()
|
|
}
|
|
t, err := time.Parse(time.RFC3339, ts)
|
|
if err != nil {
|
|
return time.Now()
|
|
}
|
|
return t
|
|
}
|
|
|
|
// FormatForUI produces a compact version for GUI or embedded display.
|
|
func FormatForUI(data weather.WeatherData) string {
|
|
icon := WeatherIcon(data.Condition)
|
|
return fmt.Sprintf("%s %s %.1f°C — %s", icon, data.Station, data.Temperature, data.Condition)
|
|
}
|