Change file size units to IEC 80000-13:2025 (binary multiples) (#2062)

* Change file size units from SI to IEC 80000-13:2025

Increase left-padded (%S) file size string length to 5 characters

Co-authored-by: Joe Lim <50560759+joelim-work@users.noreply.github.com>

* Add note about used IEC units for file size in docs

---------

Co-authored-by: Joe Lim <50560759+joelim-work@users.noreply.github.com>
This commit is contained in:
Andrew VoynovandJoe Lim authored and GitHub committed 2025-07-26 19:03:13 +10:00
1 parent d60acef469
commit 23b30ae1ee
10 files changed
+90 -56

No files matched your search

+1 -1
View File
@@ -15,5 +15,5 @@ func diskFree(wd string) string {
}
// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.F_bavail)*uint64(stat.F_bsize)))
return "df: " + humanize(uint64(stat.F_bavail)*uint64(stat.F_bsize))
}
+1 -1
View File
@@ -17,5 +17,5 @@ func diskFree(wd string) string {
}
// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.Bavail)*uint64(stat.Bsize)))
return "df: " + humanize(uint64(stat.Bavail)*uint64(stat.Bsize))
}
+1 -1
View File
@@ -17,5 +17,5 @@ func diskFree(wd string) string {
}
// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.Bavail)*uint64(stat.Bsize)))
return "df: " + humanize(uint64(stat.Bavail)*uint64(stat.Bsize))
}
+1 -1
View File
@@ -19,5 +19,5 @@ func diskFree(wd string) string {
log.Printf("diskfree: %s", err)
return ""
}
return "df: " + humanize(int64(free))
return "df: " + humanize(free)
}
+4 -1
View File
@@ -1015,9 +1015,12 @@ Meaning of each sort type:
## statfmt (string) (default `\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l`)
Format string of the file info shown in the bottom left corner.
Special expansions are provided, `%p` as the file permissions, `%c` as the link count, `%u` as the user, `%g` as the group, `%s` as the file size, `%S` as the file size but with a fixed width of four characters (left-padded with spaces), `%t` as the last modified time, `%l` as the link target, `%m` as the current mode and `%M` as the current mode but also shown in Normal mode (displaying `NORMAL` instead of a blank string).
Special expansions are provided, `%p` as the file permissions, `%c` as the link count, `%u` as the user, `%g` as the group, `%s` as the file size, `%S` as the file size but with a fixed width of five characters (left-padded with spaces), `%t` as the last modified time, `%l` as the link target, `%m` as the current mode and `%M` as the current mode but also shown in Normal mode (displaying `NORMAL` instead of a blank string).
The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown.
File size is formatted using first letter of IEC 80000-13:2025 prefixes for binary multiples (i.e. 1024 bytes is `1.0K`).
## tabstop (int) (default 8)
Number of space characters to show for horizontal tabulation (U+0009) character.
+9 -4
View File
@@ -1105,12 +1105,17 @@ statfmt (string) (default \033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l)
Format string of the file info shown in the bottom left corner. Special
expansions are provided, %p as the file permissions, %c as the link
count, %u as the user, %g as the group, %s as the file size, %S as the
file size but with a fixed width of four characters (left-padded with
file size but with a fixed width of five characters (left-padded with
spaces), %t as the last modified time, %l as the link target, %m as the
current mode and %M as the current mode but also shown in Normal mode
(displaying NORMAL instead of a blank string). The | character splits
the format string into sections. Any section containing a failed
expansion (result is a blank string) is discarded and not shown.
(displaying NORMAL instead of a blank string).
The | character splits the format string into sections. Any section
containing a failed expansion (result is a blank string) is discarded
and not shown.
File size is formatted using first letter of IEC 80000-13:2025 prefixes
for binary multiples (i.e. 1024 bytes is 1.0K).
tabstop (int) (default 8)
+7 -2
View File
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 3.7.0.2
.\"
.TH "LF" "1" "2025\-07\-19" "" "DOCUMENTATION"
.TH "LF" "1" "2025\-07\-24" "" "DOCUMENTATION"
.SH NAME
lf \- terminal file manager
.SH SYNOPSIS
@@ -966,14 +966,19 @@ Format string of the file info shown in the bottom left corner.
Special expansions are provided, \f[CR]%p\f[R] as the file permissions,
\f[CR]%c\f[R] as the link count, \f[CR]%u\f[R] as the user,
\f[CR]%g\f[R] as the group, \f[CR]%s\f[R] as the file size,
\f[CR]%S\f[R] as the file size but with a fixed width of four characters
\f[CR]%S\f[R] as the file size but with a fixed width of five characters
(left\-padded with spaces), \f[CR]%t\f[R] as the last modified time,
\f[CR]%l\f[R] as the link target, \f[CR]%m\f[R] as the current mode and
\f[CR]%M\f[R] as the current mode but also shown in Normal mode
(displaying \f[CR]NORMAL\f[R] instead of a blank string).
.PP
The \f[CR]|\f[R] character splits the format string into sections.
Any section containing a failed expansion (result is a blank string) is
discarded and not shown.
.PP
File size is formatted using first letter of IEC 80000\-13:2025 prefixes
for binary multiples (i.e.
1024 bytes is \f[CR]1.0K\f[R]).
.SS tabstop (int) (default 8)
Number of space characters to show for horizontal tabulation (U+0009)
character.
+28 -23
View File
@@ -234,38 +234,43 @@ func readPairs(r io.Reader) ([][]string, error) {
return readArrays(r, 2, 2)
}
// This function converts a size in bytes to a human readable form using metric
// suffixes (e.g. 1K = 1000). For values less than 10 the first significant
// digit is shown, otherwise it is hidden. Numbers are always rounded down.
// This should be fine for most human beings.
func humanize(size int64) string {
if size < 1000 {
// This function converts a size in bytes to a human readable form using
// prefixes for binary multiples (e.g., 1 KiB = 1024 B). The output should be
// no more than 5 characters long.
func humanize(size uint64) string {
if size < 1024 {
return fmt.Sprintf("%dB", size)
}
suffix := []string{
"K", // kilo
"M", // mega
"G", // giga
"T", // tera
"P", // peta
"E", // exa
"Z", // zeta
"Y", // yotta
// Shortened (due to TUI space constraints) version of
// IEC 80000-13:2025 prefixes for binary multiples.
// *Note*: due to [`FileSize.Size()`](https://pkg.go.dev/io/fs#FileInfo)
// being `int64`, maximum possible representable value would be 8 EiB.
prefixes := []string{
"K", // kibi (2^10)
"M", // mebi (2^20)
"G", // gibi (2^30)
"T", // tebi (2^40)
"P", // pebi (2^50)
"E", // exbi (2^60)
"Z", // zebi (2^70)
"Y", // yobi (2^80)
"R", // robi (2^90)
"Q", // quebi (2^100)
}
curr := float64(size) / 1000
for _, s := range suffix {
if curr < 10 {
return fmt.Sprintf("%.1f%s", curr-0.0499, s)
curr := float64(size) / 1024
for _, prefix := range prefixes {
if curr < 99.95 {
return fmt.Sprintf("%.1f%s", curr, prefix)
}
if curr < 1000 {
return fmt.Sprintf("%d%s", int(curr), s)
if curr < 1023.5 {
return fmt.Sprintf("%.0f%s", curr, prefix)
}
curr /= 1000
curr /= 1024
}
return ""
return fmt.Sprintf("+999%s", prefixes[len(prefixes)-1])
}
// This function compares two strings for natural sorting which takes into
+34 -18
View File
@@ -1,6 +1,7 @@
package main
import (
"math"
"os"
"reflect"
"strings"
@@ -247,29 +248,44 @@ func TestReadArrays(t *testing.T) {
func TestHumanize(t *testing.T) {
tests := []struct {
i int64
exp string
size uint64
expected string
}{
{0, "0B"},
{9, "9B"},
{99, "99B"},
{999, "999B"},
{1000, "1.0K"},
{1023, "1.0K"},
{1025, "1.0K"},
{1049, "1.0K"},
{1050, "1.0K"},
{1099, "1.0K"},
{9999, "9.9K"},
{10000, "10K"},
{10100, "10K"},
{10500, "10K"},
{1000000, "1.0M"},
{1, "1B"},
{2, "2B"},
{10, "10B"},
{100, "100B"},
{1000, "1000B"},
{1023, "1023B"},
{1024, "1.0K"},
{1025, "1.0K"}, // 1.000976563 KiB
{10188, "9.9K"}, // 9.94921875 KiB
{10189, "10.0K"}, // 9.950195313 KiB
{10240, "10.0K"}, // 10 KiB
{10291, "10.0K"}, // 10.049804688 KiB
{10292, "10.1K"}, // 10.05078125 KiB
{10342, "10.1K"}, // 10.099609375 KiB
{102348, "99.9K"}, // 99.94921875 KiB
{102349, "100K"}, // 99.950195313 KiB
{1023487, "999K"}, // 999.499023438 KiB
{1023488, "1000K"}, // 999.5 KiB
{1048063, "1023K"}, // 1023.499023438 KiB
{1048064, "1.0M"}, // 1023.5 KiB
{1072693248, "1023M"}, // 1023 MiB
{1073217535, "1023M"}, // 1023.499999046 MiB
{1073217536, "1.0G"}, // 1023.5 MiB
{1073741824, "1.0G"}, // 1 GiB
{1610612736, "1.5G"}, // 1.5 GiB
{1319413953332, "1.2T"}, // 1.2 TiB
{1463669878895412, "1.3P"}, // 1.3 PiB
{7955158381787244544, "6.9E"}, // 6.9 EiB
{math.MaxUint64, "16.0E"}, // 16 EiB
}
for _, test := range tests {
if got := humanize(test.i); got != test.exp {
t.Errorf("at input '%d' expected '%s' but got '%s'", test.i, test.exp, got)
if got := humanize(test.size); got != test.expected {
t.Errorf("at input '%d' expected '%s' but got '%s'", test.size, test.expected, got)
}
}
}
+4 -4
View File
@@ -316,9 +316,9 @@ func fileInfo(f *file, d *dir, userWidth int, groupWidth int, customWidth int) (
if f.IsDir() && f.dirSize < 0 {
sz = "-"
} else {
sz = humanize(f.TotalSize())
sz = humanize(uint64(f.TotalSize()))
}
fmt.Fprintf(&info, " %4s", sz)
fmt.Fprintf(&info, " %5s", sz)
case "time":
fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.ModTime()))
case "atime":
@@ -838,8 +838,8 @@ func (ui *ui) loadFileInfo(nav *nav) {
replace("%c", linkCount(curr))
replace("%u", userName(curr))
replace("%g", groupName(curr))
replace("%s", humanize(curr.Size()))
replace("%S", fmt.Sprintf("%4s", humanize(curr.Size())))
replace("%s", humanize(uint64(curr.Size())))
replace("%S", fmt.Sprintf("%5s", humanize(uint64(curr.Size()))))
replace("%t", curr.ModTime().Format(gOpts.timefmt))
replace("%l", curr.linkTarget)