dot

packages and services management
Log | Files | Refs | README

commit 91980316089486af0dde2e44e683c7c9b9f0197b
parent a371d9369f4f3efc4f1aaa3eb0270ff4cf4960ee
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed, 14 Mar 2018 20:14:59 +0100

splitting up into functions

Diffstat:
Mbin/lstree | 73++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 50 insertions(+), 23 deletions(-)

diff --git a/bin/lstree b/bin/lstree @@ -2,12 +2,11 @@ # list paths in a tree with some stat infos -# find walk the entire tree and then ({} + means call at the end) -# call ls -ld with all the result, which are then sorted. The -# result will be a list with all ls -l information and the the path -# name from the current directory (./). This pattern is used to -# delimit the find-style output, which is transformed into tree(1)-style -# output: +# find(1) walk the entire tree and then ({} + means call at the end) call +# ls -ld with all the result, which are then sorted. The result will be +# a list with all ls -l information and the the path name from the current +# directory (./). This pattern is used to delimit the find-style output, +# which is transformed into tree(1)-style output: # # drwxr-xr-x 2 josuah josuah 512 Feb 16 13:19 |- .ssh # -rw-r--r-- 1 josuah josuah 870 Feb 9 02:24 | `- config @@ -18,12 +17,39 @@ # -rwxr-xr-x 1 josuah josuah 468 Feb 17 19:55 ... BEGIN { + num = list(entries, ARGC == 1 ? "." : ARGV[1]); + tree(entries, num); + + for (l = 1; l <= num; l++) { + for (i = 1; entries[l":"i] != ""; i++) + printf("%s", entries[l":"i]); + printf("%s\n", entries[l"name"]); + } +} + +# Get a recursive list of all entries into entries[] with entries[i:j] +# holding the component j of the path i, and 0 has all the -l details: +# +# [ 1:[ 0:"-rw-r--r-- 1 root root 341 Mar 13 10:50", +# 1:"etc", +# 2:"passwd" ], +# 2:[ 0:"drwxr-xr-x 28 root root 4096 Mar 13 10:50", +# 1:"etc", +# 2:"sv" ], +# 3:[ 0:"drwxr-xr-x 2 root root 4096 Mar 13 10:50", +# 1:"etc", +# 2:"tor" ] ] +# +# Then return the number of entries in entries[]. + +function list(entries, path) +{ LINE = "| "; NODE = "|- "; TAIL = "`- "; VOID = " "; - find = "LC_COLLATE='C' find . " \ + find = "LC_COLLATE='C' cd '" path "' && find . " \ "-name '*.git' -prune -o " \ "-name 'CVS' -prune -o " \ "-exec ls -ld {} +"; @@ -37,33 +63,34 @@ BEGIN { count = split($0, path_v, "/"); for (i = 2; i <= count; i++) - line_v[num":"i] = LINE; + entries[num":"i] = LINE; - line_v[num":"count] = NODE; - line_v[num":"1] = infos " "; - line_v[num"name"] = count == 1 ? "." : path_v[count]; + entries[num":"count] = NODE; + entries[num":"1] = infos " "; + entries[num"name"] = count == 1 ? "." : path_v[count]; } close(find); - num--; + return num - 1; +} + +# Transform entries into a tree with duplicated components replaced by +# a line for drawing a tree. + +function tree(entries, num) +{ for (i = 2; !stop; i++) { stop = tail = 1; for (l = num; l > 0; l--) { - if (line_v[l":"i] == LINE && tail) { - line_v[l":"i] = VOID; + if (entries[l":"i] == LINE && tail) { + entries[l":"i] = VOID; stop = 0; - } else if (line_v[l":"i] == NODE && tail) { - line_v[l":"i] = TAIL; + } else if (entries[l":"i] == NODE && tail) { + entries[l":"i] = TAIL; tail = stop = 0; - } else if (!line_v[l":"i]) { + } else if (!entries[l":"i]) { tail = 1; } } } - - for (l = 1; l <= num; l++) { - for (i = 1; line_v[l":"i] != ""; i++) - printf("%s", line_v[l":"i]); - printf("%s\n", line_v[l"name"]); - } }