Skip to content

Instantly share code, notes, and snippets.

@mulle-nat
Created February 2, 2018 11:01
Show Gist options
  • Save mulle-nat/91a1c9fd5f4842f3d14787c98038f7a7 to your computer and use it in GitHub Desktop.
Save mulle-nat/91a1c9fd5f4842f3d14787c98038f7a7 to your computer and use it in GitHub Desktop.
Recursive Graphviz view of your current directory
#! /usr/bin/env bash
#
# Copyright (c) 2018 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 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.
#
# Neither the name of Mulle kybernetiK 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 HOLDER 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.
#
[ "${TRACE}" = "YES" ] && set -x && : "$0" "$@"
emit_header()
{
cat <<EOF
digraph G
{
node [ shape="folder" ]
edge [ arrowhead="none" ]
EOF
}
emit_footer()
{
cat <<EOF
}
EOF
}
emit_folder_edges()
{
local directory="$1"
local start
local end
local component
IFS="/"
for component in ${directory}
do
start="${end}"
if [ -z "${start}" ]
then
end="${component}"
else
end="${start}/${component}"
fi
echo "\"${end}\" [ label=\"${component}\" ]"
if [ ! -z "${start}" -a ! -z "${end}" ]
then
echo "\"${start}\" -> \"${end}\""
fi
done
IFS="${DEFAULT_IFS}"
}
emit_file()
{
local directory="$1"
local name="$2"
local filename
filename="${directory}/${name}"
echo "\"${directory}\" -> \"${filename}\" "
echo "\"${filename}\" [ label=\"${name}\", shape=\"note\"]"
}
emit_nodes()
{
local filename
local directory
local name
IFS="
"
for filename in `find . -print`
do
# filename="${filename:2}"
if [ -f "${filename}" ]
then
directory="`dirname -- "${filename}" `"
name="`basename -- "${filename}"`"
emit_folder_edges "${directory}"
emit_file "${directory}" "${name}"
else
emit_folder_edges "${filename}"
fi
done
}
main()
{
DEFAULT_IFS="${IFS}"
text="`emit_nodes | sort -u`" || exit 1
emit_header
echo "${text}"
emit_footer
}
main "$@"
@mulle-nat
Copy link
Author

Usage

mulle-ls-dot > x.dot
open x.dot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment