From d351c44dcc8bbda1722af81a502229a1fba60328 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 15:35:52 +0000 Subject: [PATCH 1/6] pash: POSIX sh tree implementation --- pash | 64 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/pash b/pash index 2106fdd..b1d3b53 100755 --- a/pash +++ b/pash @@ -84,15 +84,59 @@ pw_copy() { } pw_list() { - set +f - find -- * -type f -name \*.gpg + find . -type f -name \*.gpg | sed 's#^\./##;s#\.gpg$##' } pw_tree() { - command -v tree >/dev/null 2>&1 || - die "'tree' command not found" - - tree --noreport + # This is a simple 'tree' implementation based + # around counting forward slashes in strings + # to calculate the nest level. + # + # Minus the 'find' call to grab the recursive + # file lists, this is entirely shell! + find . -mindepth 1 | 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 + [ -d "$path" ] && suffix=/ || suffix= + + # Strip the leading './' and the + # trailing '.gpg' where applicable. + path=${item##./} + path=${item%%.gpg} + + # 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() { @@ -209,12 +253,8 @@ main() { c*) pw_copy "$2" ;; d*) pw_del "$2" ;; s*) pw_show "$2" ;; - - # TODO: Better handle the removal - # of '.gpg' from list output. - l*) pw_list | sed 's/\.gpg$//' ;; - t*) pw_tree | sed 's/\.gpg$//' ;; - + l*) pw_list ;; + t*) pw_tree ;; *) usage esac } From 5c3eddf82da50c4a1fa5f5aff25d75237ccf9311 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 15:42:56 +0000 Subject: [PATCH 2/6] pash: longer comments --- pash | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/pash b/pash index b1d3b53..b4b96a9 100755 --- a/pash +++ b/pash @@ -88,20 +88,17 @@ pw_list() { } pw_tree() { - # This is a simple 'tree' implementation based - # around counting forward slashes in strings - # to calculate the nest level. + # This is a simple 'tree' implementation based around + # counting forward slashes to calculate the nest level. # - # Minus the 'find' call to grab the recursive - # file lists, this is entirely shell! + # Minus the 'find' call to grab the recursive file list, + # this is entirely shell! find . -mindepth 1 | while read -r item || { - # As this implementation is naive, we need - # to round off the bottom of the output. + # 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 + # 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 @@ -111,28 +108,24 @@ pw_tree() { }; do [ -d "$path" ] && suffix=/ || suffix= - # Strip the leading './' and the - # trailing '.gpg' where applicable. + # Strip the leading './' and the trailing '.gpg' if + # the path item contains them. path=${item##./} path=${item%%.gpg} - # Split the string on each forward slash - # to count the "nest level" of each - # file and directory. + # 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. + # 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. + # 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. + # 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}" From 7522c409016a12a741b5034c0d74d9eda837ab0f Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 15:52:47 +0000 Subject: [PATCH 3/6] pash: Fix bugs with tree --- pash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pash b/pash index b4b96a9..d1cd904 100755 --- a/pash +++ b/pash @@ -93,7 +93,7 @@ pw_tree() { # # Minus the 'find' call to grab the recursive file list, # this is entirely shell! - find . -mindepth 1 | while read -r item || { + find . -mindepth 1 | sort | while read -r item || { # As this implementation is naive, we need to round off # the bottom of the output. # @@ -106,12 +106,12 @@ pw_tree() { break }; do - [ -d "$path" ] && suffix=/ || suffix= + [ -d "$item" ] && suffix=/ || suffix= # Strip the leading './' and the trailing '.gpg' if # the path item contains them. path=${item##./} - path=${item%%.gpg} + path=${path%%.gpg} # Split the string on each forward slash to count the # "nest level" of each file and directory. From fc87de9c42a2a071b215845d11702a9b3492397c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 15:53:41 +0000 Subject: [PATCH 4/6] pash: update comment --- pash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pash b/pash index d1cd904..abf0852 100755 --- a/pash +++ b/pash @@ -91,8 +91,8 @@ pw_tree() { # This is a simple 'tree' implementation based around # counting forward slashes to calculate the nest level. # - # Minus the 'find' call to grab the recursive file list, - # this is entirely shell! + # 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. From f569fafa1272b80631ae37301b4f4e924421bcfb Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 16:18:40 +0000 Subject: [PATCH 5/6] pash: Don't display hidden files in tree --- pash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pash b/pash index abf0852..9446489 100755 --- a/pash +++ b/pash @@ -106,13 +106,13 @@ pw_tree() { break }; do - [ -d "$item" ] && suffix=/ || suffix= + glob "$item" './.*' && continue - # Strip the leading './' and the trailing '.gpg' if - # the path item contains them. path=${item##./} path=${path%%.gpg} + [ -d "$item" ] && suffix=/ || suffix= + # Split the string on each forward slash to count the # "nest level" of each file and directory. # From d027a947361e8546e82d570244360a00095a3cce Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 30 Nov 2019 17:04:40 +0000 Subject: [PATCH 6/6] pash (tree): fix bug in split --- pash | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pash b/pash index 9446489..cd21a9e 100755 --- a/pash +++ b/pash @@ -122,7 +122,11 @@ pw_tree() { # Disable warning about word splitting as it is # intentional here and globbing is disabled. # shellcheck disable=2086 - IFS=/ set -- $path; shift + { + IFS=/ + set -- $path + shift + } # Print the dividers from 1-nest_level. This is a simple # iteration over the shifted argument list.