Skip to content

Commit

Permalink
[zsh] Improve OS X compatibility for _truncated_ls
Browse files Browse the repository at this point in the history
We still need the GNU coreutils version of `ls` so we can use
`--group-directories-first`, but at least now we look for it on OS X
(brew's coreutils package calls it `gls`), and we use `-m` instead of
`--chars` for wc, as OS X's `wc` command doesn't like the long-form.
  • Loading branch information
bgw committed Apr 16, 2015
1 parent a0f2c4f commit eb0152f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions zsh/commands/filesystem.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ cd() { builtin cd "$@" && _truncated_ls }
popd() { builtin popd "$@" && _truncated_ls }
pushd() { builtin pushd "$@" && _truncated_ls }

# try to use gnu's version of ls, needed for --group-directories-first
# on OS X, you'll need to run `brew install coreutils`
if hash gls >/dev/null 2>&1; then
# gls maps to gnu coreutil's ls on OS X
_GLS_COMMAND=gls
else
_GLS_COMMAND=ls
fi

# a pretty ls truncated to at most N lines; helper function for cd, popd, pushd
_truncated_ls() {
local LS_LINES=8 # use no more than N lines for ls output
Expand All @@ -37,10 +46,10 @@ _truncated_ls() {
fi

# compute and store the result of ls
local RAW_LS_OUT="$(command ls --group-directories-first \
--format=across \
--color=always \
--width=$COLUMNS)"
local RAW_LS_OUT="$(command $_GLS_COMMAND --group-directories-first \
--format=across \
--color=always \
--width=$COLUMNS)"
local RAW_LS_LINES="$(builtin echo -E "$RAW_LS_OUT" | wc -l)"

if [[ $RAW_LS_LINES -gt $LS_LINES ]]; then
Expand All @@ -53,7 +62,7 @@ _truncated_ls() {

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

0 comments on commit eb0152f

Please sign in to comment.