wttr.in/internal/view/v1/view1.go

131 lines
4.6 KiB
Go
Raw Normal View History

2022-12-23 15:25:26 +00:00
package v1
2022-06-11 16:10:07 +00:00
import (
"math"
"time"
"github.com/klauspost/lctime"
)
2022-12-23 19:55:30 +00:00
func slotTimes() []int {
return []int{9 * 60, 12 * 60, 18 * 60, 22 * 60}
}
2022-06-11 16:10:07 +00:00
//nolint:funlen,gocognit,cyclop
2022-12-24 17:10:20 +00:00
func (g *global) printDay(w weather) ([]string, error) {
2022-12-23 16:02:39 +00:00
var (
ret = []string{}
2022-12-23 16:02:39 +00:00
dateName string
names string
)
2022-06-11 16:10:07 +00:00
hourly := w.Hourly
for i := range ret {
ret[i] = "│"
}
// find hourly data which fits the desired times of day best
var slots [slotcount]cond
for _, h := range hourly {
c := int(math.Mod(float64(h.Time), 100)) + 60*(h.Time/100)
for i, s := range slots {
2022-12-23 19:55:30 +00:00
if math.Abs(float64(c-slotTimes()[i])) < math.Abs(float64(s.Time-slotTimes()[i])) {
2022-06-11 16:10:07 +00:00
h.Time = c
slots[i] = h
}
}
}
2022-12-23 19:55:30 +00:00
if g.config.RightToLeft {
2022-06-11 16:10:07 +00:00
slots[0], slots[3] = slots[3], slots[0]
slots[1], slots[2] = slots[2], slots[1]
}
for i, s := range slots {
2022-12-23 19:55:30 +00:00
if g.config.Narrow {
2022-06-11 16:10:07 +00:00
if i == 0 || i == 2 {
continue
}
}
2022-12-23 19:55:30 +00:00
ret = g.formatCond(ret, s, false)
2022-06-11 16:10:07 +00:00
for i := range ret {
2022-12-24 17:10:20 +00:00
ret[i] += "│"
2022-06-11 16:10:07 +00:00
}
}
d, _ := time.Parse("2006-01-02", w.Date)
// dateFmt := "┤ " + d.Format("Mon 02. Jan") + " ├"
2022-12-23 19:55:30 +00:00
if val, ok := locale()[g.config.Lang]; ok {
2022-12-24 17:10:20 +00:00
err := lctime.SetLocale(val)
if err != nil {
return nil, err
}
2022-06-11 16:10:07 +00:00
} else {
2022-12-24 17:10:20 +00:00
err := lctime.SetLocale("en_US")
if err != nil {
return nil, err
}
2022-06-11 16:10:07 +00:00
}
2022-12-23 16:02:39 +00:00
2022-12-23 19:55:30 +00:00
if g.config.RightToLeft {
2022-06-11 16:10:07 +00:00
dow := lctime.Strftime("%a", d)
day := lctime.Strftime("%d", d)
month := lctime.Strftime("%b", d)
dateName = reverse(month) + " " + day + " " + reverse(dow)
} else {
dateName = lctime.Strftime("%a %d %b", d)
2022-12-23 19:55:30 +00:00
if g.config.Lang == "ko" {
2022-06-11 16:10:07 +00:00
dateName = lctime.Strftime("%b %d일 %a", d)
}
2022-12-23 19:55:30 +00:00
if g.config.Lang == "zh" || g.config.Lang == "zh-tw" || g.config.Lang == "zh-cn" {
2022-06-11 16:10:07 +00:00
dateName = lctime.Strftime("%b%d日%A", d)
}
}
dateFmt := "┤" + justifyCenter(dateName, 12) + "├"
2022-12-23 19:55:30 +00:00
trans := daytimeTranslation()["en"]
if t, ok := daytimeTranslation()[g.config.Lang]; ok {
2022-06-11 16:10:07 +00:00
trans = t
}
2022-12-23 19:55:30 +00:00
if g.config.Narrow {
2022-06-11 16:10:07 +00:00
names := "│ " + justifyCenter(trans[1], 16) +
"└──────┬──────┘" + justifyCenter(trans[3], 16) + " │"
ret = append([]string{
" ┌─────────────┐ ",
"┌───────────────────────" + dateFmt + "───────────────────────┐",
names,
2022-12-23 15:35:30 +00:00
"├──────────────────────────────┼──────────────────────────────┤",
},
2022-06-11 16:10:07 +00:00
ret...)
return append(ret,
2022-12-24 17:10:20 +00:00
"└──────────────────────────────┴──────────────────────────────┘"),
nil
2022-06-11 16:10:07 +00:00
}
2022-12-23 19:55:30 +00:00
if g.config.RightToLeft {
2022-06-11 16:10:07 +00:00
names = "│" + justifyCenter(trans[3], 29) + "│ " + justifyCenter(trans[2], 16) +
"└──────┬──────┘" + justifyCenter(trans[1], 16) + " │" + justifyCenter(trans[0], 29) + "│"
} else {
names = "│" + justifyCenter(trans[0], 29) + "│ " + justifyCenter(trans[1], 16) +
"└──────┬──────┘" + justifyCenter(trans[2], 16) + " │" + justifyCenter(trans[3], 29) + "│"
}
2022-12-23 19:05:30 +00:00
//nolint:lll
2022-06-11 16:10:07 +00:00
ret = append([]string{
" ┌─────────────┐ ",
"┌──────────────────────────────┬───────────────────────" + dateFmt + "───────────────────────┬──────────────────────────────┐",
names,
2022-12-23 15:35:30 +00:00
"├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤",
},
2022-06-11 16:10:07 +00:00
ret...)
2022-12-23 19:05:30 +00:00
//nolint:lll
2022-06-11 16:10:07 +00:00
return append(ret,
2022-12-24 17:10:20 +00:00
"└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘"),
nil
2022-06-11 16:10:07 +00:00
}