Fixed bytes formatting

pull/543/head
ThiemeH 2025-03-31 21:33:43 +07:00
parent b46cd6601c
commit e14569821a
3 changed files with 45 additions and 24 deletions

@ -5,6 +5,7 @@ import (
"html/template" "html/template"
"math" "math"
"strconv" "strconv"
"strings"
"golang.org/x/text/language" "golang.org/x/text/language"
"golang.org/x/text/message" "golang.org/x/text/message"
@ -32,27 +33,14 @@ var globalTemplateFunctions = template.FuncMap{
return intl.Sprintf("%."+strconv.Itoa(precision)+"f", price) return intl.Sprintf("%."+strconv.Itoa(precision)+"f", price)
}, },
"dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs, "dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs,
"formatBytes": formatBytes,
"formatServerMegabytes": func(mb uint64) template.HTML { "formatServerMegabytes": func(mb uint64) template.HTML {
var value string formatted := formatBytes(mb * 1024 * 1024)
var label string parts := strings.Split(formatted, " ")
if len(parts) == 2 {
if mb < 1_000 { return template.HTML(parts[0] + ` <span class="color-base size-h5">` + parts[1] + `</span>`)
value = strconv.FormatUint(mb, 10)
label = "MB"
} else if mb < 1_000_000 {
if mb < 10_000 {
value = fmt.Sprintf("%.1f", float64(mb)/1_000)
} else {
value = strconv.FormatUint(mb/1_000, 10)
}
label = "GB"
} else {
value = fmt.Sprintf("%.1f", float64(mb)/1_000_000)
label = "TB"
} }
return template.HTML(formatted)
return template.HTML(value + ` <span class="color-base size-h5">` + label + `</span>`)
}, },
} }
@ -109,3 +97,39 @@ func multiply(a, b interface{}) float64 {
panic("Unsupported type for 'b', only int and float64 are supported") panic("Unsupported type for 'b', only int and float64 are supported")
} }
} }
func formatBytes(bytes uint64) string {
var value string
var unit string
if bytes < 1024 {
value = strconv.FormatUint(bytes, 10)
unit = "B"
} else if bytes < 1024*1024 {
if bytes < 10*1024 {
value = fmt.Sprintf("%.1f", float64(bytes)/1024)
} else {
value = strconv.FormatUint(bytes/1024, 10)
}
unit = "KB"
} else if bytes < 1024*1024*1024 {
if bytes < 10*1024*1024 {
value = fmt.Sprintf("%.1f", float64(bytes)/(1024*1024))
} else {
value = strconv.FormatUint(bytes/(1024*1024), 10)
}
unit = "MB"
} else if bytes < 1024*1024*1024*1024 {
if bytes < 10*1024*1024*1024 {
value = fmt.Sprintf("%.1f", float64(bytes)/(1024*1024*1024))
} else {
value = strconv.FormatUint(bytes/(1024*1024*1024), 10)
}
unit = "GB"
} else {
value = fmt.Sprintf("%.1f", float64(bytes)/(1024*1024*1024*1024))
unit = "TB"
}
return value + " " + unit
}

@ -10,14 +10,14 @@
</div> </div>
<div class="qbittorrent-torrent-progress flex items-center gap-10"> <div class="qbittorrent-torrent-progress flex items-center gap-10">
<div class="progress-bar flex-1"> <div class="progress-bar flex-1">
<div class="progress-value" style="--percent: {{ multiply .Progress 100 }};"></div> <div class="progress-value" style="--percent: {{ (multiply .Progress 100) }};"></div>
</div> </div>
<span class="qbittorrent-progress-text color-highlight text-very-compact">{{ printf "%.1f" (multiply <span class="qbittorrent-progress-text color-highlight text-very-compact">{{ printf "%.1f" (multiply
.Progress 100) }} .Progress 100) }}
<span class="color-base">%</span></span> <span class="color-base">%</span></span>
</div> </div>
<div class="qbittorrent-torrent-info"> <div class="qbittorrent-torrent-info">
<span class="qbittorrent-torrent-speed">{{ .Speed }}/s</span> <span class="qbittorrent-torrent-speed">{{ .Speed | formatBytes }}/s</span>
</div> </div>
</div> </div>
{{- end }} {{- end }}

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"log/slog"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
@ -105,8 +104,6 @@ func (widget *qbittorrentWidget) login() error {
} }
func (widget *qbittorrentWidget) update(ctx context.Context) { func (widget *qbittorrentWidget) update(ctx context.Context) {
slog.Info("Updating qBittorrent widget", "url", widget.URL)
req, err := http.NewRequestWithContext(ctx, "GET", widget.URL+qBittorrentTorrentsPath, nil) req, err := http.NewRequestWithContext(ctx, "GET", widget.URL+qBittorrentTorrentsPath, nil)
if err != nil { if err != nil {
widget.withError(fmt.Errorf("creating torrents request: %w", err)) widget.withError(fmt.Errorf("creating torrents request: %w", err))