From 5671b3312662105b9ee5713357a4d5d3ee9f2951 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sat, 24 Dec 2022 18:48:21 +0100 Subject: [PATCH] Disable leftover linter findings for view/v1 --- internal/view/v1/api.go | 1 + internal/view/v1/cmd.go | 6 ++- internal/view/v1/format.go | 92 +++++++++++++++++++------------------- internal/view/v1/icons.go | 1 + internal/view/v1/locale.go | 4 +- internal/view/v1/view1.go | 15 +------ 6 files changed, 57 insertions(+), 62 deletions(-) diff --git a/internal/view/v1/api.go b/internal/view/v1/api.go index 5c68abc..84fcb64 100644 --- a/internal/view/v1/api.go +++ b/internal/view/v1/api.go @@ -1,3 +1,4 @@ +//nolint:forbidigo,funlen,nestif,goerr113,gocognit,cyclop package v1 import ( diff --git a/internal/view/v1/cmd.go b/internal/view/v1/cmd.go index 4d5a5c2..631107c 100644 --- a/internal/view/v1/cmd.go +++ b/internal/view/v1/cmd.go @@ -1,11 +1,12 @@ // This code represents wttr.in view v1. // It is based on wego (github.com/schachmat/wego) from which it diverged back in 2016. +//nolint:forbidigo,funlen,gocognit,cyclop package v1 import ( - _ "crypto/sha512" "encoding/json" + "errors" "flag" "fmt" "io/ioutil" @@ -88,7 +89,8 @@ func (g *global) init() { g.config.Imperial = false g.config.Lang = "en" err := g.configload() - if _, ok := err.(*os.PathError); ok { + var pathError *os.PathError + if errors.Is(err, pathError) { log.Printf("No config file found. Creating %s ...", g.configpath) if err2 := g.configsave(); err2 != nil { log.Fatal(err2) diff --git a/internal/view/v1/format.go b/internal/view/v1/format.go index dcfa6b8..07951ce 100644 --- a/internal/view/v1/format.go +++ b/internal/view/v1/format.go @@ -1,3 +1,4 @@ +//nolint:funlen,nestif,cyclop,gocognit,gocyclo package v1 import ( @@ -32,6 +33,7 @@ func windDir() map[string]string { func (g *global) formatTemp(c cond) string { color := func(temp int, explicitPlus bool) string { var col int + //nolint:dupl if !g.config.Inverse { // Extremely cold temperature must be shown with violet // because dark blue is too dark @@ -174,46 +176,6 @@ func (g *global) formatTemp(c cond) string { } func (g *global) formatWind(c cond) string { - windInRightUnits := func(spd int) int { - if g.config.WindMS { - spd = (spd * 1000) / 3600 - } else if g.config.Imperial { - spd = (spd * 1000) / 1609 - } - - return spd - } - color := func(spd int) string { - col := 46 - switch spd { - case 1, 2, 3: - col = 82 - case 4, 5, 6: - col = 118 - case 7, 8, 9: - col = 154 - case 10, 11, 12: - col = 190 - case 13, 14, 15: - col = 226 - case 16, 17, 18, 19: - col = 220 - case 20, 21, 22, 23: - col = 214 - case 24, 25, 26, 27: - col = 208 - case 28, 29, 30, 31: - col = 202 - default: - if spd > 0 { - col = 196 - } - } - spd = windInRightUnits(spd) - - return fmt.Sprintf("\033[38;5;%03dm%d\033[0m", col, spd) - } - unitWindString := unitWind(0, g.config.Lang) if g.config.WindMS { unitWindString = unitWind(2, g.config.Lang) @@ -221,14 +183,12 @@ func (g *global) formatWind(c cond) string { unitWindString = unitWind(1, g.config.Lang) } - // if (config.Lang == "sl") { - // hyphen = "-" - // } hyphen := "-" - cWindGustKmph := color(c.WindGustKmph) - cWindspeedKmph := color(c.WindspeedKmph) - if windInRightUnits(c.WindGustKmph) > windInRightUnits(c.WindspeedKmph) { + cWindGustKmph := speedToColor(c.WindGustKmph, windInRightUnits(c.WindGustKmph, g.config.WindMS, g.config.Imperial)) + cWindspeedKmph := speedToColor(c.WindspeedKmph, windInRightUnits(c.WindspeedKmph, g.config.WindMS, g.config.Imperial)) + if windInRightUnits(c.WindGustKmph, g.config.WindMS, g.config.Imperial) > + windInRightUnits(c.WindspeedKmph, g.config.WindMS, g.config.Imperial) { return g.pad( fmt.Sprintf("%s %s%s%s %s", windDir()[c.Winddir16Point], cWindspeedKmph, hyphen, cWindGustKmph, unitWindString), 15) @@ -237,6 +197,46 @@ func (g *global) formatWind(c cond) string { return g.pad(fmt.Sprintf("%s %s %s", windDir()[c.Winddir16Point], cWindspeedKmph, unitWindString), 15) } +func windInRightUnits(spd int, windMS, imperial bool) int { + if windMS { + spd = (spd * 1000) / 3600 + } else if imperial { + spd = (spd * 1000) / 1609 + } + + return spd +} + +func speedToColor(spd, spdConverted int) string { + col := 46 + switch spd { + case 1, 2, 3: + col = 82 + case 4, 5, 6: + col = 118 + case 7, 8, 9: + col = 154 + case 10, 11, 12: + col = 190 + case 13, 14, 15: + col = 226 + case 16, 17, 18, 19: + col = 220 + case 20, 21, 22, 23: + col = 214 + case 24, 25, 26, 27: + col = 208 + case 28, 29, 30, 31: + col = 202 + default: + if spd > 0 { + col = 196 + } + } + + return fmt.Sprintf("\033[38;5;%03dm%d\033[0m", col, spdConverted) +} + func (g *global) formatVisibility(c cond) string { if g.config.Imperial { c.VisibleDistKM = (c.VisibleDistKM * 621) / 1000 diff --git a/internal/view/v1/icons.go b/internal/view/v1/icons.go index d92eb63..c1f8ce2 100644 --- a/internal/view/v1/icons.go +++ b/internal/view/v1/icons.go @@ -1,5 +1,6 @@ package v1 +//nolint:funlen func getIcon(name string) []string { icon := map[string][]string{ "iconUnknown": { diff --git a/internal/view/v1/locale.go b/internal/view/v1/locale.go index b40219d..3ce1b81 100644 --- a/internal/view/v1/locale.go +++ b/internal/view/v1/locale.go @@ -1,5 +1,6 @@ package v1 +//nolint:funlen func locale() map[string]string { return map[string]string{ "af": "af_ZA", @@ -75,6 +76,7 @@ func locale() map[string]string { } } +//nolint:funlen func localizedCaption() map[string]string { return map[string]string{ "af": "Weer verslag vir:", @@ -151,7 +153,7 @@ func localizedCaption() map[string]string { } } -//nolint:misspell +//nolint:misspell,funlen func daytimeTranslation() map[string][]string { return map[string][]string{ "af": {"Oggend", "Middag", "Vroegaand", "Laatnag"}, diff --git a/internal/view/v1/view1.go b/internal/view/v1/view1.go index 4fd0c19..d839659 100644 --- a/internal/view/v1/view1.go +++ b/internal/view/v1/view1.go @@ -11,15 +11,15 @@ func slotTimes() []int { return []int{9 * 60, 12 * 60, 18 * 60, 22 * 60} } +//nolint:funlen,gocognit,cyclop func (g *global) printDay(w weather) ([]string, error) { var ( - ret []string + ret = []string{} dateName string names string ) hourly := w.Hourly - ret = make([]string, 5) for i := range ret { ret[i] = "│" } @@ -82,17 +82,6 @@ func (g *global) printDay(w weather) ([]string, error) { dateName = lctime.Strftime("%b%d日%A", d) } } - // appendSide := 0 - // // for utf8.RuneCountInString(dateName) <= dateWidth { - // for runewidth.StringWidth(dateName) <= dateWidth { - // if appendSide == 1 { - // dateName = dateName + " " - // appendSide = 0 - // } else { - // dateName = " " + dateName - // appendSide = 1 - // } - // } dateFmt := "─" + justifyCenter(dateName, 12) + "├"