-
Notifications
You must be signed in to change notification settings - Fork 463
/
pretty_print_dir.py
56 lines (52 loc) · 2.13 KB
/
pretty_print_dir.py
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
#!python3
# -*- coding: utf-8 -*-
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # not required after 'pip install uiautomation'
import uiautomation as auto
def GetDirChildren(directory):
if os.path.isdir(directory):
subdirs = []
files = []
for it in os.listdir(directory):
absPath = os.path.join(directory, it)
if os.path.isdir(absPath):
subdirs.append(absPath)
else:
files.append(absPath)
return subdirs + files
def main(directory, maxDepth = 0xFFFFFFFF):
remain = {}
texts = []
absdir = os.path.abspath(directory)
for it, depth, remainCount in auto.WalkTree(absdir, getChildren=GetDirChildren, includeTop=True, maxDepth=maxDepth):
remain[depth] = remainCount
isDir = os.path.isdir(it)
prefixPrint = ''.join(['│ ' if remain[i + 1] else ' ' for i in range(depth - 1)])
prefixLog = ''.join(['│ ' if remain[i + 1] else ' ' for i in range(depth - 1)])
if depth > 0:
if remain[depth] > 0:
prefixPrint += '├─→' if isDir else '├──'
prefixLog += '├─→ ' if isDir else '├── '
else:
prefixPrint += '└─→' if isDir else '└──'
prefixLog += '└─→ ' if isDir else '└── '
file = os.path.basename(it)
texts.append(prefixLog)
texts.append(file)
texts.append('\n')
auto.Logger.Write(prefixPrint, writeToFile=False)
auto.Logger.WriteLine(file, auto.ConsoleColor.Cyan if isDir else auto.ConsoleColor.Default, writeToFile=False)
allText = ''.join(texts)
auto.Logger.WriteLine(allText, printToStdout=False)
ret = input('\npress Y to save dir tree to clipboard, press other keys to exit')
if ret.lower() == 'y':
auto.SetClipboardText(allText)
if __name__ == '__main__':
if len(sys.argv) == 1:
dir_ = input('input a dir: ')
main(dir_)
elif len(sys.argv) == 2:
main(sys.argv[1])
else:
main(sys.argv[1], int(sys.argv[2]))