Skip to content

Instantly share code, notes, and snippets.

@Lanny
Created June 28, 2021 16:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lanny/61afcb3c747a9c0f858e7460c77b464d to your computer and use it in GitHub Desktop.
Save Lanny/61afcb3c747a9c0f858e7460c77b464d to your computer and use it in GitHub Desktop.
File Hierarchy Preview

Outputs a limited file tree rooted at the current directory which highlights the position of the argument within the tree. Originally intended for FZF like so:

fzf --preview 'ptf.py {}'

Screenshot:

screenshot

#!/usr/bin/env python
import sys
import os
def get_path(path):
return os.path.join('.', os.path.normpath(path)).split(os.sep)
def redify(s):
return '\u001b[31m' + s + '\033[0m'
def render_pathspec(pathspec, red):
b = []
for idx, item in enumerate(pathspec):
if idx == len(pathspec) - 1:
b.append(redify('├') if red or item == 'r' else '├')
b.append(redify('──') if red else '──')
else:
b.append(redify('│ ') if item == 'r' else '│ ')
return ''.join(b)
def print_level(name, pathspec, red=False):
path = render_pathspec(pathspec, red)
name = redify(name) if red else name
print('%s%s' % (path, name))
def recur(idx, parts, pathspec):
cur_path = os.path.join(*parts[:idx+1])
files = [f for f in os.scandir(cur_path)]
is_final = idx == len(parts) - 2
on_path = True
if not is_final:
files = [f for f in files if f.is_dir()]
for fidx, f in enumerate(files):
red = False
is_critical_path = f.name == parts[idx+1]
if is_critical_path:
red = True
on_path = False
new_pathspec = pathspec + ('r' if on_path else 'n')
print_level(
f.name,
new_pathspec,
red=red
)
if not is_final and is_critical_path:
recur(idx+1, parts, new_pathspec)
if __name__ == '__main__':
parts = get_path(sys.argv[1])
print(redify(parts[0]))
recur(0, parts, '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment