From 16d97676059e1bf0e2add3b8c0d0b7d95143a18d Mon Sep 17 00:00:00 2001 From: Alex Rezinsky Date: Thu, 24 Jan 2019 19:03:54 +0200 Subject: [PATCH 1/3] show file size mode --- fff | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- fff.1 | 8 ++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/fff b/fff index 94ce46d..16653aa 100755 --- a/fff +++ b/fff @@ -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 + max_len=${#item} + fi if [[ -d $item ]]; then dirs+=("$item") ((item_index++)) @@ -171,6 +175,37 @@ read_dir() { cur_list=("${list[@]}") } +# Convert number to a human readable format (with K, M, G suffixes) +human_readable() +{ + local s=$1 + if [[ $s -lt 1024 ]]; then + echo "$s\\e[33m" + elif [[ $s -lt 1048576 ]]; then + echo "$((s / 1024))\\e[32mK" + elif [[ $s -lt $((1048576 * 1024)) ]]; then + echo "$((s / 1048576))\\e[32mM" + elif [[ $s -lt $((1048576 * 1048576)) ]]; then + echo "$((s / (1048576 * 1024)))\\e[32mG" + else + echo "$s" + fi +} + +# Show file size in a human readable format, only for a regular files +human_readable_size() +{ + local f="$1" + if [[ -L "$f" ]]; then + echo "" + elif [[ -f "$f" ]]; then + echo "\\e[33m$(human_readable "$(stat --printf=%s "$f")")" + else + echo "" + fi +} + + print_line() { # Format the list item and print it. local file_name="${list[$1]##*/}" @@ -251,7 +286,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 + x=$(human_readable_size "${list[$1]}") + printf '\r%b%s%*b\e[m\r' "$format" "${file_name}${suffix}" "$((max_len - ${#file_name}))" "$x" + else + printf '\r%b%s\e[m\r' "$format" "${file_name}${suffix}" + fi } draw_dir() { @@ -522,6 +562,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. @@ -830,6 +876,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. diff --git a/fff.1 b/fff.1 index 2a68d67..c0af653 100644 --- a/fff.1 +++ b/fff.1 @@ -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) @@ -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 @@ -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 From a2b28c21018d16d7bbbfd64d766ecc2bf9229ae6 Mon Sep 17 00:00:00 2001 From: Alex Rezinsky Date: Sat, 26 Jan 2019 17:35:28 +0200 Subject: [PATCH 2/3] human_readable_size function rewriten to put its result to hr_size variable Some cosmetic fixes File size are now determined by parsing of ls command output instead of stat command --- fff | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/fff b/fff index 16653aa..0801402 100755 --- a/fff +++ b/fff @@ -175,33 +175,29 @@ read_dir() { cur_list=("${list[@]}") } -# Convert number to a human readable format (with K, M, G suffixes) -human_readable() -{ - local s=$1 - if [[ $s -lt 1024 ]]; then - echo "$s\\e[33m" - elif [[ $s -lt 1048576 ]]; then - echo "$((s / 1024))\\e[32mK" - elif [[ $s -lt $((1048576 * 1024)) ]]; then - echo "$((s / 1048576))\\e[32mM" - elif [[ $s -lt $((1048576 * 1048576)) ]]; then - echo "$((s / (1048576 * 1024)))\\e[32mG" - else - echo "$s" - fi -} - # Show file size in a human readable format, only for a regular files -human_readable_size() -{ +# Result in hr_size variable +human_readable_size() { local f="$1" - if [[ -L "$f" ]]; then - echo "" - elif [[ -f "$f" ]]; then - echo "\\e[33m$(human_readable "$(stat --printf=%s "$f")")" - else - echo "" + 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 } @@ -287,8 +283,8 @@ print_line() { file_name="${file_name//[^[:print:]]/^[}" if [[ $details -gt 0 ]]; then - x=$(human_readable_size "${list[$1]}") - printf '\r%b%s%*b\e[m\r' "$format" "${file_name}${suffix}" "$((max_len - ${#file_name}))" "$x" + 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 From 2d30fa244fda28161b09261492a004e0f5ce2168 Mon Sep 17 00:00:00 2001 From: Alex Rezinsky Date: Sat, 26 Jan 2019 18:41:53 +0200 Subject: [PATCH 3/3] max_len can't be greater than COLUMNS with some spare --- fff | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fff b/fff index 0801402..b3db949 100755 --- a/fff +++ b/fff @@ -162,6 +162,9 @@ read_dir() { files+=("$item") fi done + if ((max_len >= COLUMNS+8)); then + ((max_len=COLUMNS+8)) + fi list=("${dirs[@]}" "${files[@]}")