Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion fff
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ read_dir() {
# If '$PWD' is '/', unset it to avoid '//'.
[[ $PWD == / ]] && PWD=

max_len=1
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

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

max_len=${#item}
fi
if [[ -d $item ]]; then
dirs+=("$item")
((item_index++))
Expand All @@ -158,6 +162,9 @@ read_dir() {
files+=("$item")
fi
done
if ((max_len >= COLUMNS+8)); then
((max_len=COLUMNS+8))
fi

list=("${dirs[@]}" "${files[@]}")

Expand All @@ -171,6 +178,33 @@ read_dir() {
cur_list=("${list[@]}")
}

# Show file size in a human readable format, only for a regular files
# Result in hr_size variable
human_readable_size() {
local f="$1"
local ls_output
local s
hr_size=""
if [[ ! -L $f && -f $f ]]; then
read -a ls_output <<< $(ls -ld "$f")
s=${ls_output[4]}

if ((s < 1024)); then
hr_size="$s\\e[33m"
elif ((s < 1048576)); then
hr_size="$((s / 1024))\\e[32mK"
elif ((s < 1048576 * 1024)); then
hr_size="$((s / 1048576))\\e[32mM"
elif ((s < 1048576 * 1048576)); then
hr_size="$((s / (1048576 * 1024)))\\e[32mG"
else
hr_size="$s"
fi
hr_size="\\e[33m${hr_size}"
fi
}


print_line() {
# Format the list item and print it.
local file_name="${list[$1]##*/}"
Expand Down Expand Up @@ -251,7 +285,12 @@ print_line() {
# Remove all non-printable characters.
file_name="${file_name//[^[:print:]]/^[}"

printf '\r%b%s\e[m\r' "$format" "${file_name}${suffix}"
if [[ $details -gt 0 ]]; then
human_readable_size "${list[$1]}"
printf '\r%b%s%*b\e[m\r' "$format" "${file_name}${suffix}" "$((max_len - ${#file_name}))" "$hr_size"
else
printf '\r%b%s\e[m\r' "$format" "${file_name}${suffix}"
fi
}

draw_dir() {
Expand Down Expand Up @@ -522,6 +561,12 @@ cmd_line() {

key() {
case "$1" in
# toggle details mode
"${FFF_KEY_FSIZE1:=z}")
details=$((-details))
redraw
;;

# Open list item.
# 'C' is what bash sees when the right arrow is pressed ('\e[C').
# '' is what bash sees when the enter/return key is pressed.
Expand Down Expand Up @@ -830,6 +875,10 @@ main() {
# Trap the window resize signal (handle window resize events).
trap 'get_term_size; redraw' WINCH

# Show file size mode
FFF_SHOW_FSIZE=${FFF_SHOW_FSIZE:=1}
details=$((FFF_SHOW_FSIZE>0?1:-1))

redraw full

# Vintage infinite loop.
Expand Down
8 changes: 8 additions & 0 deletions fff.1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ m: mark move
d: mark trash (~/\.cache/fff/trash/)
p: paste/move/delete
c: clear file selections
z: toggle show file size mode

[1-9]: favourites/bookmarks (see customization)

Expand Down Expand Up @@ -98,6 +99,10 @@ export FFF_TRASH=~/.cache/fff/trash
# and directories.
export FFF_TRASH_CMD="mv"

# Show file size by default. May be toggled by FFF_KEY_FSIZE key later
# Default: 1
FFF_SHOW_FSIZE=1

# Favourites (Bookmarks) (keys 1-9) (dir or file)
export FFF_FAV1=~/projects
export FFF_FAV2=~/.bashrc
Expand Down Expand Up @@ -177,6 +182,9 @@ export FFF_KEY_ATTRIBUTES="x"
# Toggle hidden files.
export FFF_KEY_HIDDEN="."

# Toggle show file size mode
export FFF_KEY_FSIZE="z"

.
.fi

Expand Down