-
Notifications
You must be signed in to change notification settings - Fork 168
show file size mode #72
base: master
Are you sure you want to change the base?
Conversation
for item in "$PWD"/*; do | ||
if [[ ${#item} -gt $max_len ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm calculating here the maximal filename length and later use in in a printf in line 296 in order to align file sizes in one column
fff
Outdated
@@ -171,6 +175,37 @@ read_dir() { | |||
cur_list=("${list[@]}") | |||
} | |||
|
|||
# Convert number to a human readable format (with K, M, G suffixes) | |||
human_readable() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces don't go on a newline.
Also functions shouldn't echo
or printf
a result. They should modify a global to avoid subshells.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in new commit
fff
Outdated
human_readable() | ||
{ | ||
local s=$1 | ||
if [[ $s -lt 1024 ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ((s < 1024))
fff
Outdated
local s=$1 | ||
if [[ $s -lt 1024 ]]; then | ||
echo "$s\\e[33m" | ||
elif [[ $s -lt 1048576 ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif ((s < 1048576))
fff
Outdated
echo "$s\\e[33m" | ||
elif [[ $s -lt 1048576 ]]; then | ||
echo "$((s / 1024))\\e[32mK" | ||
elif [[ $s -lt $((1048576 * 1024)) ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif ((s < 1048576*1024))
fff
Outdated
echo "$((s / 1024))\\e[32mK" | ||
elif [[ $s -lt $((1048576 * 1024)) ]]; then | ||
echo "$((s / 1048576))\\e[32mM" | ||
elif [[ $s -lt $((1048576 * 1048576)) ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif ((s < 1048576*1048576))
fff
Outdated
|
||
# Show file size in a human readable format, only for a regular files | ||
human_readable_size() | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces don't go on a newline
fff
Outdated
human_readable_size() | ||
{ | ||
local f="$1" | ||
if [[ -L "$f" ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if [[ ! -L $f && -f $f ]]; then
...
else
...
fi
fff
Outdated
} | ||
|
||
# Show file size in a human readable format, only for a regular files | ||
human_readable_size() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functions shouldn't echo
or printf
. They should modify a global to avoid subshells.
fff
Outdated
if [[ -L "$f" ]]; then | ||
echo "" | ||
elif [[ -f "$f" ]]; then | ||
echo "\\e[33m$(human_readable "$(stat --printf=%s "$f")")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iirc stat
has no POSIX specification. Do these flags work everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is a coreutils stat command. Will fix it to POSIX compliant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what I've found (we may have to use different flags per OS):
- GNU: https://linux.die.net/man/1/stat
- FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=stat&sektion=1
- OpenBSD: https://man.openbsd.org/stat.1
- macOS: https://ss64.com/osx/stat.html
- Busybox:
stat
stat [OPTIONS] FILE...
Display file (default) or filesystem status
-c fmt Use the specified format
-f Display filesystem status
-L Follow links
-t Display info in terse form
Valid format sequences for files:
%a Access rights in octal
%A Access rights in human readable form
%b Number of blocks allocated (see %B)
%B The size in bytes of each block reported by %b
%d Device number in decimal
%D Device number in hex
%f Raw mode in hex
%F File type
%g Group ID of owner
%G Group name of owner
%h Number of hard links
%i Inode number
%n File name
%N File name, with -> TARGET if symlink
%o I/O block size
%s Total size, in bytes
%t Major device type in hex
%T Minor device type in hex
%u User ID of owner
%U User name of owner
%x Time of last access
%X Time of last access as seconds since Epoch
%y Time of last modification
%Y Time of last modification as seconds since Epoch
%z Time of last change
%Z Time of last change as seconds since Epoch
Valid format sequences for file systems:
%a Free blocks available to non-superuser
%b Total data blocks in file system
%c Total file nodes in file system
%d Free file nodes in file system
%f Free blocks in file system
%i File System ID in hex
%l Maximum length of filenames
%n File name
%s Block size (for faster transfer)
%S Fundamental block size (for block counts)
%t Type in hex
%T Type in human readable form
Read more at: https://www.commandlinux.com/man-page/man1/busybox.1.html#lbAG
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is using ls
(which has a POSIX specification) to print the information.
…riable Some cosmetic fixes File size are now determined by parsing of ls command output instead of stat command
All done in a2b28c2 |
for item in "$PWD"/*; do | ||
if [[ ${#item} -gt $max_len ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if max_len
is larger than the window width? It might be simpler to just right align the size output so it hugs the right side of the terminal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, fixed
The main problem with this PR is the performance impact. This adds significant delay to |
I don't have an idea how to deal with performance issue. Anyway we need to read all files size in a "show file size" mode. It can be done on read_dir function only once per directory but we may get a huge delay when reading big directories. OTOH just toggle it with "z" key and you get the original performance |
The another idea may be try to read the whole directory with "ls -l" command output parsing instead of internal globbing (like in "for item in "$PWD"/*; do" line in read_dir function. Will think about that, that requires read_dir rewriting |
The problem with that approach is that the output of |
The new "show file size" mode added. This mode shows file sizes (when available) on the right and it switched on by default. Mode may be toggled by "z" key and may be switch off by default by setting "export FFF_SHOW_FSIZE=0" environment variable