Extract more functions and add ctrl+key bindings

This commit is contained in:
Joseph Ferano 2023-11-06 18:39:34 +07:00
parent 27ef7d7c93
commit 5cb2fd0fd7
3 changed files with 30 additions and 12 deletions

View File

@ -4,5 +4,7 @@ keymap = {
'left': cursor_go_left,
'right': cursor_go_right,
'up': cursor_go_up,
'down': cursor_go_down
'down': cursor_go_down,
'C-e': cursor_go_line_end,
'C-a': cursor_go_line_beginning,
}

View File

@ -43,12 +43,12 @@ def cursor_go_up():
# TODO: This should be global
cursor.col = min(cursor.want_col, line_len)
def cursor_go_beginning_line():
def cursor_go_line_beginning():
buf = ctx.current_buffer
buf.cursor.col = 0
buf.cursor.want_col = buf.cursor.col
def cursor_go_end_line():
def cursor_go_line_end():
buf = ctx.current_buffer
buf.cursor.col = buffer_line_len(buf)
buf.cursor.want_col = buf.cursor.col
@ -111,6 +111,14 @@ def buffer_delete_next_character():
curr_line.length -= 1
cursor.want_col = cursor.col
def buffer_insert_char(c):
cursor = ctx.current_buffer.cursor
curr_line = buffer_line_current(ctx.current_buffer)
curr_line.piece.insert(c, cursor.col)
curr_line.length += 1
cursor.col += 1
cursor.want_col = cursor.col
def buffer_insert_text_at_cursor(text):
cursor = ctx.current_buffer.cursor
curr_line = buffer_line_current(ctx.current_buffer)

26
fide.py
View File

@ -57,7 +57,7 @@ def buffer_total_lines(buf: Buffer) -> int:
return len(buf.lines)
def buffer_line_len(buf: Buffer) -> int:
return buffer_line_current(buf).length
return buffer_line_current(buf).length
def translate_keyname(keyname: str) -> str:
match keyname:
@ -133,10 +133,19 @@ def main():
cursor_flash = 0
curr_line = buffer_line_current(current_buffer)
if event.mod == 64: # Right Ctrl
if event.key == pg.K_e:
cursor_go_end_line()
if event.key == pg.K_a:
cursor_go_beginning_line()
# this is inefficient, might as well just pass the mod number
# and make the mapping mod_num -> config_key
mod_key = translate_keyname('left ctrl')
if event.unicode:
# print(mod_key, event.key, event.unicode)
key = mod_key + pg.key.name(event.key)
if key in keymap:
keymap[key]()
# if event.key == pg.K_e:
# cursor_go_line_end()
# if event.key == pg.K_a:
# cursor_go_line_beginning()
if event.key == pg.K_RETURN:
src = buffer_line_current(current_buffer).piece.get_text()
# try:
@ -144,6 +153,8 @@ def main():
# except:
# print('error evaluating line: ' + src[3:])
console.runsource(src[3:])
elif pg.key.name(event.key) == 'escape':
pass
elif event.key == pg.K_RETURN:
buffer_newline_at_pos()
elif event.key == pg.K_BACKSPACE:
@ -155,10 +166,7 @@ def main():
# pass
# elif event.mod < 2:
elif event.unicode:
curr_line.piece.insert(event.unicode, cursor.col)
curr_line.length += 1
cursor.col += 1
cursor.want_col = cursor.col
buffer_insert_char(event.unicode)
for i,curr_line in enumerate(current_buffer.lines):
render = text_renderer.render(curr_line.piece.get_text(), True, 'black')