Skip to content

Commit 844ddf8

Browse files
committed
Merge pull request #61 from supercrabtree/use-lscolors-on-osx
Use lscolors on osx
2 parents 725f736 + dea46c3 commit 844ddf8

File tree

1 file changed

+99
-29
lines changed

1 file changed

+99
-29
lines changed

k.sh

Lines changed: 99 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,43 @@ k () {
9292
base_dirs=($@)
9393
fi
9494

95+
96+
# Colors
97+
# ----------------------------------------------------------------------------
98+
# default colors
99+
K_COLOR_DI="0;34" # di:directory
100+
K_COLOR_LN="0;35" # ln:symlink
101+
K_COLOR_SO="0;32" # so:socket
102+
K_COLOR_PI="0;33" # pi:pipe
103+
K_COLOR_EX="0;31" # ex:executable
104+
K_COLOR_BD="34;46" # bd:block special
105+
K_COLOR_CD="34;43" # cd:character special
106+
K_COLOR_SU="30;41" # su:executable with setuid bit set
107+
K_COLOR_SG="30;46" # sg:executable with setgid bit set
108+
K_COLOR_TW="30;42" # tw:directory writable to others, with sticky bit
109+
K_COLOR_OW="30;43" # ow:directory writable to others, without sticky bit
110+
111+
# read colors if osx and $LSCOLORS is defined
112+
if [[ $(uname) == 'Darwin' && -n $LSCOLORS ]]; then
113+
# Translate OSX/BSD's LSCOLORS so we can use the same here
114+
K_COLOR_DI=$(_k_bsd_to_ansi $LSCOLORS[1] $LSCOLORS[2])
115+
K_COLOR_LN=$(_k_bsd_to_ansi $LSCOLORS[3] $LSCOLORS[4])
116+
K_COLOR_SO=$(_k_bsd_to_ansi $LSCOLORS[5] $LSCOLORS[6])
117+
K_COLOR_PI=$(_k_bsd_to_ansi $LSCOLORS[7] $LSCOLORS[8])
118+
K_COLOR_EX=$(_k_bsd_to_ansi $LSCOLORS[9] $LSCOLORS[10])
119+
K_COLOR_BD=$(_k_bsd_to_ansi $LSCOLORS[11] $LSCOLORS[12])
120+
K_COLOR_CD=$(_k_bsd_to_ansi $LSCOLORS[13] $LSCOLORS[14])
121+
K_COLOR_SU=$(_k_bsd_to_ansi $LSCOLORS[15] $LSCOLORS[16])
122+
K_COLOR_SG=$(_k_bsd_to_ansi $LSCOLORS[17] $LSCOLORS[18])
123+
K_COLOR_TW=$(_k_bsd_to_ansi $LSCOLORS[17] $LSCOLORS[18])
124+
K_COLOR_OW=$(_k_bsd_to_ansi $LSCOLORS[19] $LSCOLORS[20])
125+
fi
126+
127+
# read colors if linux and $LS_COLORS is defined
128+
# if [[ $(uname) == 'Linux' && -n $LS_COLORS ]]; then
129+
130+
# fi
131+
95132
# ----------------------------------------------------------------------------
96133
# Loop over passed directories and files to display
97134
# ----------------------------------------------------------------------------
@@ -249,7 +286,7 @@ k () {
249286
typeset PERMISSIONS HARDLINKCOUNT OWNER GROUP FILESIZE FILESIZE_OUT DATE NAME SYMLINK_TARGET
250287
typeset FILETYPE PER1 PER2 PER3 PERMISSIONS_OUTPUT STATUS
251288
typeset TIME_DIFF TIME_COLOR DATE_OUTPUT
252-
typeset -i IS_DIRECTORY IS_SYMLINK IS_EXECUTABLE
289+
typeset -i IS_DIRECTORY IS_SYMLINK IS_SOCKET IS_PIPE IS_EXECUTABLE IS_BLOCK_SPECIAL IS_CHARACTER_SPECIAL HAS_UID_BIT HAS_GID_BIT HAS_STICKY_BIT IS_WRITABLE_BY_OTHERS
253290
typeset -i COLOR
254291

255292
k=1
@@ -261,7 +298,14 @@ k () {
261298
REPOMARKER=" "
262299
IS_DIRECTORY=0
263300
IS_SYMLINK=0
301+
IS_SOCKET=0
302+
IS_PIPE=0
264303
IS_EXECUTABLE=0
304+
IS_BLOCK_SPECIAL=0
305+
IS_CHARACTER_SPECIAL=0
306+
HAS_UID_BIT=0
307+
HAS_GID_BIT=0
308+
HAS_STICKY_BIT=0
265309

266310
PERMISSIONS="${sv[mode]}"
267311
HARDLINKCOUNT="${sv[nlink]}"
@@ -274,7 +318,16 @@ k () {
274318

275319
# Check for file types
276320
if [[ -d "$NAME" ]]; then IS_DIRECTORY=1; fi
277-
if [[ -L "$NAME" ]]; then IS_SYMLINK=1; fi
321+
if [[ -L "$NAME" ]]; then IS_SYMLINK=1; fi
322+
if [[ -S "$NAME" ]]; then IS_SOCKET=1; fi
323+
if [[ -p "$NAME" ]]; then IS_PIPE=1; fi
324+
if [[ -x "$NAME" ]]; then IS_EXECUTABLE=1; fi
325+
if [[ -b "$NAME" ]]; then IS_BLOCK_SPECIAL=1; fi
326+
if [[ -c "$NAME" ]]; then IS_CHARACTER_SPECIAL=1; fi
327+
if [[ -u "$NAME" ]]; then HAS_UID_BIT=1; fi
328+
if [[ -g "$NAME" ]]; then HAS_GID_BIT=1; fi
329+
if [[ -k "$NAME" ]]; then HAS_STICKY_BIT=1; fi
330+
if [[ $PERMISSIONS[9] == 'w' ]]; then IS_WRITABLE_BY_OTHERS=1; fi
278331

279332
# IS_GIT_REPO is a 1 if $NAME is a file/directory in a git repo, OR if $NAME is a git-repo itself
280333
# GIT_TOPLEVEL is set to the directory containing the .git folder of a git-repo
@@ -318,16 +371,6 @@ k () {
318371
# --------------------------------------------------------------------------
319372
# Colour the first character based on filetype
320373
FILETYPE="${PERMISSIONS[1]}"
321-
if (( IS_DIRECTORY ))
322-
then
323-
FILETYPE=${FILETYPE//d/$'\e[1;36m'd$'\e[0m'};
324-
elif (( IS_SYMLINK ))
325-
then
326-
FILETYPE=${FILETYPE//l/$'\e[0;35m'l$'\e[0m'};
327-
elif [[ $FILETYPE == "-" ]];
328-
then
329-
FILETYPE=${FILETYPE//-/$'\e[0;37m'-$'\e[0m'};
330-
fi
331374

332375
# Permissions Owner
333376
PER1="${PERMISSIONS[2,4]}"
@@ -340,18 +383,8 @@ k () {
340383

341384
PERMISSIONS_OUTPUT="$FILETYPE$PER1$PER2$PER3"
342385

343-
# --x --x --x warning
344-
if [[ $PER1[3] == "x" || $PER2[3] == "x" || $PER3[3] == "x" ]]; then
345-
IS_EXECUTABLE=1
346-
fi
347-
348-
# --- --- rwx warning
349-
if [[ $PER3 == "rwx" ]] && (( ! IS_SYMLINK )); then
350-
PERMISSIONS_OUTPUT=$'\e[30;41m'"$PERMISSIONS"$'\e[0m'
351-
fi
352-
353386
# --------------------------------------------------------------------------
354-
# Colour the symlinks - TODO
387+
# Colour the symlinks
355388
# --------------------------------------------------------------------------
356389

357390
# --------------------------------------------------------------------------
@@ -441,12 +474,22 @@ k () {
441474
# But we don't want to quote '.'; so instead we escape the escape manually and use q-
442475
NAME="${${NAME##*/}//$'\e'/\\e}" # also propagate changes to SYMLINK_TARGET below
443476

444-
if (( IS_DIRECTORY ))
445-
then
446-
NAME=$'\e[38;5;32m'"$NAME"$'\e[0m'
447-
elif (( IS_SYMLINK ))
448-
then
449-
NAME=$'\e[0;35m'"$NAME"$'\e[0m'
477+
if [[ $IS_DIRECTORY == 1 ]]; then
478+
if [[ $IS_WRITABLE_BY_OTHERS == 1 ]]; then
479+
if [[ $HAS_STICKY_BIT == 1 ]]; then
480+
NAME=$'\e['"$K_COLOR_TW"'m'"$NAME"$'\e[0m';
481+
fi
482+
NAME=$'\e['"$K_COLOR_OW"'m'"$NAME"$'\e[0m';
483+
fi
484+
NAME=$'\e['"$K_COLOR_DI"'m'"$NAME"$'\e[0m';
485+
elif [[ $IS_SYMLINK == 1 ]]; then NAME=$'\e['"$K_COLOR_LN"'m'"$NAME"$'\e[0m';
486+
elif [[ $IS_SOCKET == 1 ]]; then NAME=$'\e['"$K_COLOR_SO"'m'"$NAME"$'\e[0m';
487+
elif [[ $IS_PIPE == 1 ]]; then NAME=$'\e['"$K_COLOR_PI"'m'"$NAME"$'\e[0m';
488+
elif [[ $HAS_UID_BIT == 1 ]]; then NAME=$'\e['"$K_COLOR_SU"'m'"$NAME"$'\e[0m';
489+
elif [[ $HAS_GID_BIT == 1 ]]; then NAME=$'\e['"$K_COLOR_SG"'m'"$NAME"$'\e[0m';
490+
elif [[ $IS_EXECUTABLE == 1 ]]; then NAME=$'\e['"$K_COLOR_EX"'m'"$NAME"$'\e[0m';
491+
elif [[ $IS_BLOCK_SPECIAL == 1 ]]; then NAME=$'\e['"$K_COLOR_BD"'m'"$NAME"$'\e[0m';
492+
elif [[ $IS_CHARACTER_SPECIAL == 1 ]]; then NAME=$'\e['"$K_COLOR_CD"'m'"$NAME"$'\e[0m';
450493
fi
451494

452495
# --------------------------------------------------------------------------
@@ -464,5 +507,32 @@ k () {
464507
done
465508
}
466509

510+
_k_bsd_to_ansi() {
511+
local foreground=$1 background=$2 foreground_ansi background_ansi
512+
case $foreground in
513+
a) foreground_ansi=30;;
514+
b) foreground_ansi=31;;
515+
c) foreground_ansi=32;;
516+
d) foreground_ansi=33;;
517+
e) foreground_ansi=34;;
518+
f) foreground_ansi=35;;
519+
g) foreground_ansi=36;;
520+
h) foreground_ansi=37;;
521+
x) foreground_ansi=0;;
522+
esac
523+
case $background in
524+
a) background_ansi=40;;
525+
b) background_ansi=41;;
526+
c) background_ansi=42;;
527+
d) background_ansi=43;;
528+
e) background_ansi=44;;
529+
f) background_ansi=45;;
530+
g) background_ansi=46;;
531+
h) background_ansi=47;;
532+
x) background_ansi=0;;
533+
esac
534+
printf "%s;%s" $foreground_ansi $background_ansi
535+
}
536+
467537
# http://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg
468538
# vim: set ts=2 sw=2 ft=zsh et :

0 commit comments

Comments
 (0)