editor: Add overwriting mode

- Using latest version of fill3 that recognizes the insert key.
This commit is contained in:
Andrew Hamilton 2022-01-28 19:42:55 +10:00
parent 5c0d44df2e
commit dfe075b11c
4 changed files with 20 additions and 5 deletions

2
TODO
View file

@ -5,7 +5,6 @@ Todo:
- Bulk indent/dedent.
- Search.
- Search and replace.
- Overwrite mode.
- Large pastes.
@ -31,6 +30,7 @@ Done:
- Put it on github.
- tab key should align code.
- Right align the left editor.
- Overwrite mode.
Shelved:

View file

@ -194,6 +194,7 @@ class Editor:
self.last_height = 40
self.is_editing = True
self.theme_index = 0
self.is_overwriting = False
self.previous_term_code = None
self.history = []
@ -341,8 +342,9 @@ class Editor:
def insert_text(self, text):
try:
current_line = self.text_widget[self.cursor_y]
new_line = current_line[:self.cursor_x] + text + current_line[self.cursor_x:]
self.text_widget[self.cursor_y] = new_line
replace_count = len(text) if self.is_overwriting else 0
self.text_widget[self.cursor_y] = (current_line[:self.cursor_x] + text
+ current_line[self.cursor_x+replace_count:])
except IndexError:
self.text_widget.append(text)
self.cursor_x += len(text)
@ -533,6 +535,9 @@ class Editor:
def undo(self):
self.text_widget[:], self._cursor_x, self._cursor_y = self.history.pop()
def toggle_overwrite(self):
self.is_overwriting = not self.is_overwriting
def abort_command(self):
self.mark = None
self.ring_bell()
@ -644,7 +649,8 @@ class Editor:
terminal.CTRL_L: center_cursor, terminal.ALT_SEMICOLON: comment_lines,
terminal.ALT_c: cycle_syntax_highlighting, terminal.CTRL_X: prefix, terminal.ESC: quit,
terminal.CTRL_C: ctrl_c, terminal.CTRL_K: delete_line, terminal.TAB: tab_align,
terminal.CTRL_UNDERSCORE: undo, terminal.CTRL_Z: undo, terminal.CTRL_G: abort_command}
terminal.CTRL_UNDERSCORE: undo, terminal.CTRL_Z: undo, terminal.CTRL_G: abort_command,
terminal.INSERT: toggle_overwrite}
def main():

View file

@ -18,4 +18,4 @@ setup(name="diff-edit",
entry_points={"console_scripts": ["diff-edit=diff_edit:main"]},
install_requires=[
"pygments==2.10.0", "docopt==0.6.2",
"fill3 @ git+https://github.com/ahamilton/eris@v2022.01.27#subdirectory=fill3"])
"fill3 @ git+https://github.com/ahamilton/eris@v2022.01.28#subdirectory=fill3"])

View file

@ -80,6 +80,15 @@ class EditorTestCase(unittest.TestCase):
self._assert_editor("a", (1, 0))
self.editor.insert_text("bc")
self._assert_editor("abc", (3, 0))
# overwrite
self.editor.toggle_overwrite()
self.editor.cursor_left()
self.editor.insert_text("d")
self._assert_editor("abd", (3, 0))
self.editor.cursor_left()
self.editor.cursor_left()
self.editor.insert_text("ef")
self._assert_editor("aef", (3, 0))
def test_enter(self):
self._set_editor("ab", (1, 0))