Skip to content

Commit 078cb71

Browse files
committed
Use py_curses_editor for editing cells
1 parent bfbc71b commit 078cb71

1 file changed

Lines changed: 19 additions & 52 deletions

File tree

tabview/tabview.py

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Based on code contributed by A.M. Kuchling <amk at amk dot ca>
66
77
"""
8+
from editor.editor import Editor
9+
810
import csv
911
import _curses
1012
import curses
@@ -405,62 +407,27 @@ def delete_row(self):
405407

406408
self.set_term_title(True)
407409

408-
def _edit_validator(self, ch):
409-
"""Fix Enter and backspace for textbox.
410-
411-
Used as an aux function for the textpad.edit method
412-
413-
"""
414-
if ch == curses.ascii.NL: # Enter
415-
return curses.ascii.BEL
416-
elif ch == curses.KEY_HOME:
417-
return self.textpad.do_command(KEY_CTRL('a'))
418-
elif ch == curses.KEY_END:
419-
return self.textpad.do_command(KEY_CTRL('e'))
420-
elif ch == curses.ascii.ESC:
421-
self.textpad.insert_mode = False
422-
return curses.ascii.BEL
423-
return ch
424-
425410
def edit_cell(self, edit_existing=True):
426411
yp = self.y + self.win_y
427412
xp = self.x + self.win_x
428413
self.box_height = self.max_y - int(self.max_y / 2)
429-
box_height = int(self.box_height / 4)
430-
from editor.editor import Editor
431-
out = Editor(self.scr, inittext=self.data[yp][xp], win_location=(yp, xp), win_size=(self.max_y-box_height-1, self.max_x))()
432-
self.data[yp][xp] = out
433-
# yp = self.y + self.win_y
434-
# xp = self.x + self.win_x
435-
# self.box_height = self.max_y - int(self.max_y / 2)
436-
# box_height = int(self.box_height / 4)
437-
#
438-
# prompt = "Edit: "
439-
# scr2 = curses.newwin(box_height+1, self.max_x, self.max_y-box_height-1, 0)
440-
# scr3 = scr2.derwin(1, self.max_x-len(prompt)-3, 1, len(prompt)+1)
441-
#
442-
# scr2.box()
443-
# scr2.move(1, 1)
444-
#
445-
# addstr(scr2, prompt)
446-
# if edit_existing:
447-
# addstr(scr3, self.data[yp][xp])
448-
#
449-
# scr2.refresh()
450-
# curses.curs_set(1)
451-
#
452-
# self.textpad = Textbox(scr3, insert_mode=True)
453-
#
454-
# result = self.textpad.edit(self._edit_validator)[:-1].strip()
455-
# if self.textpad.insert_mode: # False if escape pressed (discard)
456-
# self.undo_buffer.insert(0, (yp, xp, self.data[yp][xp]))
457-
# self.data[yp][xp] = result
458-
# self.set_term_title(True)
459-
#
460-
# try:
461-
# curses.curs_set(0)
462-
# except _curses.error:
463-
# pass
414+
self.term_rows, self.term_cols = self.scr.getmaxyx()
415+
data = self.data[yp][xp] if edit_existing else ""
416+
417+
prompt = "Edit: "
418+
editor = Editor(
419+
self.scr, title=prompt, inittext=data, max_paragraphs=1,
420+
win_size=(int(self.term_rows / 2), self.term_cols),
421+
win_location=(self.box_height, 0),
422+
)
423+
editor.end() # Move to end of text for easier editing
424+
425+
result = editor().strip()
426+
if result != self.data[yp][xp]:
427+
self.undo_buffer.insert(0, (yp, xp, self.data[yp][xp]))
428+
self.data[yp][xp] = result
429+
self.set_term_title(True)
430+
curses.curs_set(0)
464431

465432
def undo_redo(self, undo=True):
466433
if undo:

0 commit comments

Comments
 (0)