-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·212 lines (176 loc) · 5.58 KB
/
main.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
import os,sys,argparse,termios,tty,time,signal
import file_list
import text_pager
import string_util as su
import colors
import view_layout
import select
import debug
import text_input
import table
stdin_fd = sys.stdin.fileno()
try:
old_term_settings = termios.tcgetattr(stdin_fd)
except:
old_term_settings = None
def reset_term():
if old_term_settings == None:
return
termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_term_settings)
def getchar(timeout:float=0.01):
ch = ""
#Returns a single character from standard input
try:
tty.setraw(stdin_fd,when=termios.TCSANOW)
i, o, e = select.select( [sys.stdin], [], [], timeout)
if i:
ch = (sys.stdin.buffer.raw.read(1)).decode('utf-8')
else:
return None
finally:
reset_term()
return ch
def getstr():
s = ""
ret = ""
timeout=1 # the first getchar has a big timeout
while ret != None:
s += ret
ret = getchar(timeout)
timeout=0.01 # subsequent getchars have a small timeout
return s
parser = argparse.ArgumentParser(
prog='Explorer',
description='Explores folders',
epilog='Schpaloingus!')
parser.add_argument('dir', nargs='?', default=os.getcwd())
parser.add_argument('-o', '--once', action='store_true')
parser.add_argument('-b', '--batch', action='store_true')
parser.add_argument('-n', '--iterations', type=int, default=-1)
parser.add_argument('-p', '--profile', action='store_true')
args = parser.parse_args()
path = args.dir
layout = view_layout.ViewLayout(None,[],10,10)
files = file_list.FileList(layout, path)
pager = text_pager.TextPager(layout, height=50)
search_bar = text_input.TextInput(layout,100,1,"Find: ",placeholder=" ",name="Search bar",use_border=False)
search_bar.hide()
@search_bar.set_ontype
def ontype(self: text_input.TextInput):
@files.table.set_filter
def filter(row: table.TableRow):
name=su.strip_ansi(row.data["name"])
return self.text in name
@search_bar.set_onsubmit
def onsubmit(self: text_input.TextInput):
layout.set_focus_by_name("FileList")
self.hide()
layout.structure = [
[files,pager],
[search_bar]
]
pager.visible = False
if args.profile:
import functiontrace
functiontrace.trace()
debug_pager = text_pager.TextPager(None,100,15,"Debug Log","DebugPager")
debug_pager.onfocus()
@files.set_onsubmit
def submit(self):
global path,files,pager
debug.log(f"Changing directory!")
file = files.get_selected_file()
if file != None:
if os.path.isdir(os.path.realpath(file.path)): # use os.path.realpath to follow symlinks
path = os.path.abspath( os.path.join( path, file.get_name() ) )
update_files()
if file.get_name() == "..": # if going up, then select the previous folder
new_files=files.table.get_field("name")
try:
index = new_files.index( os.path.basename(os.path.dirname(file.path)) )
except ValueError:
index = 1
files.table.selected = index
elif os.path.isfile(os.path.realpath(file.path)):
pager.visible=True
pager.load_from_file(file)
running = True
if args.once:
running = False
def update_files():
files.set_dir(path)
def update():
debug_pager.text=debug.debug_text
try:
term_size = os.get_terminal_size()
layout.width = term_size.columns-2
layout.height = term_size.lines-1
files.height = layout.height - 10
search_bar.width=layout.width
if pager.visible:
files.width = int(term_size.columns/2 - 1)
pager.height = files.height
else:
files.width = term_size.columns
pager.width = int(term_size.columns/2 - 2)
except OSError:
# term_size = os.terminal_size([],{})
pass
pager.update()
files.update_table()
debug_pager.scroll_y=debug_pager.get_line_count()
debug_pager.update()
def view():
s = "\x1b[2J\x1b[H\x1b[0m"
if iteration%2==0:
s += "#"
else:
s += " "
s += f"==== {path} ====\n"
s += f"{len(files.get_files())} files, "
s += f"{len(files.get_folders())} folders, "
s += f"{len(files.get_links())} links"
s += "\n"
s += f"{files.table.width}x{files.table.height}"
s += "\n\n"
s += layout.view()
if debug.debug_mode:
x=layout.width-debug_pager.width-1
s += "\x1b[s" # save cursor position
s += su.goto(x,4)
s += su.float_text(debug_pager.view())
s += "\x1b[u" # restore cursor position
return s
def handle_input(char):
global running,path,show_pager,pager_focused
debug.log(f"Input char {repr(char)}")
if char=="\x03": #Ctrl-C
running=False
elif char=="d":
debug.debug_mode = not debug.debug_mode
elif char=="q":
running=False
else:
layout.input(char)
if __name__ == "__main__":
iteration = 0
def sigwinch_handle(signum,frame):
reset_term() # reset terminal to not mess up printing
update()
print(view())
tty.setraw(stdin_fd,when=termios.TCSANOW) # set tty back to raw mode for input
signal.signal(signal.SIGWINCH,sigwinch_handle)
update()
print(view())
while running:
if args.iterations > 0 and iteration >= args.iterations:
running=False
if not args.batch:
text = getstr()
if text != "":
handle_input(text)
update()
# print("Loading...")
print(view())
iteration += 1