1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 05:59:04 -08:00
rc/sh/lsz

179 lines
3.5 KiB
Text
Raw Normal View History

2013-06-28 05:22:14 -07:00
#!/bin/zsh
2014-11-02 21:36:44 -08:00
emulate -L zsh
setopt ksh_typeset typeset_silent
2013-06-28 05:22:14 -07:00
local -A unames gnames us
while IFS=: read -rA A; do unames[${A[3]}]=${A[1]} done < /etc/passwd
while IFS=: read -rA A; do gnames[${A[3]}]=${A[1]} done < /etc/group
for x in $(groups); do us[$x]=1 done
local -a colors
colors=(bla red gre yel blu pur cya whi)
2014-11-02 21:36:44 -08:00
local i $=colors ${(U)=colors} clr=$'\e[0m'
2013-06-28 05:22:14 -07:00
for i in {0..7}; do
local x=${colors[$((i+1))]}
eval "$x=$'\\e[3${i}m'" "${(U)x}=$'\\e[9${i}m'"
done
local s=1 m h d y B=1 K M G T
noglob let m=60*s h=60*m d=24*h y=365*d K=1024*B M=1024*K G=1024*M T=1024*G
local s_ft='
y*10 y BLA
y*2 y blu
y d BLU
d*30 d cya
d*7 d CYA
d h gre
h*12 h GRE
h m yel
m*10 m YEL
m s pur
0 s PUR
'
2014-10-30 08:55:12 -07:00
2013-06-28 05:22:14 -07:00
local s_fs='
T*8 T red
T G red
G*8 G pur
G M pur
M M yel
M K yel
2013-09-15 19:55:24 -07:00
K*8 K CYA
2013-06-28 05:22:14 -07:00
K B CYA
1 B cya
0 B BLA
'
local permcolors="$BLA $WHI $yel $YEL $blu $BLU $gre $GRE"
permcolors=($=permcolors)
2014-10-30 08:55:12 -07:00
# chr dir blk file sym
2014-11-02 21:36:44 -08:00
local typecolors="$RED $BLA $YEL $BLA $BLU $BLA $yel $BLA $clr $BLA $CYA"
2014-10-30 08:55:12 -07:00
typecolors=($=typecolors)
2014-11-02 21:36:44 -08:00
local ft fs
2013-06-28 05:22:14 -07:00
local min unit color
for min unit color in $=s_ft; do
ft+=($(($min)) $unit $color)
done
for min unit color in $=s_fs; do
fs+=($(($min)) $unit $color)
done
2014-11-03 10:39:24 -08:00
_lsz_pretty() {
2013-06-28 05:22:14 -07:00
local min unit color
for min unit color in ${(P)2}; do
[ $1 -lt $min ] && continue
local unitified=$(($1/$unit))
2014-11-02 21:36:44 -08:00
echo -n "${(P)color}${(l:4:)unitified}$clr$unit"
2013-06-28 05:22:14 -07:00
break
done
}
2014-11-03 10:39:24 -08:00
_lsz_min() {
2013-06-28 05:22:14 -07:00
local min=99999999999 x
for x; do [ $x -lt $min ] && min=$x; done
echo $min
}
2014-11-03 10:39:24 -08:00
_lsz_max() {
local _lsz_max=-99999999999 x
for x; do [ $x -gt $_lsz_max ] && _lsz_max=$x; done
echo $_lsz_max
2014-10-30 08:55:12 -07:00
}
2014-11-03 10:39:24 -08:00
_lsz_perm() {
2013-06-28 05:22:14 -07:00
echo -n ${permcolors[$(($1+1))]}$1
}
2014-11-03 10:39:24 -08:00
_lsz_type() {
2014-11-02 21:36:44 -08:00
echo -n ${typecolors[$(($1+1))]}
2014-10-30 08:55:12 -07:00
}
2013-06-28 05:22:14 -07:00
local now=$(date +%s)
2014-11-03 10:39:24 -08:00
_lsz_date() {
2014-10-30 08:55:12 -07:00
[ $1 -lt 32 ] \
2014-11-02 21:36:44 -08:00
&& echo -n "$BLA N/A$clr" \
2014-11-03 10:39:24 -08:00
|| _lsz_pretty $((now-$1)) ft
2014-11-02 21:36:44 -08:00
echo -n ' '
}
_lsz() {
2014-11-03 10:39:24 -08:00
local din=${1%/}
2014-11-02 21:36:44 -08:00
local dinf=$(readlink -f ${din:-/})
local glob=$dinf
[ -d "$dinf" ] && glob=$dinf/*(DN)
2014-11-03 10:39:24 -08:00
local count=${2:-0}
2014-11-02 21:36:44 -08:00
[ $count -gt 1 ] && echo "$WHI$dinf"
local -a matches
matches=(${~glob})
[ ${#matches} -eq 0 ] && return
stat -t -- ${matches[@]} | while read -r; do
__lsz "$REPLY" "$dinf"
done
2014-10-30 08:55:12 -07:00
}
2014-11-02 21:36:44 -08:00
__lsz() {
local A dinf=$2
read -rA A <<<$1
2013-06-28 05:22:14 -07:00
local uid=${A[-12]} gid=${A[-11]} is_me=0 is_us=0
local uname=${unames[$uid]} gname=${gnames[$gid]}
[ $uid -eq $UID ] && is_me=1
[ -n "$gname" ] && [[ ${us[$gname]} -eq 1 ]] && is_us=1
2014-11-02 21:36:44 -08:00
local bits type uperm gperm operm our_perm
bits=$((16#${A[-13]}))
2013-06-28 05:22:14 -07:00
let 'type=(bits & 8#170000) >> 12'
let 'uperm=(bits & 8#700) >> 6'
let 'gperm=(bits & 8#70) >> 3'
let 'operm=bits & 8#7'
let 'our_perm=(is_me?uperm:0) | (is_us?gperm:0) | operm'
2014-10-30 08:55:12 -07:00
case $type in
2014-11-02 21:36:44 -08:00
2) echo -n ' c' ;;
6) echo -n ' b' ;;
4) echo -n ' /' ;;
10) echo -n ' >' ;;
2014-11-03 10:39:24 -08:00
*) _lsz_pretty ${A[-15]} fs ;;
2014-10-30 08:55:12 -07:00
esac
2014-11-02 21:36:44 -08:00
true && {
echo -n ' '
2014-11-03 10:39:24 -08:00
_lsz_perm $our_perm
2014-11-02 21:36:44 -08:00
}
2013-06-28 05:22:14 -07:00
true && {
echo -n ' '
2014-11-03 10:39:24 -08:00
_lsz_perm $uperm
_lsz_perm $gperm
_lsz_perm $operm
2013-06-28 05:22:14 -07:00
}
echo -n "$clr"
local da=${A[-5]} dc=${A[-4]} dm=${A[-3]}
2014-10-30 08:55:12 -07:00
true && {
2014-11-03 10:39:24 -08:00
_lsz_date $da
_lsz_date $dm
_lsz_date $dc
2014-10-30 08:55:12 -07:00
} || {
2014-11-03 10:39:24 -08:00
_lsz_date $(_lsz_max $da $dc $dm)
2014-10-30 08:55:12 -07:00
}
2013-06-28 05:22:14 -07:00
2014-10-30 08:55:12 -07:00
local n=${A[1,-16]}
n=${n##$dinf/}
2014-11-03 10:39:24 -08:00
_lsz_type $type
2014-10-30 08:55:12 -07:00
echo $n$clr
2014-11-02 21:36:44 -08:00
}
local f
2014-11-03 10:39:24 -08:00
[ ${#@} -eq 0 ] && set -- $PWD
2014-11-02 21:36:44 -08:00
for f; _lsz $f ${#@}