misc/scripts/t

26 lines
765 B
Plaintext
Raw Permalink Normal View History

2021-03-05 17:26:17 +01:00
#!/bin/sh
# compact version of tree
2022-01-21 11:05:56 +01:00
# max directories depth
level=$(echo "$@" | sed -nE 's/.*-L ?([0-9]).*/\1/p')
2022-09-18 21:03:02 +02:00
# indentation of leaf directory
2022-01-21 11:05:56 +01:00
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
2021-03-05 17:26:17 +01:00
res=$(tree --dirsfirst --noreport -L 2 -C "$@" \
2022-01-21 11:05:56 +01:00
| 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"├── ..."}}
' \
2021-03-05 17:26:17 +01:00
| sed 's/ / /g')
2022-01-21 11:05:56 +01:00
# use a pager when output doesn't fit the screen
2021-03-05 17:26:17 +01:00
if test $(printf "%s\n" "$res" | wc -l) -gt $(tput lines)
2022-01-21 11:05:56 +01:00
then printf "%s\n" "$res" | $PAGER -RSicX
2021-03-05 17:26:17 +01:00
else printf "%s\n" "$res"
fi