Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

pash: POSIX sh tree implementation #11

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions pash
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,52 @@ pw_list() {
}

pw_tree() {
command -v tree >/dev/null 2>&1 ||
die "'tree' command not found"
# This is a simple 'tree' implementation based around
# counting forward slashes to calculate the nest level.
#
# Minus the 'find' and 'sort' calls to grab the recursive
# file list, this is entirely shell!
find . -mindepth 1 | sort | while read -r item || {
# As this implementation is naive, we need to round off
# the bottom of the output.
#
# This will execute once on the last iteration of the
# while loop. This simply uses the last nest level to
# close the tree.
printf └
for _; do printf ──┴; done
printf '\b┘\n'

break
}; do
glob "$item" './.*' && continue

path=${item##./}
path=${path%%.gpg}

tree --noreport | sed 's/\.gpg$//'
[ -d "$item" ] && suffix=/ || suffix=

# Split the string on each forward slash to count the
# "nest level" of each file and directory.
#
# The shift is added as the resulting count will always
# be one higher than intended.
#
# Disable warning about word splitting as it is
# intentional here and globbing is disabled.
# shellcheck disable=2086
{
IFS=/
set -- $path
shift
}

# Print the dividers from 1-nest_level. This is a simple
# iteration over the shifted argument list.
for _; do printf '│ '; done

printf '├─ %s\n' "${path##*/}${suffix}"
done
}

yn() {
Expand Down