-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
375 lines (310 loc) · 12.4 KB
/
Copy patheditor.py
File metadata and controls
375 lines (310 loc) · 12.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
editor.py - PY360 ISPF Style Editor
Part of the Python Mainframe Experience Layer
Full screen editor using terminal.py engine.
Invoked from menu.py or directly for testing.
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import config
import catalog
from terminal import Terminal, ATTR_NORMAL, ATTR_BOLD, ATTR_DIM, ATTR_REVERSE
# Layout constants
LINENUM_WIDTH = 7 # "000001 "
TEXT_COL = LINENUM_WIDTH
MAX_TEXT_COLS = 80 - LINENUM_WIDTH
def edit_tk(dsn: str, term: Terminal):
"""
Entry point from menu.py.
Resolve DSN and launch editor on existing terminal.
"""
path = catalog.resolve_path(dsn)
if path is None:
return f"DSN NOT IN CATALOG: {dsn.upper()}"
_editor_loop(term, dsn.upper(), path)
return None
def edit(dsn: str):
"""
Standalone entry point - creates its own terminal.
Used for direct testing.
"""
config.load()
path = catalog.resolve_path(dsn)
if path is None:
return f"DSN NOT IN CATALOG: {dsn.upper()}"
term = Terminal(sysname=config.sysname())
_editor_loop(term, dsn.upper(), path)
term.close()
def _editor_loop(term: Terminal, dsn: str, path: str):
"""Main editor logic."""
# Load file
if os.path.exists(path) and os.stat(path).st_size > 0:
with open(path, "r") as f:
lines = [l.rstrip("\n") for l in f.readlines()]
else:
lines = []
# Editor state
top_line = 0 # index of first visible data line (0=TOP marker)
cur_row = 1 # screen row (0=cmd, 1=first data row)
cur_col = 0 # col in text area
cur_mode = 1 # 0=command line, 1=linenum area, 2=text area
command = "" # primary command field
message = "" # status message
modified = False
clipboard = [] # copy/move buffer
line_cmds = {} # {line_index: cmd_string}
running = True
DATA_ROWS = 20 # rows available for data (rows 2-21)
def draw():
term.clear()
# --- Title bar ---
mod_flag = " (MODIFIED)" if modified else ""
title = f" EDIT - {dsn}{mod_flag} "
right = f" LINE {top_line+1} COL 1 80 "
bar = title.ljust(80 - len(right)) + right
term.put(0, 0, bar[:80], ATTR_REVERSE)
# --- Command line ---
term.put(1, 0, "COMMAND ===> ", ATTR_NORMAL)
term.put(1, 13, f"{command:<47}", ATTR_NORMAL)
term.put(1, 61, "SCROLL ===> PAGE", ATTR_DIM)
# --- Data rows ---
for row in range(DATA_ROWS):
li = top_line + row
screen_row = row + 2 # row 0=title, row 1=cmd, row 2+ = data
if li == 0:
marker = f"{'':6} {'*** TOP OF DATA ':*<{80-8}}"
term.put(screen_row, 0, marker[:80], ATTR_DIM)
continue
data_li = li - 1 # actual index into lines[]
if data_li < len(lines):
lcmd = line_cmds.get(data_li, "")
linenum = f"{data_li+1:06d}"
prefix = f"{lcmd:<6} " if lcmd else f"{linenum} "
text = lines[data_li]
term.put(screen_row, 0, prefix, ATTR_DIM)
term.put(screen_row, LINENUM_WIDTH,
text[:80-LINENUM_WIDTH], ATTR_NORMAL)
elif data_li == len(lines):
marker = f"{'':6} {'*** BOTTOM OF DATA ':*<{80-8}}"
term.put(screen_row, 0, marker[:80], ATTR_DIM)
break
# --- Message bar ---
term.put(23, 0, f" {message:<78}", ATTR_REVERSE)
# --- Cursor ---
# screen_row: row 0=title, row 1=command, row 2=TOP OF DATA marker
# first actual data line is screen row 3, which is cur_row=1, top_line offset 1
if cur_mode == 0:
term.move_cursor(1, 13 + len(command))
elif cur_mode == 1:
data_li = top_line + cur_row - 1
lcmd = line_cmds.get(data_li, "")
term.move_cursor(cur_row + 2, len(lcmd))
else:
term.move_cursor(cur_row + 2, LINENUM_WIDTH + cur_col)
term.refresh()
def save():
nonlocal modified, message
with open(path, "w") as f:
f.write("\n".join(lines) + ("\n" if lines else ""))
modified = False
message = "FILE SAVED"
def do_primary_cmd(cmd: str):
nonlocal message, running, top_line, cur_row
parts = cmd.strip().split()
if not parts:
return
verb = parts[0].upper()
if verb in ("SAVE", "FILE"):
save()
elif verb in ("CANCEL", "QUIT"):
running = False
elif verb == "END":
if modified:
save()
running = False
elif verb == "FIND" and len(parts) >= 2:
needle = parts[1]
for i, line in enumerate(lines):
if needle.lower() in line.lower():
top_line = max(0, i)
cur_row = 1
message = f"FOUND '{needle}' AT LINE {i+1}"
return
message = f"'{needle}' NOT FOUND"
elif verb == "CHANGE" and len(parts) >= 3:
needle, replacement = parts[1], parts[2]
for i, line in enumerate(lines):
if needle.lower() in line.lower():
lines[i] = line.replace(needle, replacement, 1)
message = f"CHANGED LINE {i+1}"
return
message = f"'{needle}' NOT FOUND"
elif verb == "UP":
n = int(parts[1]) if len(parts) > 1 else DATA_ROWS
top_line = max(0, top_line - n)
elif verb == "DOWN":
n = int(parts[1]) if len(parts) > 1 else DATA_ROWS
top_line = min(max(0, len(lines) - DATA_ROWS + 2), top_line + n)
else:
message = f"UNKNOWN COMMAND: {verb}"
def do_line_cmds():
nonlocal modified, message, clipboard, cur_row, cur_col
pending = dict(line_cmds)
line_cmds.clear()
mm_lines = sorted([i for i, c in pending.items() if c.upper() == "MM"])
cc_lines = sorted([i for i, c in pending.items() if c.upper() == "CC"])
for li, cmd in sorted(pending.items()):
c = cmd.strip().upper()
if c == "D":
del lines[li]
modified = True
message = f"LINE {li+1} DELETED"
elif c == "I":
lines.insert(li + 1, "")
modified = True
cur_row += 1
cur_col = 0
message = f"LINE INSERTED AFTER {li+1}"
elif c == "R":
lines.insert(li + 1, lines[li])
modified = True
message = f"LINE {li+1} REPEATED"
elif c == "C":
clipboard = [lines[li]]
message = f"LINE {li+1} COPIED - USE A OR B TO PASTE"
elif c == "M":
clipboard = [lines[li]]
lines.pop(li)
modified = True
message = f"LINE MOVED - USE A OR B TO PASTE"
elif c == "A" and clipboard:
for j, l in enumerate(clipboard):
lines.insert(li + 1 + j, l)
modified = True
message = f"PASTED {len(clipboard)} LINE(S) AFTER {li+1}"
clipboard = []
elif c == "B" and clipboard:
for j, l in enumerate(clipboard):
lines.insert(li + j, l)
modified = True
message = f"PASTED {len(clipboard)} LINE(S) BEFORE {li+1}"
clipboard = []
if len(cc_lines) == 2:
clipboard = lines[cc_lines[0]:cc_lines[1]+1]
message = f"BLOCK COPIED ({len(clipboard)} lines) - USE A OR B TO PASTE"
if len(mm_lines) == 2:
clipboard = lines[mm_lines[0]:mm_lines[1]+1]
del lines[mm_lines[0]:mm_lines[1]+1]
modified = True
message = f"BLOCK MOVED ({len(clipboard)} lines) - USE A OR B TO PASTE"
# --- Main input loop ---
while running:
draw()
message = ""
key = term.wait_key()
if key == "F1":
import help as py360help
py360help.show("EDITOR", term)
elif key == "F3":
if modified:
save()
running = False
elif key == "F4":
running = False
elif key == "TAB":
# Cycle: command(0) -> linenum(1) -> text(2) -> command(0)
cur_mode = (cur_mode + 1) % 3
if cur_mode == 0:
pass # command line
elif cur_mode == 1:
cur_row = max(1, cur_row) # ensure on a data row
else:
cur_row = max(1, cur_row)
elif key == "UP":
if cur_mode == 0:
cur_mode = 2
cur_row = 1
elif cur_row > 1:
cur_row -= 1
elif top_line > 0:
top_line -= 1
elif key == "DOWN":
if cur_mode == 0:
cur_mode = 1
cur_row = 1
else:
max_row = min(len(lines), DATA_ROWS - 1)
if cur_row < max_row:
cur_row += 1
elif top_line + DATA_ROWS <= len(lines):
top_line += 1
elif key == "LEFT":
if cur_mode == 2:
cur_col = max(0, cur_col - 1)
elif key == "RIGHT":
if cur_mode == 2:
data_li = top_line + cur_row - 1
if data_li < len(lines):
cur_col = min(len(lines[data_li]), cur_col + 1)
elif key == "PGUP":
top_line = max(0, top_line - DATA_ROWS)
cur_row = 1
elif key == "PGDN":
top_line = min(max(0, len(lines) - DATA_ROWS + 2),
top_line + DATA_ROWS)
cur_row = 1
elif key == "ENTER":
if cur_mode == 0:
do_primary_cmd(command)
command = ""
else:
do_line_cmds()
elif key == "BACKSPACE":
if cur_mode == 0:
command = command[:-1]
elif cur_mode == 1:
data_li = top_line + cur_row - 1
lcmd = line_cmds.get(data_li, "")
if lcmd:
line_cmds[data_li] = lcmd[:-1]
if not line_cmds[data_li]:
del line_cmds[data_li]
else:
data_li = top_line + cur_row - 1
if data_li < len(lines) and cur_col > 0:
lines[data_li] = (lines[data_li][:cur_col-1] +
lines[data_li][cur_col:])
cur_col -= 1
modified = True
elif key == "DELETE":
if cur_mode == 2:
data_li = top_line + cur_row - 1
if data_li < len(lines):
lines[data_li] = (lines[data_li][:cur_col] +
lines[data_li][cur_col+1:])
modified = True
elif len(key) == 1 and 32 <= ord(key) <= 126:
if cur_mode == 0:
command += key
elif cur_mode == 1:
# Typing in line number area = line command
data_li = top_line + cur_row - 1
if 0 <= data_li < len(lines):
lcmd = line_cmds.get(data_li, "")
if len(lcmd) < 6:
line_cmds[data_li] = lcmd + key.upper()
else:
data_li = top_line + cur_row - 1
if data_li < len(lines):
lines[data_li] = (lines[data_li][:cur_col] +
key +
lines[data_li][cur_col:])
cur_col += 1
modified = True
elif data_li == len(lines):
lines.append(key)
cur_col = 1
modified = True
if __name__ == "__main__":
edit("USER.DAVE.SOURCE")