Skip to content

Instantly share code, notes, and snippets.

@denzuko
Forked from Lanny/README.md
Last active January 6, 2023 17:41
Show Gist options
  • Select an option

  • Save denzuko/6a828c0f694d189de384f89ac1af9d79 to your computer and use it in GitHub Desktop.

Select an option

Save denzuko/6a828c0f694d189de384f89ac1af9d79 to your computer and use it in GitHub Desktop.

Revisions

  1. denzuko revised this gist Jan 6, 2023. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions 00-Intro
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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
  2. @Lanny Lanny created this gist Jun 28, 2021.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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](https://i.ibb.co/Qc9sYmR/Screen-Shot-2021-06-28-at-11-36-12-AM.png)
    60 changes: 60 additions & 0 deletions ptf.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/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, '')