diff --git a/superhighway84.go b/superhighway84.go index 3850511..a61283e 100644 --- a/superhighway84.go +++ b/superhighway84.go @@ -3,6 +3,8 @@ package main import ( "context" "embed" + "encoding/json" + "net/http" "net/url" "os" "runtime" @@ -63,7 +65,7 @@ func main() { var articlesRoots []*models.Article TUI := tui.Init(&EMBEDFS, cfg, logger) - TUI.SetVersion(version) + TUI.SetVersion(version, getLatestVersion()) TUI.ArticlesDatasource = &articles TUI.ArticlesRoots = &articlesRoots @@ -114,3 +116,22 @@ func main() { TUI.Launch() } + +func getLatestVersion() (string) { + var client = &http.Client{Timeout: 10 * time.Second} + r, err := client.Get( + "https://api.github.com/repos/mrusme/superhighway84/releases/latest", + ) + if err != nil { + return version + } + defer r.Body.Close() + var result map[string]interface{} + json.NewDecoder(r.Body).Decode(&result) + + if val, exist := result["tag_name"]; exist == true { + return val.(string) + } + return version +} + diff --git a/tui/mainscreen.go b/tui/mainscreen.go index 2ce51c6..4075605 100644 --- a/tui/mainscreen.go +++ b/tui/mainscreen.go @@ -16,7 +16,7 @@ var HEADER_LOGO = [teal] / / / // [-][hotpink]/ __/_ _____ ___ ____/ / (_)__ _/ / _ _____ ___ __( _ )/ / /[-] [teal] _\ _\_\_\\_[-][fuchsia]\ \/ // / _ \/ -_) __/ _ \/ / _ \/ _ \ |/|/ / _ \/ // / _ /_ _/[-] [darkcyan] / / / // [-][hotpink]/___/\_,_/ .__/\__/_/ /_//_/_/\_, /_//_/__,__/\_,_/\_, /\___/ /_/[-] [dimgray]%s[-] -[hotpink] /_/ /___/ /___/[-] +[hotpink] /_/ /___/ /___/[-] [yellow]%s[-] ` var STATS_TEMPLATE = @@ -171,10 +171,22 @@ func (mainscreen *Mainscreen) SetInfo(info map[string]string) { ) } -func (mainscreen *Mainscreen) SetVersion(version string) { +func (mainscreen *Mainscreen) SetVersion(version string, versionLatest string) { + v := version + if version == "v0.0.0" { + v = "DeLorean @ 1.21 Gigawatts" + } + + l := "" + if versionLatest != version && + version != "v0.0.0" { + l = fmt.Sprintf("%s update available!", versionLatest) + } + mainscreen.Header.SetText( fmt.Sprintf(HEADER_LOGO, - version, + v, + l, ), ) } diff --git a/tui/tui.go b/tui/tui.go index 8b0798a..c863994 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -34,6 +34,7 @@ type TUI struct { Stats map[string]int64 Version string + VersionLatest string } type View interface { @@ -239,8 +240,9 @@ func (t* TUI) SetStats(peers, rateIn, rateOut, totalIn, totalOut int64) () { t.App.Draw() } -func (t* TUI) SetVersion(version string) { +func (t* TUI) SetVersion(version string, versionLatest string) { t.Version = version - t.Views["mainscreen"].(*Mainscreen).SetVersion(t.Version) + t.VersionLatest = versionLatest + t.Views["mainscreen"].(*Mainscreen).SetVersion(t.Version, t.VersionLatest) }