-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzxp.py
232 lines (184 loc) Β· 6.92 KB
/
zxp.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import json
import curses
from curses import wrapper
import consts
import readline
import glob
import logging
import argparse
import sys
from shutil import rmtree
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--log', action='store_true', help='Enable logging (stores file where executable is)')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.2.0')
args = parser.parse_args()
if args.log:
logging.basicConfig(
filename="zxp.log",
encoding="utf-8",
filemode="a",
format="{asctime} - {levelname} - {message}",
style="{",
datefmt="%Y-%m-%d %H:%M",
level=logging.INFO
)
emojis = json.load(
open(os.path.join(os.path.dirname(__file__), "emojis.json")))
def draw_menu(stdscr, files, current_dir, current_input):
curses.init_pair(1, consts.ACCENT_COLOR, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLACK, consts.ACCENT_COLOR)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
BLUE_AND_BLACK = curses.color_pair(1)
HIGHLIGHT = curses.color_pair(2)
WHITE_AND_BLACK = curses.color_pair(3)
stdscr.clear()
h, w = stdscr.getmaxyx()
#if consts.SHOW_HELP == True:
# stdscr.addstr( # Help text
# h - 1,
# w - len(consts.HELP_TEXT) - 1, consts.HELP_TEXT, curses.A_BOLD,)
stdscr.addstr(0, 0, current_dir, HIGHLIGHT)
pad = curses.newpad(h, w) # h, w
current_line = 0
current_column = 0
longest = 0
for i in files:
if current_line >= h:
current_line = 0
current_column += 1
if len(i) > longest:
longest = len(i) + 1
try:
# Make this work while typing instad of just after enter is pressed
if i[2:] == current_input: # The [2:] is to remove the emoji
pad.addstr(
current_line,
(current_column * longest) + (current_column != 0) * 2,
i,
HIGHLIGHT)
else:
pad.addstr(
current_line,
(current_column * longest) + (current_column != 0) * 2,
i,
BLUE_AND_BLACK if i[0] == "π" else WHITE_AND_BLACK)
except curses.error:
pass # This happens when the terminal is too small. Temporary fix.
current_line += 1
stdscr.refresh()
pad.refresh(0, 0, 1, consts.INDENT, h - 2, w - consts.INDENT)
def complete_path(text, state):
return (glob.glob(os.path.expanduser(text) + "*") + [None])[state]
def get_input(stdscr, current_dir):
curses.echo()
readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(complete_path)
h, w = stdscr.getmaxyx()
input_win = curses.newwin(1, w - 45, h - 1, 0)
input_win.keypad(1)
current_input = ""
while True:
input_win.clear()
input_win.addstr(0, 0, f"> {current_input}", curses.A_BOLD)
input_win.refresh()
try:
char = input_win.getch()
except KeyboardInterrupt:
exit(0)
if char == 219:
exit(0)
if char == ord("\n"): # Enter key
break
elif char == ord("\t"): # Tab key
# Goofy autocomplete stuff
# TODO: Make it so it can autocomplete a path that isn't the entire input
# Example: path/to/file :m need/autocomplete/here ignore the middle :m
completions = glob.glob(os.path.join(
current_dir, current_input) + "*")
if len(completions) == 1:
current_input = os.path.relpath(completions[0], current_dir)
elif len(completions) > 1:
common_prefix = os.path.commonprefix(completions)
current_input = os.path.relpath(common_prefix, current_dir)
elif char == curses.KEY_BACKSPACE or char == 127:
current_input = current_input[:-1]
elif 32 <= char <= 126: # Printable characters
current_input += chr(char)
curses.noecho()
return current_input
def format_files(directory, files):
new = []
for i in files:
full_path = os.path.join(directory, i)
if os.path.isdir(full_path):
new.append(f"π {i}/")
else:
try:
new.append(f"{emojis[os.path.splitext(i)[1]]} {i}")
except KeyError:
new.append(f"π {i}")
new.sort(key=lambda x: (not x.startswith("π "), x))
return new
def get_files(directory, see_hidden=False):
if see_hidden:
visible_files = os.listdir(directory)
else:
visible_files = [f for f in os.listdir(
directory) if not f.startswith(".")]
formatted_files = format_files(directory, visible_files)
return formatted_files
def explorer(stdscr):
logging.info("Starting explorer")
current_dir = os.getcwd()
h, w = stdscr.getmaxyx()
see_hidden = False
user_input = ""
while True:
files = get_files(current_dir, see_hidden)
draw_menu(stdscr, files, current_dir, user_input)
user_input = get_input(stdscr, current_dir)
# All commands
if user_input in [":quit", ":q"]:
logging.info("Quitting")
sys.exit(0)
elif user_input in [":back", ":b", ".."]:
current_dir = os.path.dirname(current_dir)
elif user_input in [":hidden", ":h"]:
see_hidden = not see_hidden
elif user_input.startswith("$"):
os.system(user_input[1:]) # Run command in subshell
elif user_input in [":move", ":m", ":rename", ":r"]:
try:
command = user_input.split()
source = os.path.join(current_dir, command[0])
dest = os.path.join(current_dir, command[2])
os.rename(source, dest)
logging.info(f"Moved {source} to {dest}")
except:
logging.warning(f"Failed to move file from {source} to {dest}")
elif user_input in [":delete", ":d", ":remove", ":rm"]:
try:
command = user_input.split()
source = os.path.join(current_dir, command[0])
if os.path.isdir(source):
rmtree(source)
logging.info(f"Deleted directory {source}")
continue
else:
os.remove(source)
logging.info(f"Deleted file {source}")
except:
logging.warning(f"Failed to delete file {source}")
else:
full_path = os.path.join(current_dir, user_input)
if os.path.exists(full_path):
if os.path.isdir(full_path):
current_dir = full_path
else:
os.system(f"xdg-open '{full_path}'")
def main():
wrapper(explorer)
if __name__ == "__main__":
main()