scripts/t: handle arbitrary depths

This commit is contained in:
Michele Guerini Rocco 2022-01-21 11:05:56 +01:00
parent c8b37c7b04
commit 9381733ab1
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -1,11 +1,25 @@
#!/bin/sh
# compact version of tree
# max directories depth
level=$(echo "$@" | sed -nE 's/.*-L ?([0-9]).*/\1/p')
# indentatiof of leaf directory
begin=$(printf "%$((level - 2))s" | sed 's/ /│   /g')
# indentation of leaf files
line=$(printf "%$((level - 1))s" | sed 's/ /│   /g')
# limit to 6 files and add ellipses
res=$(tree --dirsfirst --noreport -L 2 -C "$@" \
| awk '/^├|└/{l1=0} /^│/{l1+=1} {if (l1 <= 5){print $0} if(l1 == 6){print "│   ├── ..."}}' \
| awk -v begin="$begin" -v line="$line" '
($0 ~ "^"begin"├|└") {count=0}
($0 ~ "^"line) {count+=1}
{if (count <= 5){print $0} if(count == 6){print line"├── ..."}}
' \
| sed 's/ / /g')
# use a pager when output doesn't fit the screen
if test $(printf "%s\n" "$res" | wc -l) -gt $(tput lines)
then printf "%s\n" "$res" | less -RSicX
then printf "%s\n" "$res" | $PAGER -RSicX
else printf "%s\n" "$res"
fi