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:
| Leverging FZF for a user interface one can do fuzzy searching with a directory tree and document preview. | |
| The PTF.py creates that tree then colors the path thus giving a graph of the item searched. | |
| Example Usage includes: | |
| - More advance Poorman's Mindmap | |
| - Person/Contact Graph as a filesystem | |
| - Asset Mapping (with pass(1) and LDAP file system structures) | |
| - Maps with stow | |
| - Project Management / Kanboard |
| #!/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, '') |