From 9d77b8a29e82e00035e2a8aadbf228d4c875fefe Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 15 Aug 2016 21:47:45 +1200 Subject: Keep ls(1) option metadata in cache --- sh/profile.d/ls.sh | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'sh/profile.d') diff --git a/sh/profile.d/ls.sh b/sh/profile.d/ls.sh index a3cc84b7..7eb61fde 100644 --- a/sh/profile.d/ls.sh +++ b/sh/profile.d/ls.sh @@ -1,18 +1,31 @@ -# Store ls(1)'s --help output in a variable -ls_help=$(ls --help 2>/dev/null) +# Test that we have metadata about what options this system's ls(1) supports, +# and try to create it if not +( + # Create a directory to hold metadata about ls(1) + lcd=$HOME/.cache/ls + if ! [ -d "$lcd" ] ; then + mkdir -p -- "$lcd" || exit + fi -# Run dircolors(1) to export LS_COLORS if available and appropriate -case $ls_help in - *--color*) - if command -v dircolors >/dev/null 2>&1 ; then - if [ -r "$HOME"/.dircolors ] ; then - eval "$(dircolors --sh -- "$HOME"/.dircolors)" - else - eval "$(dircolors --sh)" - fi + # Write ls(1)'s --help output to a file, even if it's empty + if ! [ -f "$lcd"/help ] ; then + ls --help >"$lcd"/help 2>/dev/null || exit + + # Iterate through some useful options and create files to show they're + # available + if grep -q -- --color "$lcd"/help ; then + touch -- "$lcd"/color || exit fi - ;; -esac + fi +) || return -# We're done parsing ls(1)'s --help output now -unset -v ls_help +# Run dircolors(1) to export LS_COLORS if available and appropriate +if [ -e "$HOME"/.cache/ls/color ] ; then + if command -v dircolors >/dev/null ; then + if [ -r "$HOME"/.dircolors ] ; then + eval "$(dircolors --sh -- "$HOME"/.dircolors)" + else + eval "$(dircolors --sh)" + fi + fi +fi -- cgit v1.2.3