Skip to content

Commit

Permalink
[zsh] Shorten _truncated_ls by using more builtins
Browse files Browse the repository at this point in the history
-   Fixed off-by-one issue with _right_align, causing a remaining space
    at the right-most side.
-   Avoid `wc -m` by using ${#var} syntax
-   Avoid the loop in _right_align by using printf
-   Use herestrings to avoid calls to `echo -E`
-   There shouldn't ever be more than 3 forks caused by running `cd`
    now.

Hopefully this should keep cd from hanging for a second when my machine
is under heavy load.
  • Loading branch information
bgw committed Apr 16, 2015
1 parent 1f3df2a commit e54d6da
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions zsh/commands/filesystem.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ _truncated_ls() {
--format=across \
--color=always \
--width=$COLUMNS)"
local RAW_LS_LINES="$(builtin echo -E "$RAW_LS_OUT" | wc -l)"
local RAW_LS_LINES="$(command wc -l <<< "$RAW_LS_OUT")"

if [[ $RAW_LS_LINES -gt $LS_LINES ]]; then
builtin echo -E "$RAW_LS_OUT" | head -n $(($LS_LINES - 1))
command head -n $(($LS_LINES - 1)) <<< "$RAW_LS_OUT"
_right_align "... $(($RAW_LS_LINES - $LS_LINES + 1)) lines hidden"
else
builtin echo -E "$RAW_LS_OUT"
Expand All @@ -62,12 +62,8 @@ _truncated_ls() {

# right align text and echo it; helper function for _truncated_ls
_right_align() {
local PADDING=$(($COLUMNS - $(builtin echo "$1" | wc -m)))
if [[ $PADDING -gt 0 ]]; then
for i in {1..$PADDING}; do
builtin echo -n " "
done
fi
local PADDING=$(($COLUMNS - ${#1}))
[[ $PADDING -gt 0 ]] && builtin printf "%${PADDING}s"
builtin echo "$1"
}

Expand Down

0 comments on commit e54d6da

Please sign in to comment.