Add option to specify binary or decimal sizes (#2118)
* Add option to specify binary or decimal sizes * Fix test case error message * Add documentation * Fix typo
This commit is contained in:
1 parent
8150586bc2
commit
4fe8f5a858
8 files changed
+103
-31
No files matched your search
@@ -398,6 +398,8 @@ func completeCmd(acc []rune) (matches []compMatch, result string) {
|
||||
matches, result = matchList(f[2], []string{"mode", "timestamps"})
|
||||
case "selmode":
|
||||
matches, result = matchWord(f[2], []string{"all", "dir"})
|
||||
case "sizeunits":
|
||||
matches, result = matchWord(f[2], []string{"binary", "decimal"})
|
||||
case "sortby":
|
||||
matches, result = matchWord(f[2], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"})
|
||||
default:
|
||||
|
||||
@@ -198,6 +198,7 @@ The following options can be used to customize the behavior of lf:
|
||||
shellflag string (default '-c' for Unix and '/c' for Windows)
|
||||
shellopts []string (default '')
|
||||
showbinds bool (default true)
|
||||
sizeunits string (default 'binary')
|
||||
smartcase bool (default true)
|
||||
smartdia bool (default false)
|
||||
sortby string (default 'natural')
|
||||
@@ -981,6 +982,10 @@ List of shell options to pass to the shell executable.
|
||||
|
||||
Show bindings associated with pressed keys.
|
||||
|
||||
## sizeunits (string) (default `binary`)
|
||||
|
||||
Determines whether file sizes are displayed using binary units (`1K` is 1024 bytes) or decimal units (`1K` is 1000 bytes).
|
||||
|
||||
## smartcase (bool) (default true)
|
||||
|
||||
Override `ignorecase` option when the pattern contains an uppercase character.
|
||||
@@ -1015,8 +1020,6 @@ Special expansions are provided, `%p` as the file permissions, `%c` as the link
|
||||
|
||||
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.
|
||||
|
||||
@@ -188,6 +188,7 @@ The following options can be used to customize the behavior of lf:
|
||||
shellflag string (default '-c' for Unix and '/c' for Windows)
|
||||
shellopts []string (default '')
|
||||
showbinds bool (default true)
|
||||
sizeunits string (default 'binary')
|
||||
smartcase bool (default true)
|
||||
smartdia bool (default false)
|
||||
sortby string (default 'natural')
|
||||
@@ -1065,6 +1066,11 @@ showbinds (bool) (default true)
|
||||
|
||||
Show bindings associated with pressed keys.
|
||||
|
||||
sizeunits (string) (default binary)
|
||||
|
||||
Determines whether file sizes are displayed using binary units (1K is
|
||||
1024 bytes) or decimal units (1K is 1000 bytes).
|
||||
|
||||
smartcase (bool) (default true)
|
||||
|
||||
Override ignorecase option when the pattern contains an uppercase
|
||||
@@ -1106,9 +1112,6 @@ 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)
|
||||
|
||||
@@ -378,6 +378,14 @@ func (e *setExpr) eval(app *app, args []string) {
|
||||
return
|
||||
}
|
||||
gOpts.shellopts = strings.Split(e.val, ":")
|
||||
case "sizeunits":
|
||||
switch e.val {
|
||||
case "binary", "decimal":
|
||||
gOpts.sizeunits = e.val
|
||||
default:
|
||||
app.ui.echoerr("sizeunits: value should either be 'binary' or 'decimal'")
|
||||
return
|
||||
}
|
||||
case "sortby":
|
||||
method := sortMethod(e.val)
|
||||
if !isValidSortMethod(method) {
|
||||
|
||||
@@ -203,6 +203,7 @@ shell string (default \(aqsh\(aq for Unix and \(aqcmd\(aq for Win
|
||||
shellflag string (default \(aq\-c\(aq for Unix and \(aq/c\(aq for Windows)
|
||||
shellopts []string (default \(aq\(aq)
|
||||
showbinds bool (default true)
|
||||
sizeunits string (default \(aqbinary\(aq)
|
||||
smartcase bool (default true)
|
||||
smartdia bool (default false)
|
||||
sortby string (default \(aqnatural\(aq)
|
||||
@@ -931,6 +932,10 @@ Command line flag used to pass shell commands.
|
||||
List of shell options to pass to the shell executable.
|
||||
.SS showbinds (bool) (default true)
|
||||
Show bindings associated with pressed keys.
|
||||
.SS sizeunits (string) (default \f[CR]binary\f[R])
|
||||
Determines whether file sizes are displayed using binary units
|
||||
(\f[CR]1K\f[R] is 1024 bytes) or decimal units (\f[CR]1K\f[R] is 1000
|
||||
bytes).
|
||||
.SS smartcase (bool) (default true)
|
||||
Override \f[CR]ignorecase\f[R] option when the pattern contains an
|
||||
uppercase character.
|
||||
@@ -972,10 +977,6 @@ Special expansions are provided, \f[CR]%p\f[R] as the file permissions,
|
||||
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.
|
||||
|
||||
@@ -242,39 +242,43 @@ func readPairs(r io.Reader) ([][]string, error) {
|
||||
}
|
||||
|
||||
// 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.
|
||||
// prefixes for either binary (1 KiB = 1024 B) or decimal (1 KB = 1000 B)
|
||||
// multiples. The output should be no more than 5 characters long.
|
||||
func humanize(size uint64) string {
|
||||
if size < 1024 {
|
||||
var base uint64 = 1024
|
||||
if gOpts.sizeunits == "decimal" {
|
||||
base = 1000
|
||||
}
|
||||
|
||||
if size < base {
|
||||
return fmt.Sprintf("%dB", size)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// being `int64`, the maximum possible representable value would be 8 EiB or
|
||||
// 9.2 EB.
|
||||
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)
|
||||
"K", // kibi (2^10) or kilo (10^3)
|
||||
"M", // mebi (2^20) or mega (10^6)
|
||||
"G", // gibi (2^30) or giga (10^9)
|
||||
"T", // tebi (2^40) or tera (10^2)
|
||||
"P", // pebi (2^50) or peta (10^15)
|
||||
"E", // exbi (2^60) or exa (10^18)
|
||||
"Z", // zebi (2^70) or zetta (10^21)
|
||||
"Y", // yobi (2^80) or yotta (10^24)
|
||||
"R", // robi (2^90) or ronna (10^27)
|
||||
"Q", // quebi (2^100) or quetta (10^30)
|
||||
}
|
||||
|
||||
curr := float64(size) / 1024
|
||||
curr := float64(size) / float64(base)
|
||||
for _, prefix := range prefixes {
|
||||
if curr < 99.95 {
|
||||
return fmt.Sprintf("%.1f%s", curr, prefix)
|
||||
return fmt.Sprintf("%.1f%s", curr+1e-9, prefix)
|
||||
}
|
||||
if curr < 1023.5 {
|
||||
return fmt.Sprintf("%.0f%s", curr, prefix)
|
||||
if curr < float64(base)-0.5 {
|
||||
return fmt.Sprintf("%.0f%s", curr+1e-9, prefix)
|
||||
}
|
||||
curr /= 1024
|
||||
curr /= float64(base)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("+999%s", prefixes[len(prefixes)-1])
|
||||
|
||||
+50
-1
@@ -289,12 +289,61 @@ func TestHumanize(t *testing.T) {
|
||||
{1319413953332, "1.2T"}, // 1.2 TiB
|
||||
{1463669878895412, "1.3P"}, // 1.3 PiB
|
||||
{7955158381787244544, "6.9E"}, // 6.9 EiB
|
||||
{math.MaxInt64, "8.0E"}, // 8 EiB
|
||||
{math.MaxUint64, "16.0E"}, // 16 EiB
|
||||
}
|
||||
|
||||
gOpts.sizeunits = "binary"
|
||||
for _, test := range tests {
|
||||
if got := humanize(test.size); got != test.expected {
|
||||
t.Errorf("at input '%d' expected '%s' but got '%s'", test.size, test.expected, got)
|
||||
t.Errorf("at input ('%d', '%s') expected '%s' but got '%s'", test.size, gOpts.sizeunits, test.expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
tests = []struct {
|
||||
size uint64
|
||||
expected string
|
||||
}{
|
||||
{0, "0B"},
|
||||
{1, "1B"},
|
||||
{2, "2B"},
|
||||
{10, "10B"},
|
||||
{100, "100B"},
|
||||
{999, "999B"},
|
||||
{1000, "1.0K"},
|
||||
{1001, "1.0K"},
|
||||
{1049, "1.0K"},
|
||||
{1050, "1.1K"},
|
||||
{1051, "1.1K"},
|
||||
{9949, "9.9K"},
|
||||
{9950, "10.0K"},
|
||||
{9951, "10.0K"},
|
||||
{9999, "10.0K"},
|
||||
{10000, "10.0K"},
|
||||
{10001, "10.0K"},
|
||||
{99949, "99.9K"},
|
||||
{99950, "100K"},
|
||||
{99951, "100K"},
|
||||
{999499, "999K"},
|
||||
{999500, "1.0M"},
|
||||
{999501, "1.0M"},
|
||||
{999999, "1.0M"},
|
||||
{1000000, "1.0M"},
|
||||
{1000001, "1.0M"},
|
||||
{999499999, "999M"},
|
||||
{999500000, "1.0G"},
|
||||
{999500001, "1.0G"},
|
||||
{999999999, "1.0G"},
|
||||
{1000000000, "1.0G"},
|
||||
{1000000001, "1.0G"},
|
||||
{math.MaxInt64, "9.2E"},
|
||||
{math.MaxUint64, "18.4E"},
|
||||
}
|
||||
|
||||
gOpts.sizeunits = "decimal"
|
||||
for _, test := range tests {
|
||||
if got := humanize(test.size); got != test.expected {
|
||||
t.Errorf("at input ('%d', '%s') expected '%s' but got '%s'", test.size, gOpts.sizeunits, test.expected, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ var gOpts struct {
|
||||
shellflag string
|
||||
shellopts []string
|
||||
showbinds bool
|
||||
sizeunits string
|
||||
smartcase bool
|
||||
smartdia bool
|
||||
sortby sortMethod
|
||||
@@ -252,6 +253,7 @@ func init() {
|
||||
gOpts.shellflag = gDefaultShellFlag
|
||||
gOpts.shellopts = nil
|
||||
gOpts.showbinds = true
|
||||
gOpts.sizeunits = "binary"
|
||||
gOpts.smartcase = true
|
||||
gOpts.smartdia = false
|
||||
gOpts.sortby = naturalSort
|
||||
|
||||
Reference in new issue
Block a user