mirror of
https://github.com/notwa/rc
synced 2024-11-05 03:29:02 -08:00
160 lines
3.4 KiB
Bash
Executable file
160 lines
3.4 KiB
Bash
Executable file
#!/bin/zsh
|
|
# TODO: safe-ify echos and prints
|
|
local din=${1:-$(pwd)}
|
|
din=${din%/} # TODO: hackish
|
|
|
|
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)
|
|
local i= $=colors ${(U)=colors} clr=$'\e[0m'
|
|
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 -a ft fut fct fs fus fcs
|
|
|
|
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
|
|
'
|
|
|
|
local s_fs='
|
|
T*8 T red
|
|
T G red
|
|
G*8 G pur
|
|
G M pur
|
|
M M yel
|
|
M K yel
|
|
K*8 K CYA
|
|
K B CYA
|
|
1 B cya
|
|
0 B BLA
|
|
'
|
|
|
|
local permcolors="$BLA $WHI $yel $YEL $blu $BLU $gre $GRE"
|
|
permcolors=($=permcolors)
|
|
|
|
# chr dir blk file sym
|
|
local typecolors="$RED $BLA $BLA $BLA $BLU $BLA $BLA $BLA $clr $BLA $CYA"
|
|
typecolors=($=typecolors)
|
|
|
|
local min unit color
|
|
for min unit color in $=s_ft; do
|
|
# TODO: consider dereferencing color
|
|
ft+=($(($min)) $unit $color)
|
|
done
|
|
for min unit color in $=s_fs; do
|
|
fs+=($(($min)) $unit $color)
|
|
done
|
|
|
|
trunc() { # deprecate me for speed...
|
|
local len=${1:-$COLUMNS} c='…' L
|
|
local end=$((len-1))
|
|
while read -r L; do
|
|
[ ${#L} -gt $len ] && echo ${L[1,$end]}$c || echo $L
|
|
done
|
|
}
|
|
|
|
prettify() {
|
|
local min unit color
|
|
for min unit color in ${(P)2}; do
|
|
[ $1 -lt $min ] && continue
|
|
local unitified=$(($1/$unit))
|
|
echo -n "${(P)color}${(l:4:)unitified}$clr$unit "
|
|
break
|
|
done
|
|
}
|
|
|
|
min() {
|
|
local min=99999999999 x
|
|
for x; do [ $x -lt $min ] && min=$x; done
|
|
echo $min
|
|
}
|
|
|
|
max() {
|
|
local max=-99999999999 x
|
|
for x; do [ $x -gt $max ] && max=$x; done
|
|
echo $max
|
|
}
|
|
|
|
permprint() {
|
|
echo -n ${permcolors[$(($1+1))]}$1
|
|
}
|
|
|
|
typeprint() {
|
|
echo -n ${typecolors[$(($1+1))]} #${(l:3:)1}
|
|
}
|
|
|
|
local now=$(date +%s)
|
|
dateprint() {
|
|
[ $1 -lt 32 ] \
|
|
&& echo -n "$BLA N/A$clr " \
|
|
|| prettify $((now-$1)) ft
|
|
}
|
|
|
|
local dinf=$(readlink -f $din/)
|
|
local f="$dinf/*(D)"
|
|
|
|
stat -t -- ${~f} | while read -rA A; do
|
|
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
|
|
|
|
local bits=$((16#${A[-13]})) type= uperm= gperm= operm= our_perm=
|
|
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'
|
|
|
|
case $type in
|
|
2) echo -n ' c ' ;;
|
|
6) echo -n ' b ' ;;
|
|
4) echo -n ' / ' ;;
|
|
10) echo -n ' > ' ;;
|
|
*) prettify ${A[-15]} fs ;;
|
|
esac
|
|
|
|
permprint $our_perm
|
|
|
|
true && {
|
|
echo -n ' '
|
|
permprint $uperm
|
|
permprint $gperm
|
|
permprint $operm
|
|
}
|
|
|
|
echo -n "$clr"
|
|
|
|
local da=${A[-5]} dc=${A[-4]} dm=${A[-3]}
|
|
true && {
|
|
dateprint $da
|
|
dateprint $dm
|
|
dateprint $dc
|
|
} || {
|
|
dateprint $(max $da $dc $dm)
|
|
}
|
|
|
|
local n=${A[1,-16]}
|
|
n=${n##$dinf/}
|
|
typeprint $type
|
|
echo $n$clr
|
|
done
|