-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
56 lines (48 loc) · 1.84 KB
/
view.py
File metadata and controls
56 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from humanize import naturalsize
from directory import DirectoryInfo
from screen import Screen
def list_view(dir, all=False, summarize=False, measure=False, depth=None, fullpath=False, max_count=None, window=False):
root = None if fullpath else dir.path
if max_count is None:
if summarize:
walk = [dir]
else:
walk = list(dir.walk(all=all))[::-1]
else:
walk = dir.get_max_objects(max_count, files=all)
if depth is not None:
walk = filter(lambda x: x.depth <= depth, walk)
lines = [obj.str(measure=measure, root_path=root) for obj in walk]
if window:
win = Screen(lines[::-1])
win.run()
else:
for line in lines:
print(line)
def tree_view(dir, all=False, summarize=False, measure=False, depth=None, fullpath=False, window=False):
space = ' '
branch = '│ '
tee = '├── '
last = '└── '
def tree_lines(dir, prefix=''):
contents = dir.subdirs
if all:
contents += dir.files
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, content in zip(pointers, contents):
size = str(naturalsize(content.size) if measure else content.size)
content_name = content.path if fullpath else content.name
yield f"{prefix}{pointer}[{size}] {content_name}"
if isinstance(content, DirectoryInfo):
extension = branch if pointer == tee else space
if depth is None or content.depth < depth:
yield from tree_lines(content, prefix=prefix + extension)
lines = [f"[{naturalsize(dir.size) if measure else dir.size}]"]
if not summarize:
lines += tree_lines(dir)
if window:
win = Screen(lines)
win.run()
else:
for line in lines:
print(line)