-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileExplorer.py
134 lines (105 loc) · 2.94 KB
/
fileExplorer.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
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
import os
import curses
def menu(title,belowby:int,titlecolor,options,win):
onselect=0
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
while(1):
win.clear()
win.addnstr(0,0,title,256,titlecolor)
for i in range(len(options)):
if i == onselect:
win.addnstr(i+belowby,0, options[i],256, curses.color_pair(1))
else:
win.addnstr(i+belowby,0, options[i],256)
win.refresh()
char = win.getch()
win.refresh()
if char == curses.KEY_UP:
if onselect > 0:
onselect -= 1
elif char == curses.KEY_DOWN:
if onselect < len(options)-1:
onselect += 1
elif char == curses.KEY_RIGHT:
return options[onselect]
def explorer(win = curses.initscr()):
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_RED)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)
win.keypad(True)
curses.cbreak()
curses.echo()
path = '/home/'
onfile = 0
notADir = False
while(1):
try:
files = os.listdir(path)
except Exception as x:
curses.endwin()
print('This program does not have permission to access that directory, perhaps try running it with \'sudo python fileExplorer.py\' instead')
return 1
win.clear()
win.addnstr(0,0,path,256, curses.color_pair(3))
for i in range(onfile,len(files)):
if i-onfile+1 == 42: break
if i == onfile:
win.addnstr(
i-onfile+1
,0,files[i],10,
curses.color_pair(1))
else: win.addnstr(
i-onfile+1,0,
files[i],10)
if os.path.isdir(
f'{path}/{files[i]}/'):
win.addnstr(
i-onfile+1,15,
' Directory\n', 21)
else:
win.addnstr(
i-onfile+1,15,
' File\n',15)
if notADir:
win.addnstr(41,0,
'NOT A DIRECTORY',25,
curses.color_pair(2))
win.addnstr(os.get_terminal_size()[1]-1,0,
'Go back a dir[LeftArrow] Enter selected dir[RightArrow] Move up [UpArrow] Move down [DownArrow] Exit [Esc]',256,curses.color_pair(3))
char = ''
win.refresh()
char = win.getch()
if char == curses.KEY_UP:
if onfile > 0:
onfile -= 1
notADir = False
elif char == curses.KEY_DOWN:
if onfile < len(files)-1:
onfile += 1
notADir = False
elif char == curses.KEY_RIGHT:
if os.path.isdir(f'{str(path)}{files[onfile]}/'):
path = f'{path}{files[onfile]}/'
notADir = False
onfile=0
else: notADir = True
elif char == curses.KEY_LEFT:
split = path[1:]
split = split[:len(split)-1]
split = split.split('/')
notADir = False
path = ''
for i in range(len(split)-1):
path += '/'+split[i]
path += '/'
onfile=0
elif char == 27:
options = ["Yes","No"]
response = menu("Do you want to really exit?",2,curses.color_pair(3),options,win)
if response == 'Yes':
break
curses.endwin()
print('Program finished with no errors, and a file path of\n'+path)
curses.wrapper(explorer)