Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

show file size mode #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

yiselieren
Copy link

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

fff
for item in "$PWD"/*; do
if [[ ${#item} -gt $max_len ]]; then
Copy link
Owner

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?

Copy link
Author

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()
Copy link
Owner

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens on my machine:

fff

Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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()
{
Copy link
Owner

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
Copy link
Owner

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()
Copy link
Owner

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")")"
Copy link
Owner

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?

Copy link
Author

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

Copy link
Owner

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):

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

Copy link
Owner

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
@yiselieren
Copy link
Author

All done in a2b28c2

fff
for item in "$PWD"/*; do
if [[ ${#item} -gt $max_len ]]; then
Copy link
Owner

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, fixed

@dylanaraps
Copy link
Owner

The main problem with this PR is the performance impact. This adds significant delay to fff. If I hold down a scroll key it takes seconds to complete when I stop holding it down.

@yiselieren
Copy link
Author

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

@yiselieren
Copy link
Author

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

@dylanaraps
Copy link
Owner

The problem with that approach is that the output of ls shouldn't be used in scripts (when related to using the file name portion.). ls prints the list items in a way where it can't be guaranteed that it matches the actual file name.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants