From 27ef7d7c93fdd5b73e17d4111012bfa03cb6df14 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 6 Nov 2023 16:47:07 +0700 Subject: [PATCH] Fix bug moving down/up cols and try using a config.py --- config.py | 8 +++++++ editing.py | 22 +++++++++++++----- fide.py | 68 ++++++++++++++++++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 34 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..3f418c1 --- /dev/null +++ b/config.py @@ -0,0 +1,8 @@ +from editing import * + +keymap = { + 'left': cursor_go_left, + 'right': cursor_go_right, + 'up': cursor_go_up, + 'down': cursor_go_down +} diff --git a/editing.py b/editing.py index 1fc7ad4..8a31363 100644 --- a/editing.py +++ b/editing.py @@ -1,8 +1,5 @@ from fide import * -global ctx -ctx = GlobalContext() - def cursor_go_left(): buf = ctx.current_buffer cursor = ctx.current_buffer.cursor @@ -30,19 +27,32 @@ def cursor_go_right(): def cursor_go_down(): buf = ctx.current_buffer cursor = ctx.current_buffer.cursor - line_len = buffer_line_len(buf) + if cursor.line_num == len(buf.lines): + return cursor.line_num = min(len(buf.lines) - 1, cursor.line_num + 1) + line_len = buf.lines[cursor.line_num].length cursor.col = min(cursor.want_col, line_len) def cursor_go_up(): buf = ctx.current_buffer cursor = ctx.current_buffer.cursor + if cursor.line_num == 0: + return cursor.line_num = max(0, cursor.line_num - 1) - line_len = buffer_line_len(buf) + line_len = buf.lines[cursor.line_num].length # TODO: This should be global - line_len = 20 cursor.col = min(cursor.want_col, line_len) +def cursor_go_beginning_line(): + buf = ctx.current_buffer + buf.cursor.col = 0 + buf.cursor.want_col = buf.cursor.col + +def cursor_go_end_line(): + buf = ctx.current_buffer + buf.cursor.col = buffer_line_len(buf) + buf.cursor.want_col = buf.cursor.col + def buffer_insert_line_below(buf: Buffer, text): line_num = buffer_line_num(buf) buf.lines.insert(line_num + 1, line_create(text)) diff --git a/fide.py b/fide.py index aa803f2..1a61e4e 100755 --- a/fide.py +++ b/fide.py @@ -12,7 +12,6 @@ from dataclasses import dataclass from typing import List from code import InteractiveConsole, InteractiveInterpreter -from editing import * @dataclass(slots=True) class Cursor: @@ -32,7 +31,6 @@ class Buffer: lines: List[Line] cursor: Cursor - def cursor_pos(cursor: Cursor): return (cursor.line_num, cursor.col) @@ -61,9 +59,31 @@ def buffer_total_lines(buf: Buffer) -> int: def buffer_line_len(buf: Buffer) -> int: return buffer_line_current(buf).length +def translate_keyname(keyname: str) -> str: + match keyname: + case 'left ctrl': + return 'C-' + case 'left alt': + return 'A-' + case 'left meta': + return 'M-' + case 'left shift': + return 'S-' -oswinx, oswiny = 1940,50 + case 'right ctrl': + return 'RC-' + case 'right alt': + return 'RA-' + case 'right meta': + return 'RM-' + case 'right shift': + return 'RS-' + + +# oswinx, oswiny = 1940,50 +oswinx, oswiny = 20,50 os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny) +os.environ['SDL_VIDEO_X11_WMCLASS'] = "pygame" clock = pg.time.Clock() pg.init() @@ -85,43 +105,44 @@ class GlobalContext: current_buffer = buffer_create(text, cursor) # global ctx +global ctx +ctx = GlobalContext() +from config import * +from editing import * def main(): running = True frame_count = 0 cursor_flash = 0 cursor_on = True line_height = 20 - cursor = ctx.cursor current_buffer = ctx.current_buffer console = InteractiveInterpreter(locals()) console.runsource('from editing import *') - key_map = { - 'left': cursor_go_left, - 'right': cursor_go_right, - 'up': cursor_go_up, - 'down': cursor_go_down - } while running: dt = clock.tick(120) / 1000.0 screen.fill('white') frame_count += 1 for event in pg.event.get(): + # print(pg.event.event_name(event.type)) if event.type == pg.QUIT: running = False if event.type == pg.KEYDOWN: + # print(pg.key.name(event.key)) cursor_on = True cursor_flash = 0 curr_line = buffer_line_current(current_buffer) if event.mod == 64: # Right Ctrl if event.key == pg.K_e: - cursor.col = buffer_line_len(buf) - cursor.want_col = cursor.col + cursor_go_end_line() if event.key == pg.K_a: - cursor.col = 0 - cursor.want_col = cursor.col + cursor_go_beginning_line() if event.key == pg.K_RETURN: src = buffer_line_current(current_buffer).piece.get_text() + # try: + # eval(src[3:]) + # except: + # print('error evaluating line: ' + src[3:]) console.runsource(src[3:]) elif event.key == pg.K_RETURN: buffer_newline_at_pos() @@ -129,20 +150,11 @@ def main(): buffer_delete_character_under_cursor() elif event.key == pg.K_DELETE: buffer_delete_next_character() - elif event.key == pg.K_LEFT: - key_map['left']() - elif event.key == pg.K_RIGHT: - key_map['right']() - elif event.key == pg.K_UP: - key_map['up']() - elif event.key == pg.K_DOWN: - key_map['down']() - elif event.key == pg.K_SPACE: - curr_line.piece.insert(u' ', cursor.col) - curr_line.length += 1 - cursor.col += 1 - cursor.want_col = cursor.col - elif event.key >= 33 and event.key <= 126 and event.mod < 2: + elif pg.key.name(event.key) in keymap: + keymap[pg.key.name(event.key)]() + # pass + # elif event.mod < 2: + elif event.unicode: curr_line.piece.insert(event.unicode, cursor.col) curr_line.length += 1 cursor.col += 1