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