-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (132 loc) · 4.53 KB
/
Copy pathmain.py
File metadata and controls
163 lines (132 loc) · 4.53 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import sys
import os
import pyperclip as pc
from pathlib import Path
from colorama import init, Fore, Back
class TextTheme:
ERROR = Back.RESET + Fore.LIGHTRED_EX
WARNING = Back.RESET + Fore.YELLOW
INFO = Back.RESET + Fore.LIGHTBLACK_EX
APP_TITLE = Back.RESET + Fore.CYAN
PROMPT = Back.RESET + Fore.LIGHTBLACK_EX
INPUT = Back.RESET + Fore.CYAN
NONE = Back.RESET + Fore.RESET
def showInfo(msg):
printMsg = f"\n[Info] {msg}"
print(TextTheme.INFO + printMsg)
def showWarning(msg):
printMsg = f"\n[Warning] {msg}"
print(TextTheme.WARNING + printMsg)
def showError(msg):
printMsg = f"\n[Error] {msg}"
print(TextTheme.ERROR + printMsg)
def firstPage():
print(TextTheme.APP_TITLE +
r"""
____ _ _____
| _ \(_)_ _|_ _| __ ___ ___
| | | | | '__|| || '__/ _ \/ _ \
| |_| | | | | || | | __/ __/
|____/|_|_| |_||_| \___|\___|
""")
print(TextTheme.PROMPT +
"""
* Name : DirTree
* Function : A tool used to scan a specified directory and output the results as a tree structure.
* Version : v1.0.0
* Developer: 294Ryan
""")
def getInput(prompt = ""):
temp = input(TextTheme.INPUT + f"{prompt}\n>> ").strip()
return temp
def p(arg):
print(TextTheme.NONE + arg, end = "")
return arg
def newLine():
print(TextTheme.NONE + "")
return "\n"
def printTree(oPath, treeNow:str, ignoreHiddenDir:bool, ignoreHiddenFile:bool, prefix:str="", visited = None):
""" ├ │ └ ─ """
if visited is None:
visited = set()
fullPath = oPath.resolve()
treeNow += p(fullPath.name)
treeNow += p(r"/")
# symlink check
if fullPath in visited:
treeNow += p(" [Skip this symlink] ")
treeNow += newLine()
return treeNow
visited.add(fullPath)
# catch PermissionError
try:
entries = list(oPath.iterdir())
files = sorted(x for x in entries if x.is_file())
if ignoreHiddenFile:
files = [x for x in files if not x.name.startswith(".")]
dirs = sorted(x for x in entries if x.is_dir())
if ignoreHiddenDir:
dirs = [x for x in dirs if not x.name.startswith(".")]
except OSError:
treeNow += p(f" [Cannot read this directory. Permission denied] ")
treeNow += newLine()
return treeNow
treeNow += newLine()
items = dirs + files
total = len(items)
for idx, item in enumerate(items):
isLast = idx == total - 1
treeNow += p(prefix)
treeNow += p("└─" if isLast else "├─")
childPrefix = prefix + (" " if isLast else "│ ")
if item.is_dir():
treeNow = printTree(item, treeNow, ignoreHiddenDir, ignoreHiddenFile, childPrefix, visited)
else:
treeNow += p(item.name)
treeNow += newLine()
return treeNow
def main():
os.system("cls" if os.name == "nt" else "clear")
init()
firstPage()
while True:
path = Path(getInput("Enter a directory path"))
if path.is_dir():
break
else:
showError(f'Path "{path.resolve()}" is not a directory.')
ignoreHiddenDir = None
while ignoreHiddenDir == None:
_input = getInput('Ignore hidden directories? (Directories starting with ".") [Y/n]')
if _input.lower() == "y" or _input == "":
ignoreHiddenDir = True
break
elif _input.lower() == "n":
ignoreHiddenDir = False
ignoreHiddenFile = None
while ignoreHiddenFile == None:
_input = getInput('Ignore hidden files? (Files starting with ".") [Y/n]')
if _input.lower() == "y" or _input == "":
ignoreHiddenFile = True
break
elif _input.lower() == "n":
ignoreHiddenFile = False
print(TextTheme.PROMPT + "\nDirectory tree: \n")
fullTree = printTree(path, "", ignoreHiddenDir, ignoreHiddenFile)
showInfo("Directory scaned.")
_input = None
while _input == None:
_input = getInput('Ignore hidden files? (Files starting with ".") [Y/n]')
if _input.lower() == "y" or _input == "":
try:
pc.copy(fullTree)
showInfo("Copied.")
except:
showWarning("Copy failed. Your device may not support clipboard access. Please copy it manually.")
break
elif _input.lower() == "n":
break
input(TextTheme.PROMPT + "\nPress [Enter] to exit.")
sys.exit()
if __name__ == "__main__":
main()