Skip to content

A self-hosted dashboard that puts all your feeds in one place

Fixed bytes formatting

ThiemeHe14569821a0e
diff --git a/internal/glance/templates.go b/internal/glance/templates.go
index 14eb368..1633c30 100644
--- a/internal/glance/templates.go
+++ b/internal/glance/templates.go
@@ -5,6 +5,7 @@ import (
"html/template"
"math"
"strconv"
+ "strings"
"golang.org/x/text/language"
"golang.org/x/text/message"
@@ -32,27 +33,14 @@ var globalTemplateFunctions = template.FuncMap{
return intl.Sprintf("%."+strconv.Itoa(precision)+"f", price)
},
"dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs,
+ "formatBytes": formatBytes,
"formatServerMegabytes": func(mb uint64) template.HTML {
- var value string
- var label string
-
- if mb < 1_000 {
- 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"
+ formatted := formatBytes(mb * 1024 * 1024)
+ parts := strings.Split(formatted, " ")
+ if len(parts) == 2 {
+ return template.HTML(parts[0] + ` <span class="color-base size-h5">` + parts[1] + `</span>`)
}
-
- return template.HTML(value + ` <span class="color-base size-h5">` + label + `</span>`)
+ return template.HTML(formatted)
},
}
@@ -109,3 +97,39 @@ func multiply(a, b interface{}) float64 {
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
+}
diff --git a/internal/glance/templates/qbittorrent.html b/internal/glance/templates/qbittorrent.html
index 51721e1..bc53270 100644
--- a/internal/glance/templates/qbittorrent.html
+++ b/internal/glance/templates/qbittorrent.html
@@ -10,14 +10,14 @@
</div>
<div class="qbittorrent-torrent-progress flex items-center gap-10">
<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>
<span class="qbittorrent-progress-text color-highlight text-very-compact">{{ printf "%.1f" (multiply
.Progress 100) }}
<span class="color-base">%</span></span>
</div>
<div class="qbittorrent-torrent-info">
- <span class="qbittorrent-torrent-speed">{{ .Speed }}/s</span>
+ <span class="qbittorrent-torrent-speed">{{ .Speed | formatBytes }}/s</span>
</div>
</div>
{{- end }}
diff --git a/internal/glance/widget-qbittorrent.go b/internal/glance/widget-qbittorrent.go
index 1235d75..99f490d 100644
--- a/internal/glance/widget-qbittorrent.go
+++ b/internal/glance/widget-qbittorrent.go
@@ -7,7 +7,6 @@ import (
"fmt"
"html/template"
"io"
- "log/slog"
"net/http"
"net/http/cookiejar"
"net/url"
@@ -105,8 +104,6 @@ func (widget *qbittorrentWidget) login() error {
}
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)
if err != nil {
widget.withError(fmt.Errorf("creating torrents request: %w", err))