From 85da14dad0b345bf386d11ece0129564fe742bfd Mon Sep 17 00:00:00 2001 From: notwa Date: Tue, 14 May 2013 18:55:04 -0700 Subject: [PATCH 1/9] --- lsf.sh | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 lsf.sh diff --git a/lsf.sh b/lsf.sh new file mode 100644 index 0000000..affcd38 --- /dev/null +++ b/lsf.sh @@ -0,0 +1,166 @@ +#!/bin/bash +# ultra-fancy ultra-pointless `ls -l` alternative +# compatible with bash and zsh +# can be sourced or executed + +# to maintain zsh compatibility: +# $() is surrounded in double quotes +# echo -E is used to avoid \033 expanding +# bare asterisks are escaped to prevent globbing +# arrays are used instead of relying on word expansion +# for loops over ${array[@]} as it's the only commonality I know + +# TODO: set better defaults for quick glances at filesize +# TODO: utilize filetype; opt to strip path from output +# make sure to add / to path if necessary (warning: symlinks) +# TODO: rewrite trunc to use len + +_lsf_begin(){ + local begin=' +me='$UID' +'"$( +awk -F: '{print "unames["$3"]=\""$1"\""}' < /etc/passwd +awk -F: '{print "gnames["$3"]=\""$1"\""}' < /etc/group +for x in $(groups); do echo "us[\"$x\"]=1"; done +i=0;for x in bla red gre yel blu pur cya whi; do + echo -E $x'="\033[3'${i}'m"' + echo -n "$x" | tr '[a-z]' '[A-Z]' + echo -E '="\033[1;3'${i}'m"' + let i++ +done +)"' +clr="\033[0m" +BLD="\033[1m" +s=1 +m=60*s +h=60*m +d=24*h +y=365*d +B=1 +K=1024*B +M=1024*K +G=1024*M +T=1024*G +ff["time"]="%3d%s " clr +ff["size"]="%4d" clr "%s " +'"$( +for x in s m h d y;do echo "u[\"time,$x\"]=$x";done +for x in B K M G T;do echo "u[\"size,$x\"]=$x";done +ft=(-1 m m\*10 h h\*12 d d\*7 30\*d y y\*2 y\*10) +fut=(s s s m m h h d d d y y) +fct=(RED PUR pur YEL yel GRE gre CYA cya BLU blu BLA) +fs=( 0 K K\*8 M M\*8 G G\*8 T T\*8) +fus=(B B B K K M M G G T) +fcs=(BLA cya CYA CYA yel yel pur pur red red) +pc=(BLA WHI yel YEL blu BLU gre GRE) +i=0;for x in ${ft[@]}; do echo "f[\"time,$i\"]=$x"; let i++;done +echo "f[\"times\"]=$i" +i=0;for x in ${fs[@]}; do echo "f[\"size,$i\"]=$x"; let i++;done +echo "f[\"sizes\"]=$i" +i=0;for x in ${fut[@]};do echo "fu[\"time,$i\"]=\"$x\"";let i++;done +i=0;for x in ${fus[@]};do echo "fu[\"size,$i\"]=\"$x\"";let i++;done +i=0;for x in ${fct[@]};do echo "fc[\"time,$i\"]=$x"; let i++;done +i=0;for x in ${fcs[@]};do echo "fc[\"size,$i\"]=$x"; let i++;done +i=0;for x in ${pc[@]}; do echo "pc[$i]=$x"; let i++;done +)" + echo -E "$begin" +} + +_lsf_cached= + +lsf(){ + local o_showallperm=1 o_showuser=1 o_showgroup=1 + local o_mostrecent=0 opts=mgupf opt + while getopts $opts'h' opt; do + case $opt in + f) _lsf_cached=;; + p) o_showallperm=0;; + u) o_showuser=0;; + g) o_showgroup=0;; + m) o_mostrecent=1;; + ?) echo "usage: $0 [-$opts] [dir]" + return 1;; + esac + done + + [ "$_lsf_cached" ] || _lsf_cached="$(_lsf_begin)" + local program='BEGIN{'"$_lsf_cached"' +now='"$(date +%s)"' +} +function printff(id, n) { + len=f[id "s"] + for(i=0;i<=len;i++) { + idi=id "," i + if(i!=len && n>f[idi]) continue + unit=fu[idi] + printf(fc[idi] ff[id], n/u[id "," unit], unit) + break + } +} + +function trunc(str, len) { + # for the moment len doesnt do anything + return gensub(/(.{5})..+/,"\\1…",1,sprintf("%6s",str)) +} + +{ + printff("size", $(NF-14)) + + uid=$(NF-11) + gid=$(NF-10) + is_me=(uid==me) + is_us=(gnames[gid] in us) + + gross=("0x" $(NF-12))+0 + type=rshift(gross, 9) + operm=and(gross, 07) + gperm=rshift(and(gross, 070), 3) + uperm=rshift(and(gross, 0700), 6) + our_perm=or(or((is_me)?uperm:0, (is_us)?gperm:0), operm) + + printf(pc[our_perm] "%o " clr, our_perm) + if('$o_showallperm') { + printf(pc[uperm] "%o" clr, uperm) + printf(pc[gperm] "%o" clr, gperm) + printf(pc[operm] "%o " clr, operm) + } + + if('$o_showuser') { + name=trunc((uid in unames)?unames[uid]:uid, 6) + if(is_me) name=YEL name clr + printf("%s ", name) + } + + if('$o_showgroup') { + name=trunc((gid in gnames)?gnames[gid]:gid, 6) + if(is_us) name=YEL name clr + printf("%s ", name) + } + + da=$(NF-4) + dc=$(NF-3) + dm=$(NF-2) + if(!'$o_mostrecent') { + printff("time", now-da) + printff("time", now-dm) + printff("time", now-dc) + } else { + max=(da>dm)?da:dm + max=(max>dc)?max:dc + printff("time", now-max) + } + + # print filename by killing all fields not part of it + NF-=15 + print +}' + + shift $(($OPTIND-1)) + find "${1:-.}" -maxdepth 1 -print0 | xargs -0 stat -t | awk --non-decimal-data "$program" +} + +case $- in + *i*) ;; + *) lsf -f +esac + From 6e286aec91821ffefc8dd81a7a113b09aeac0753 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 14 May 2013 19:07:50 -0700 Subject: [PATCH 2/9] trunc len --- lsf.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) mode change 100644 => 100755 lsf.sh diff --git a/lsf.sh b/lsf.sh old mode 100644 new mode 100755 index affcd38..a39be64 --- a/lsf.sh +++ b/lsf.sh @@ -14,6 +14,7 @@ # TODO: utilize filetype; opt to strip path from output # make sure to add / to path if necessary (warning: symlinks) # TODO: rewrite trunc to use len +# TODO: include sorting somehow _lsf_begin(){ local begin=' @@ -99,8 +100,11 @@ function printff(id, n) { } function trunc(str, len) { - # for the moment len doesnt do anything - return gensub(/(.{5})..+/,"\\1…",1,sprintf("%6s",str)) + e=length(str)>len?"…":"" + return substr(str,0,len-(e?1:0)) e +} +function fixlen(str, len) { + return trunc(sprintf("%" len "s", str), len) } { @@ -126,13 +130,13 @@ function trunc(str, len) { } if('$o_showuser') { - name=trunc((uid in unames)?unames[uid]:uid, 6) + name=fixlen((uid in unames)?unames[uid]:uid, 6) if(is_me) name=YEL name clr printf("%s ", name) } if('$o_showgroup') { - name=trunc((gid in gnames)?gnames[gid]:gid, 6) + name=fixlen((gid in gnames)?gnames[gid]:gid, 6) if(is_us) name=YEL name clr printf("%s ", name) } From ea6bf420b2e3aa498a44ad7957e29aa7cb7cd717 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 14 May 2013 19:08:38 -0700 Subject: [PATCH 3/9] forgot --- lsf.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/lsf.sh b/lsf.sh index a39be64..b628105 100755 --- a/lsf.sh +++ b/lsf.sh @@ -13,7 +13,6 @@ # TODO: set better defaults for quick glances at filesize # TODO: utilize filetype; opt to strip path from output # make sure to add / to path if necessary (warning: symlinks) -# TODO: rewrite trunc to use len # TODO: include sorting somehow _lsf_begin(){ From 31ebe0b64928c0d2bb4e75a7a667519fb019dd46 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 20 May 2013 12:54:10 -0700 Subject: [PATCH 4/9] lint --- lsf.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lsf.sh b/lsf.sh index b628105..adbfc6c 100755 --- a/lsf.sh +++ b/lsf.sh @@ -8,7 +8,7 @@ # echo -E is used to avoid \033 expanding # bare asterisks are escaped to prevent globbing # arrays are used instead of relying on word expansion -# for loops over ${array[@]} as it's the only commonality I know +# for loops over "${array[@]}" as it works the same in both # TODO: set better defaults for quick glances at filesize # TODO: utilize filetype; opt to strip path from output @@ -24,7 +24,7 @@ awk -F: '{print "gnames["$3"]=\""$1"\""}' < /etc/group for x in $(groups); do echo "us[\"$x\"]=1"; done i=0;for x in bla red gre yel blu pur cya whi; do echo -E $x'="\033[3'${i}'m"' - echo -n "$x" | tr '[a-z]' '[A-Z]' + echo -n "$x" | tr a-z A-Z echo -E '="\033[1;3'${i}'m"' let i++ done @@ -53,15 +53,15 @@ fs=( 0 K K\*8 M M\*8 G G\*8 T T\*8) fus=(B B B K K M M G G T) fcs=(BLA cya CYA CYA yel yel pur pur red red) pc=(BLA WHI yel YEL blu BLU gre GRE) -i=0;for x in ${ft[@]}; do echo "f[\"time,$i\"]=$x"; let i++;done +i=0;for x in "${ft[@]}"; do echo "f[\"time,$i\"]=$x"; let i++;done echo "f[\"times\"]=$i" -i=0;for x in ${fs[@]}; do echo "f[\"size,$i\"]=$x"; let i++;done +i=0;for x in "${fs[@]}"; do echo "f[\"size,$i\"]=$x"; let i++;done echo "f[\"sizes\"]=$i" -i=0;for x in ${fut[@]};do echo "fu[\"time,$i\"]=\"$x\"";let i++;done -i=0;for x in ${fus[@]};do echo "fu[\"size,$i\"]=\"$x\"";let i++;done -i=0;for x in ${fct[@]};do echo "fc[\"time,$i\"]=$x"; let i++;done -i=0;for x in ${fcs[@]};do echo "fc[\"size,$i\"]=$x"; let i++;done -i=0;for x in ${pc[@]}; do echo "pc[$i]=$x"; let i++;done +i=0;for x in "${fut[@]}";do echo "fu[\"time,$i\"]=\"$x\"";let i++;done +i=0;for x in "${fus[@]}";do echo "fu[\"size,$i\"]=\"$x\"";let i++;done +i=0;for x in "${fct[@]}";do echo "fc[\"time,$i\"]=$x"; let i++;done +i=0;for x in "${fcs[@]}";do echo "fc[\"size,$i\"]=$x"; let i++;done +i=0;for x in "${pc[@]}"; do echo "pc[$i]=$x"; let i++;done )" echo -E "$begin" } @@ -158,8 +158,8 @@ function fixlen(str, len) { print }' - shift $(($OPTIND-1)) - find "${1:-.}" -maxdepth 1 -print0 | xargs -0 stat -t | awk --non-decimal-data "$program" + shift $((OPTIND-1)) + find "${1:-.}" -maxdepth 1 -exec stat -t {} + | awk --non-decimal-data "$program" } case $- in From 5865c56fd821ba7e22b01fe597f535f2a362cb16 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 20 May 2013 16:58:54 -0700 Subject: [PATCH 5/9] refactor --- lsf.sh | 60 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lsf.sh b/lsf.sh index adbfc6c..6fc0f73 100755 --- a/lsf.sh +++ b/lsf.sh @@ -67,26 +67,7 @@ i=0;for x in "${pc[@]}"; do echo "pc[$i]=$x"; let i++;done } _lsf_cached= - -lsf(){ - local o_showallperm=1 o_showuser=1 o_showgroup=1 - local o_mostrecent=0 opts=mgupf opt - while getopts $opts'h' opt; do - case $opt in - f) _lsf_cached=;; - p) o_showallperm=0;; - u) o_showuser=0;; - g) o_showgroup=0;; - m) o_mostrecent=1;; - ?) echo "usage: $0 [-$opts] [dir]" - return 1;; - esac - done - - [ "$_lsf_cached" ] || _lsf_cached="$(_lsf_begin)" - local program='BEGIN{'"$_lsf_cached"' -now='"$(date +%s)"' -} +_lsf_program=' function printff(id, n) { len=f[id "s"] for(i=0;i<=len;i++) { @@ -122,19 +103,19 @@ function fixlen(str, len) { our_perm=or(or((is_me)?uperm:0, (is_us)?gperm:0), operm) printf(pc[our_perm] "%o " clr, our_perm) - if('$o_showallperm') { + if(OSP) { printf(pc[uperm] "%o" clr, uperm) printf(pc[gperm] "%o" clr, gperm) printf(pc[operm] "%o " clr, operm) } - if('$o_showuser') { + if(OSU) { name=fixlen((uid in unames)?unames[uid]:uid, 6) if(is_me) name=YEL name clr printf("%s ", name) } - if('$o_showgroup') { + if(OSG) { name=fixlen((gid in gnames)?gnames[gid]:gid, 6) if(is_us) name=YEL name clr printf("%s ", name) @@ -143,14 +124,14 @@ function fixlen(str, len) { da=$(NF-4) dc=$(NF-3) dm=$(NF-2) - if(!'$o_mostrecent') { - printff("time", now-da) - printff("time", now-dm) - printff("time", now-dc) - } else { + if(OMR) { max=(da>dm)?da:dm max=(max>dc)?max:dc printff("time", now-max) + } else { + printff("time", now-da) + printff("time", now-dm) + printff("time", now-dc) } # print filename by killing all fields not part of it @@ -158,12 +139,31 @@ function fixlen(str, len) { print }' +lsf(){ + local o_showallperm=1 o_showuser=1 o_showgroup=1 + local o_mostrecent=0 opts=mgupf opt + while getopts $opts'h' opt; do + case $opt in + f) _lsf_cached=;; + p) o_showallperm=0;; + u) o_showuser=0;; + g) o_showgroup=0;; + m) o_mostrecent=1;; + ?) echo "usage: $0 [-$opts] [dir]" + return 1;; + esac + done + + [ "$_lsf_cached" ] || _lsf_cached="$(_lsf_begin)" + shift $((OPTIND-1)) - find "${1:-.}" -maxdepth 1 -exec stat -t {} + | awk --non-decimal-data "$program" + find "${1:-.}" -maxdepth 1 -exec stat -t {} + | awk --non-decimal-data \ +-v"now=$(date +%s)" -v"OMR=$o_mostrecent" -v"OSU=$o_showuser" \ +-v"OSG=$o_showgroup" -v"OSP=$o_showallperm" "BEGIN{$_lsf_cached}$_lsf_program" } case $- in *i*) ;; - *) lsf -f + *) lsf -f /var/lib esac From 2ed08389c9a1139173d894e1da5d93d21429cf9c Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 20 May 2013 17:09:40 -0700 Subject: [PATCH 6/9] oops --- lsf.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lsf.sh b/lsf.sh index 6fc0f73..c1c8202 100755 --- a/lsf.sh +++ b/lsf.sh @@ -164,6 +164,6 @@ lsf(){ case $- in *i*) ;; - *) lsf -f /var/lib + *) lsf -f esac From 2edb454851e1a8cf6284fc124052d0b25ae02a15 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 20 May 2013 20:10:49 -0700 Subject: [PATCH 7/9] strip path option --- lsf.sh | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lsf.sh b/lsf.sh index c1c8202..43e7e5a 100755 --- a/lsf.sh +++ b/lsf.sh @@ -11,8 +11,6 @@ # for loops over "${array[@]}" as it works the same in both # TODO: set better defaults for quick glances at filesize -# TODO: utilize filetype; opt to strip path from output -# make sure to add / to path if necessary (warning: symlinks) # TODO: include sorting somehow _lsf_begin(){ @@ -86,7 +84,6 @@ function trunc(str, len) { function fixlen(str, len) { return trunc(sprintf("%" len "s", str), len) } - { printff("size", $(NF-14)) @@ -95,11 +92,12 @@ function fixlen(str, len) { is_me=(uid==me) is_us=(gnames[gid] in us) - gross=("0x" $(NF-12))+0 - type=rshift(gross, 9) - operm=and(gross, 07) - gperm=rshift(and(gross, 070), 3) - uperm=rshift(and(gross, 0700), 6) + bits=("0x" $(NF-12))+0 + # note: we ignore the set and sticky bits... for now + type=rshift(and(bits, 0170000), 12) + operm=and(bits, 07) + gperm=rshift(and(bits, 070), 3) + uperm=rshift(and(bits, 0700), 6) our_perm=or(or((is_me)?uperm:0, (is_us)?gperm:0), operm) printf(pc[our_perm] "%o " clr, our_perm) @@ -111,13 +109,13 @@ function fixlen(str, len) { if(OSU) { name=fixlen((uid in unames)?unames[uid]:uid, 6) - if(is_me) name=YEL name clr + if(is_me) name=WHI name clr printf("%s ", name) } if(OSG) { name=fixlen((gid in gnames)?gnames[gid]:gid, 6) - if(is_us) name=YEL name clr + if(is_us) name=WHI name clr printf("%s ", name) } @@ -134,14 +132,21 @@ function fixlen(str, len) { printff("time", now-dc) } - # print filename by killing all fields not part of it + # acquire filename by killing all fields not part of it NF-=15 - print + fn=$0 + + if (!OSPA) { + if (NR!=1) fn=substr(fn,firstlen) + else firstlen=length(fn)+((type==04)?2:1) + } + + print fn }' lsf(){ local o_showallperm=1 o_showuser=1 o_showgroup=1 - local o_mostrecent=0 opts=mgupf opt + local o_mostrecent=0 o_showpath=1 opts=mgupfs opt while getopts $opts'h' opt; do case $opt in f) _lsf_cached=;; @@ -149,6 +154,7 @@ lsf(){ u) o_showuser=0;; g) o_showgroup=0;; m) o_mostrecent=1;; + s) o_showpath=0;; ?) echo "usage: $0 [-$opts] [dir]" return 1;; esac @@ -159,7 +165,8 @@ lsf(){ shift $((OPTIND-1)) find "${1:-.}" -maxdepth 1 -exec stat -t {} + | awk --non-decimal-data \ -v"now=$(date +%s)" -v"OMR=$o_mostrecent" -v"OSU=$o_showuser" \ --v"OSG=$o_showgroup" -v"OSP=$o_showallperm" "BEGIN{$_lsf_cached}$_lsf_program" +-v"OSG=$o_showgroup" -v"OSP=$o_showallperm" -v"OSPA=$o_showpath" \ +"BEGIN{$_lsf_cached}$_lsf_program" } case $- in From c8fc1d462e6b879d32e27c6aaee2ca9b61d5dcb6 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 20 May 2013 20:29:40 -0700 Subject: [PATCH 8/9] dumbest bug --- lsf.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lsf.sh b/lsf.sh index 43e7e5a..8cfa27c 100755 --- a/lsf.sh +++ b/lsf.sh @@ -12,6 +12,7 @@ # TODO: set better defaults for quick glances at filesize # TODO: include sorting somehow +# TODO: handle symlinks nicely _lsf_begin(){ local begin=' @@ -138,7 +139,7 @@ function fixlen(str, len) { if (!OSPA) { if (NR!=1) fn=substr(fn,firstlen) - else firstlen=length(fn)+((type==04)?2:1) + else firstlen=length(fn)+2-(fn ~ /\/$/) } print fn From dceb0894b35c55f750ce680f8b5e0d0919308042 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Fri, 21 Jun 2013 20:38:51 -0700 Subject: [PATCH 9/9] f that s --- lsf.sh | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lsf.sh b/lsf.sh index 8cfa27c..21d4804 100755 --- a/lsf.sh +++ b/lsf.sh @@ -1,7 +1,7 @@ #!/bin/bash # ultra-fancy ultra-pointless `ls -l` alternative -# compatible with bash and zsh -# can be sourced or executed +# to be sourced by bash or zsh +# similar project: https://github.com/trapd00r/ls-- # to maintain zsh compatibility: # $() is surrounded in double quotes @@ -13,6 +13,7 @@ # TODO: set better defaults for quick glances at filesize # TODO: include sorting somehow # TODO: handle symlinks nicely +# TODO: append WHI / clr to dir names _lsf_begin(){ local begin=' @@ -169,9 +170,3 @@ lsf(){ -v"OSG=$o_showgroup" -v"OSP=$o_showallperm" -v"OSPA=$o_showpath" \ "BEGIN{$_lsf_cached}$_lsf_program" } - -case $- in - *i*) ;; - *) lsf -f -esac -