-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop_helpers.py
215 lines (157 loc) · 5.57 KB
/
loop_helpers.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
#
# Functional helper units of the main shell loop
#
# mapper -> dictionary mapping all key strokes to the
# appropriate terminal action
#
import sys
import os
import time
import curses
from socket import gethostname
from Execute import execute
from window_helpers import clear_line, set_the_shell, prompt, pad_pos, curses_colors
prev_cmds = [] # in memory list of all previous commands
arrow_counter = 0 # up and down arrow index position
def backspace_key(cmd: str, w, **kwargs):
'''
delete a character from terminal
return new string for the command with the
appropriate index being removed.
'''
curs = w.getyx()
temp = cmd[:curs[1] - 3] + cmd[curs[1] - 2:] if curs[1] > 2 else cmd
if temp != cmd:
clear_line(w)
w.addstr(prompt, curses.color_pair(curses_colors["YELLOW"]))
w.addstr(temp)
w.move(curs[0], curs[1] - 1)
return temp
##################################################################
# left and right arrows allow placing the cursor left and right
##################################################################
def right_arrow(cmd: str, w, **kwargs):
'''
implements the right arrow key
'''
curs = w.getyx()
max = w.getmaxyx()
w.move(curs[0], curs[1] + 1) if curs[1] + 1 < max[1] else None
return cmd
def left_arrow(cmd: str, w, **kwargs):
'''
implements the left arrow key
'''
curs = w.getyx()
w.move(curs[0], curs[1] - 1) if curs[1] - 1 >= 0 else None
return cmd
##################################################################
# up and down arrows implement switching between previous commands
##################################################################
def up_arrow(cmd: str, w, **kwargs):
'''
implements the up arrow key
'''
global arrow_counter
if arrow_counter == 0:
prev_cmds.insert(0, cmd)
arrow_counter += 1 if arrow_counter + 1 < len(prev_cmds) else 0
clear_line(w)
new_cmd = prev_cmds[arrow_counter]
w.addstr(prompt, curses.color_pair(curses_colors["YELLOW"]))
w.addstr(new_cmd)
return new_cmd
def down_arrow(cmd: str, w, **kwargs):
'''
implements the down arrow key
'''
global arrow_counter
arrow_counter -= 1 if arrow_counter - 1 >= 0 else 0
clear_line(w)
new_cmd = prev_cmds[arrow_counter]
w.addstr(prompt, curses.color_pair(curses_colors["YELLOW"]))
w.addstr(new_cmd)
return new_cmd
##################################################################
# window scroll buttons 'page up' and 'page down'
##################################################################
scroller = 0
def scroll_up(cmd:str, w, **kwargs):
'''
scrolls the window 'up', increments the y axis by one
use the 'Page up' button to scroll up
'''
y, x = w.getyx()
w_height, w_width = kwargs["curses_w"].getmaxyx()
global scroller
scroller += 1
refresh_y = y - scroller if y - scroller > 0 else 0
w.refresh(refresh_y, 0, 0, 0, w_height - 1, w_width - 1)
return cmd
def scroll_down(cmd:str, w, **kwargs):
'''
scrolls the window 'down', decrements the y position by one
use the 'Page Down' button to scroll down
'''
y, x = w.getyx()
w_height, w_width = kwargs["curses_w"].getmaxyx()
global scroller
scroller -= 1
refresh_y = y - scroller if y - scroller > 0 else 0
w.refresh(refresh_y, 0, 0, 0, w_height - 1, w_width - 1)
return cmd
##################################################################
# enter_key sends the currently typed command to execute for
# processing the command properly
##################################################################
def enter_key(cmd: str, w, **kwargs):
'''
executes the command
'''
#time.sleep(1)
global prev_cmds
prev_cmds.insert(0, cmd)
result = execute(cmd, w) if cmd != "" else None
temp = [x for x in prev_cmds if x != ""]
prev_cmds = temp
with open(f"/home/bench/Shell/cmd_pkg_use/cmd_pkg/command_history.txt", "w") as cmd_hist:
cmd_hist.write(str(prev_cmds))
set_the_shell(w)
return "" # reset the command in shell
def tab_button(cmd: str, w, **kwargs):
'''
auto complete for file name being typed into terminal
tab to autocomplete a directory in cd or when passing to
any command a file in the cwd.
'''
try:
directory = os.listdir(os.getcwd())
# find the being typed file at the end of the command, reconstruct after
temp = cmd.split()
#print(temp[-1])
file_name = temp[-1]
return_cmd = cmd
for path in directory:
if file_name in path:
return_cmd = cmd[:-len(file_name)] + path # replace the currently typed file
# with the completed file if any
clear_line(w)
w.addstr(prompt, curses.color_pair(curses_colors["YELLOW"]))
w.addstr(return_cmd)
return return_cmd
except IndexError: # nothing typed in for cmd
return cmd
# maps all of the curses values for each of the navigational components
nav_mapper = {
curses.KEY_UP: up_arrow, #259
curses.KEY_DOWN: down_arrow, #258
curses.KEY_LEFT: left_arrow, #260
curses.KEY_RIGHT: right_arrow, #261
10: enter_key,
8: backspace_key, # windows
263: backspace_key, # ubuntu
127: backspace_key, # ubuntu server
curses.KEY_PPAGE: scroll_up,
curses.KEY_NPAGE: scroll_down,
9: tab_button
}