lt - add new tree-like utility
authorAlex Hornung <ahornung@gmail.com>
Thu, 8 Jul 2010 21:24:02 +0000 (22:24 +0100)
committerAlex Hornung <ahornung@gmail.com>
Thu, 8 Jul 2010 21:32:43 +0000 (22:32 +0100)
* lt shows the directory structure below some specified directory much
  like the tree utility does.

usr.bin/Makefile
usr.bin/lt/Makefile [new file with mode: 0644]
usr.bin/lt/lt.1 [new file with mode: 0644]
usr.bin/lt/lt.sh [new file with mode: 0755]

index 4db5b9a..7bef2c9 100644 (file)
@@ -109,6 +109,7 @@ SUBDIR=     alias \
        look \
        lorder \
        lsvfs \
+       lt \
        m4 \
        mail \
        make \
diff --git a/usr.bin/lt/Makefile b/usr.bin/lt/Makefile
new file mode 100644 (file)
index 0000000..36d10af
--- /dev/null
@@ -0,0 +1,4 @@
+SCRIPTS=lt.sh
+MAN=lt.1
+
+.include <bsd.prog.mk>
diff --git a/usr.bin/lt/lt.1 b/usr.bin/lt/lt.1
new file mode 100644 (file)
index 0000000..b8c084a
--- /dev/null
@@ -0,0 +1,51 @@
+.\"
+.\" Copyright (c) 2010 The DragonFly Project.  All rights reserved.
+.\" 
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in
+.\"    the documentation and/or other materials provided with the
+.\"    distribution.
+.\" 3. Neither the name of The DragonFly Project nor the names of its
+.\"    contributors may be used to endorse or promote products derived
+.\"    from this software without specific, prior written permission.
+.\" 
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd July 8, 2010
+.Os
+.Dt LT 1
+.Sh NAME
+.Nm lt
+.Nd show a tree-like output of any directory
+.Sh SYNOPSIS
+.Nm
+.Op Ar directory ...
+.Sh DESCRIPTION
+The
+.Nm
+script shows a tree of the path specified by
+.Ar directory ,
+including files and subdirectories, much like the
+.Nm tree
+utility.
+If no directory is specified, the current directory
+is shown.
+.Sh SEE ALSO
+.Xr find 1
diff --git a/usr.bin/lt/lt.sh b/usr.bin/lt/lt.sh
new file mode 100755 (executable)
index 0000000..75d9cec
--- /dev/null
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+# The awk script used in this file was written by vgersh99
+
+if [ "$#" = "0" ]; then
+       DIR="."
+else
+       DIR=$*
+fi
+
+for _dir in $DIR; do
+if [ ! -d $_dir ]; then
+       echo "$_dir: No such directory"
+       continue
+fi
+
+echo $_dir | sed -E 's/\/+$//'
+find ${_dir}/ | sort | awk '
+BEGIN {
+        FS = "/";
+        i = 0;
+}
+
+function fineprint(branches)
+{
+        gsub(" ","    ", branches);
+        gsub("[|]","&   ", branches);
+        gsub("`","&-- ", branches);
+        gsub("[+]","|-- ", branches);
+        return branches;
+}
+
+function mktree(number,branches,   tnumber)
+{
+
+        if (number > NR) {
+                return number - 1;
+        }
+
+        tnumber = number;
+
+        while (array["shift", number] < array["shift", tnumber + 1]) {
+                tnumber = mktree(tnumber + 1, branches" ");
+                if (tnumber == NR) break;
+        }
+
+        if (array["shift", number] == array["shift", tnumber + 1]) {
+                array["slip", number] = branches"+";
+        }
+
+        if ((array["shift", number] > array["shift", tnumber + 1]) \
+                || tnumber == NR) {
+                array["slip", number] = branches"`";
+        }
+
+
+        return tnumber;
+}
+
+{
+        array["shift", NR] = NF;
+        array["name", NR] = $(NF);
+}
+
+END {
+        if (EXIT)
+                exit EXIT;
+
+        for (i = 1; i <= NR; i++) {
+                i = mktree(i, "");
+        }
+
+        for (i = 1; i <= NR; i++) {
+
+                if (i > 1) {
+                        lprev = length(array["slip", i - 1]);
+                        lcurr = length(array["slip", i]);
+                        if (lprev > lcurr - 1)
+                                legacy = substr(array["slip", i - 1], 0, \
+                                                lcurr - 1);
+                        else
+                                legacy = array["slip", i-1];
+                        tail = substr(array["slip", i], length(legacy) + 1 , \
+                                        lcurr - length(legacy));
+                               gsub("[+]", "|", legacy);
+                        gsub("`", " ", legacy);
+                        array["slip", i] = (legacy)(tail);
+                }
+
+                printf "%s%s\n", fineprint(array["slip", i]), \
+                                array["name", i];
+
+        }
+}
+' | grep -v '^|-- $'
+done
+