Skip to content

Instantly share code, notes, and snippets.

@norpol
Last active June 14, 2018 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norpol/1ff30f0f614c38dccfabb4f9f62a73e6 to your computer and use it in GitHub Desktop.
Save norpol/1ff30f0f614c38dccfabb4f9f62a73e6 to your computer and use it in GitHub Desktop.
chpwd ls improvement
# Put this into your ~/.zshrc
# Enables chpwd, which runs a command if you cd/pushd/popd...
# wrapper around ls, which ommits output if more than 1/3 of screen would be used
# Demo https://asciinema.org/a/123167
ls_reduced() {
files="$(ls -tr --color=always -- "$(pwd)")"
lines="$(printf -- "%b" "${files}" | wc -l)"
term_height="$(tput lines)"
term_target_height="$((term_height/3))"
lines_ommited="$((lines-term_target_height))"
if [ "${lines}" -gt "${term_target_height}" ]; then
printf -- "%b" "${files}\n─ Ommited ${lines_ommited} additional lines ─\n" | tail -${term_target_height}
else
printf -- "%b" "${files}\n"
fi
}
chpwd() { ls_reduced; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment