This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
show file size mode #72
Open
yiselieren
wants to merge
3
commits into
dylanaraps:master
Choose a base branch
from
yiselieren:file_size
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++)) | ||
|
@@ -158,6 +162,9 @@ read_dir() { | |
files+=("$item") | ||
fi | ||
done | ||
if ((max_len >= COLUMNS+8)); then | ||
((max_len=COLUMNS+8)) | ||
fi | ||
|
||
list=("${dirs[@]}" "${files[@]}") | ||
|
||
|
@@ -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]##*/}" | ||
|
@@ -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() { | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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