Fix bug moving down/up cols and try using a config.py

This commit is contained in:
Joseph Ferano 2023-11-06 16:47:07 +07:00
parent d690d7a2a3
commit 27ef7d7c93
3 changed files with 64 additions and 34 deletions

8
config.py Normal file
View File

@ -0,0 +1,8 @@
from editing import *
keymap = {
'left': cursor_go_left,
'right': cursor_go_right,
'up': cursor_go_up,
'down': cursor_go_down
}

View File

@ -1,8 +1,5 @@
from fide import * from fide import *
global ctx
ctx = GlobalContext()
def cursor_go_left(): def cursor_go_left():
buf = ctx.current_buffer buf = ctx.current_buffer
cursor = ctx.current_buffer.cursor cursor = ctx.current_buffer.cursor
@ -30,19 +27,32 @@ def cursor_go_right():
def cursor_go_down(): def cursor_go_down():
buf = ctx.current_buffer buf = ctx.current_buffer
cursor = ctx.current_buffer.cursor 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) 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) cursor.col = min(cursor.want_col, line_len)
def cursor_go_up(): def cursor_go_up():
buf = ctx.current_buffer buf = ctx.current_buffer
cursor = ctx.current_buffer.cursor cursor = ctx.current_buffer.cursor
if cursor.line_num == 0:
return
cursor.line_num = max(0, cursor.line_num - 1) 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 # TODO: This should be global
line_len = 20
cursor.col = min(cursor.want_col, line_len) 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): def buffer_insert_line_below(buf: Buffer, text):
line_num = buffer_line_num(buf) line_num = buffer_line_num(buf)
buf.lines.insert(line_num + 1, line_create(text)) buf.lines.insert(line_num + 1, line_create(text))

68
fide.py
View File

@ -12,7 +12,6 @@ from dataclasses import dataclass
from typing import List from typing import List
from code import InteractiveConsole, InteractiveInterpreter from code import InteractiveConsole, InteractiveInterpreter
from editing import *
@dataclass(slots=True) @dataclass(slots=True)
class Cursor: class Cursor:
@ -32,7 +31,6 @@ class Buffer:
lines: List[Line] lines: List[Line]
cursor: Cursor cursor: Cursor
def cursor_pos(cursor: Cursor): def cursor_pos(cursor: Cursor):
return (cursor.line_num, cursor.col) return (cursor.line_num, cursor.col)
@ -61,9 +59,31 @@ def buffer_total_lines(buf: Buffer) -> int:
def buffer_line_len(buf: Buffer) -> int: 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:
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_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny)
os.environ['SDL_VIDEO_X11_WMCLASS'] = "pygame"
clock = pg.time.Clock() clock = pg.time.Clock()
pg.init() pg.init()
@ -85,43 +105,44 @@ class GlobalContext:
current_buffer = buffer_create(text, cursor) current_buffer = buffer_create(text, cursor)
# global ctx # global ctx
global ctx
ctx = GlobalContext()
from config import *
from editing import *
def main(): def main():
running = True running = True
frame_count = 0 frame_count = 0
cursor_flash = 0 cursor_flash = 0
cursor_on = True cursor_on = True
line_height = 20 line_height = 20
cursor = ctx.cursor cursor = ctx.cursor
current_buffer = ctx.current_buffer current_buffer = ctx.current_buffer
console = InteractiveInterpreter(locals()) console = InteractiveInterpreter(locals())
console.runsource('from editing import *') 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: while running:
dt = clock.tick(120) / 1000.0 dt = clock.tick(120) / 1000.0
screen.fill('white') screen.fill('white')
frame_count += 1 frame_count += 1
for event in pg.event.get(): for event in pg.event.get():
# print(pg.event.event_name(event.type))
if event.type == pg.QUIT: if event.type == pg.QUIT:
running = False running = False
if event.type == pg.KEYDOWN: if event.type == pg.KEYDOWN:
# print(pg.key.name(event.key))
cursor_on = True cursor_on = True
cursor_flash = 0 cursor_flash = 0
curr_line = buffer_line_current(current_buffer) curr_line = buffer_line_current(current_buffer)
if event.mod == 64: # Right Ctrl if event.mod == 64: # Right Ctrl
if event.key == pg.K_e: if event.key == pg.K_e:
cursor.col = buffer_line_len(buf) cursor_go_end_line()
cursor.want_col = cursor.col
if event.key == pg.K_a: if event.key == pg.K_a:
cursor.col = 0 cursor_go_beginning_line()
cursor.want_col = cursor.col
if event.key == pg.K_RETURN: if event.key == pg.K_RETURN:
src = buffer_line_current(current_buffer).piece.get_text() src = buffer_line_current(current_buffer).piece.get_text()
# try:
# eval(src[3:])
# except:
# print('error evaluating line: ' + src[3:])
console.runsource(src[3:]) console.runsource(src[3:])
elif event.key == pg.K_RETURN: elif event.key == pg.K_RETURN:
buffer_newline_at_pos() buffer_newline_at_pos()
@ -129,20 +150,11 @@ def main():
buffer_delete_character_under_cursor() buffer_delete_character_under_cursor()
elif event.key == pg.K_DELETE: elif event.key == pg.K_DELETE:
buffer_delete_next_character() buffer_delete_next_character()
elif event.key == pg.K_LEFT: elif pg.key.name(event.key) in keymap:
key_map['left']() keymap[pg.key.name(event.key)]()
elif event.key == pg.K_RIGHT: # pass
key_map['right']() # elif event.mod < 2:
elif event.key == pg.K_UP: elif event.unicode:
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:
curr_line.piece.insert(event.unicode, cursor.col) curr_line.piece.insert(event.unicode, cursor.col)
curr_line.length += 1 curr_line.length += 1
cursor.col += 1 cursor.col += 1