feat: use simple chars for bar

This commit is contained in:
Yiyang Kang 2022-11-22 20:35:18 +08:00
parent 18cb9715e4
commit 412725c6b9
1 changed files with 10 additions and 9 deletions

19
bot.go
View File

@ -163,18 +163,19 @@ func handleChatInfoCmd(c tele.Context) error {
} }
func drawBar(progress float64, length int) string { func drawBar(progress float64, length int) string {
barChars := []rune("░▒▓█") barChars := []rune("·-=")
if length <= 0 { if length <= 0 {
return "" return ""
} }
step := 1 / float64(length) step := 1 / float64(length)
buf := make([]rune, length) buf := make([]rune, length+2)
buf[0], buf[length+1] = '[', ']'
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
fill := (progress - float64(i)*step) / step fill := (progress - float64(i)*step) / step
fill = math.Min(math.Max(fill, 0), 1) fill = math.Min(math.Max(fill, 0), 1)
idx := int(math.Round(fill * float64(len(barChars)-1))) idx := int(math.Round(fill * float64(len(barChars)-1)))
buf[i] = barChars[idx] buf[i+1] = barChars[idx]
} }
return string(buf) return string(buf)
} }
@ -183,7 +184,7 @@ func drawBarForTrafficRecord(r stats.VnstatTrafficRecord) string {
effective := lo.Max([]uint64{r.Rx, r.Tx}) effective := lo.Max([]uint64{r.Rx, r.Tx})
max := config.MonthlyTrafficLimitGiB max := config.MonthlyTrafficLimitGiB
ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max) ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max)
return fmt.Sprintf("%s `%2.0f%%`", drawBar(ratio, 16), ratio*100) return fmt.Sprintf("`%s %2.0f%%`", drawBar(ratio, 25), ratio*100)
} }
func handleYearProgressCmd(c tele.Context) error { func handleYearProgressCmd(c tele.Context) error {
@ -193,9 +194,9 @@ func handleYearProgressCmd(c tele.Context) error {
elapsed := time.Since(yearStart) elapsed := time.Since(yearStart)
ratio := float64(elapsed) / float64(yearDur) ratio := float64(elapsed) / float64(yearDur)
replyText := []string{ replyText := fmt.Sprintf(
fmt.Sprintf("`%d is %2.0f%% complete.`", time.Now().Year(), ratio*100), "\n%d is <b>%2.0f%%</b> complete.\n<pre>%s</pre>",
drawBar(ratio, 20), time.Now().Year(), ratio*100, drawBar(ratio, 25),
} )
return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}) return c.Reply(replyText, &tele.SendOptions{ParseMode: tele.ModeHTML})
} }