From 3a54dcabb83174496edc8e499e020468f2cceeb4 Mon Sep 17 00:00:00 2001 From: xPMo Date: Sat, 9 Mar 2019 23:48:20 -0600 Subject: [PATCH 1/2] Implement highlighting of permissions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two methods, set by K_PERM_COLOR abs*: u ⇒ green, g ⇒ yellow, o ⇒ red rel*: u,g ⇒ fade if user doesn't have UID/GID --- k.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 19 deletions(-) diff --git a/k.sh b/k.sh index 7a50b33..fb19a04 100644 --- a/k.sh +++ b/k.sh @@ -8,7 +8,7 @@ k () { # Stop stat failing when a directory contains either no files or no hidden files # Track if we _accidentally_ create a new global variable - setopt local_options null_glob typeset_silent no_auto_pushd nomarkdirs + setopt local_options null_glob typeset_silent no_auto_pushd nomarkdirs extendedglob # Process options and get files/directories typeset -a o_all o_almost_all o_human o_si o_directory o_group_directories \ @@ -287,14 +287,15 @@ k () { typeset -i i=1 j=1 k=1 typeset -a STATS_PARAMS_LIST typeset fn statvar h - typeset -A sv + typeset -A nsv sv STATS_PARAMS_LIST=() for fn in $show_list do statvar="stats_$i" typeset -A $statvar - zstat -H $statvar -Lsn -F "%s^%d^%b^%H:%M^%Y" -- "$fn" # use lstat, render mode/uid/gid to strings + zstat -H $statvar -Ln -F "%s^%d^%b^%H:%M^%Y" -- "$fn" # use lstat, render mode/uid/gid to strings + zstat -H "n$statvar" -Ln -- "$fn" # use numeric versions of mode/uid/gid STATS_PARAMS_LIST+=($statvar) i+=1 done @@ -337,7 +338,9 @@ k () { k=1 for statvar in "${STATS_PARAMS_LIST[@]}" do + nstatvar="n$statvar" sv=("${(@Pkv)statvar}") + nsv=("${(@Pkv)nstatvar}") # We check if the result is a git repo later, so set a blank marker indication the result is not a git repo REPOMARKER=" " @@ -414,31 +417,70 @@ k () { FILESIZE_OUT="${(l:MAX_LEN[5]:)FILESIZE_OUT}" # -------------------------------------------------------------------------- - # Colour the permissions - TODO + # Colour the permissions # -------------------------------------------------------------------------- - # Colour the first character based on filetype - FILETYPE="${PERMISSIONS[1]}" - - # Permissions Owner - PER1="${PERMISSIONS[2,4]}" + if (( IS_SYMLINK )); then + PERMISSIONS_OUTPUT=$'\e[38;5;'"$K_COLOR_LN"'m'"$PERMISSIONS"$'\e[0m' + else + case $K_PERM_COLOR in + abs*) + local color + # Colour the first character based on filetype + FILETYPE="${PERMISSIONS[1]}" + + local reset=$'\e[0m' + # Permissions User + color=$'\e[38;5;2m' + PER1="${PERMISSIONS[2,4]//(#m)[^\-]/$color$MATCH$reset}" + + # Permissions Group + color=$'\e[38;5;3m' + PER2="${PERMISSIONS[5,7]//(#m)[^\-]/$color$MATCH$reset}" + + # Permissions Other + color=$'\e[38;5;1m' + PER3="${PERMISSIONS[8,10]//(#m)[^\-]/${color}$MATCH$reset}" + + PERMISSIONS_OUTPUT="$FILETYPE$PER1$PER2$PER3" + ;; + rel*) + # relative: fade unless user matches + local color=$'\e[38;5;8m' + local reset=$'\e[0m' + # Colour the first character based on filetype + FILETYPE="${PERMISSIONS[1]}" + + # Permissions User + if (( UID == nsv[uid] )); then + PER1="${PERMISSIONS[2,4]}" + PER1="${PERMISSIONS[2,4]//(#m)[\-]/$color$MATCH$reset}" + else + PER1="$color${PERMISSIONS[2,4]}$reset" + fi - # Permissions Group - PER2="${PERMISSIONS[5,7]}" + # Permissions Group + if (( usergroups[(r)${nsv[gid]}] )); then + PER2="${PERMISSIONS[5,7]}" + PER2="${PERMISSIONS[5,7]//(#m)[\-]/$color$MATCH$reset}" + else + PER2="$color${PERMISSIONS[5,7]}$reset" + fi - # Permissions User - PER3="${PERMISSIONS[8,10]}" + # Permissions Other + PER3="${PERMISSIONS[8,10]//(#m)[\-]/$color$MATCH$reset}" - PERMISSIONS_OUTPUT="$FILETYPE$PER1$PER2$PER3" + PERMISSIONS_OUTPUT="$FILETYPE$PER1$PER2$PER3" + ;; + *) PERMISSIONS_OUTPUT="$PERMISSIONS" + esac - # -------------------------------------------------------------------------- - # Colour the symlinks - # -------------------------------------------------------------------------- + fi # -------------------------------------------------------------------------- # Colour Owner and Group # -------------------------------------------------------------------------- - OWNER=$'\e[38;5;241m'"$OWNER"$'\e[0m' - GROUP=$'\e[38;5;241m'"$GROUP"$'\e[0m' + (( UID == nsv[uid] )) || OWNER=$'\e[38;5;241m'"$OWNER"$'\e[0m' + (( usergroups[(r)${nsv[gid]}] )) || GROUP=$'\e[38;5;241m'"$GROUP"$'\e[0m' # -------------------------------------------------------------------------- # Colour file weights From ca4ca4599c3179924ae55959c2d9a61f396b410e Mon Sep 17 00:00:00 2001 From: xPMo Date: Mon, 11 Mar 2019 04:09:46 -0500 Subject: [PATCH 2/2] Fade (rwx) permissions together on rel method --- k.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/k.sh b/k.sh index fb19a04..ea2ae06 100644 --- a/k.sh +++ b/k.sh @@ -453,7 +453,6 @@ k () { # Permissions User if (( UID == nsv[uid] )); then PER1="${PERMISSIONS[2,4]}" - PER1="${PERMISSIONS[2,4]//(#m)[\-]/$color$MATCH$reset}" else PER1="$color${PERMISSIONS[2,4]}$reset" fi @@ -461,13 +460,11 @@ k () { # Permissions Group if (( usergroups[(r)${nsv[gid]}] )); then PER2="${PERMISSIONS[5,7]}" - PER2="${PERMISSIONS[5,7]//(#m)[\-]/$color$MATCH$reset}" else PER2="$color${PERMISSIONS[5,7]}$reset" fi - # Permissions Other - PER3="${PERMISSIONS[8,10]//(#m)[\-]/$color$MATCH$reset}" + PER3="${PERMISSIONS[8,10]}" PERMISSIONS_OUTPUT="$FILETYPE$PER1$PER2$PER3" ;;