Compare commits

...

3 Commits

4 changed files with 135 additions and 88 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 *
global ctx
ctx = GlobalContext()
def cursor_go_left():
buf = ctx.current_buffer
cursor = ctx.current_buffer.cursor
@ -30,15 +27,95 @@ 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))
def buffer_newline_at_pos():
buf = ctx.current_buffer
curr_line = buffer_line_current(buf)
cursor = buf.cursor
if cursor.col < buffer_line_len(buf):
remainder = curr_line.length - cursor.col
line = curr_line.piece.string_at(cursor.col, remainder)
curr_line.piece.delete(cursor.col, remainder)
curr_line.length -= remainder
else:
line = ""
buffer_insert_line_below(buf, line)
cursor.line_num += 1
# Eventually when we need to be indentation aware
cursor.col = 0
cursor.want_col = 0
def buffer_delete_character_under_cursor():
buf = ctx.current_buffer
cursor = buf.cursor
if cursor_pos(cursor) != (0,0):
if cursor.col == 0:
curr_line = buf.lines.pop(cursor.line_num)
cursor.line_num -= 1
above_row = buf.lines[cursor.line_num]
above_row.piece.insert(curr_line.piece.get_text(), above_row.length)
cursor.col = above_row.length
cursor.want_col = cursor.col
above_row.length += curr_line.length
else:
curr_line = buffer_line_current(buf)
curr_line.piece.delete(cursor.col - 1, 1)
curr_line.length -= 1
cursor.col = max(0, cursor.col - 1)
cursor.want_col = cursor.col
def buffer_delete_next_character():
buf = ctx.current_buffer
cursor = buf.cursor
curr_line = buffer_line_current(buf)
if cursor.col >= buffer_line_len(buf):
if cursor.line_num < len(buf.lines) - 1:
below_row = buf.lines.pop(cursor.line_num+1)
if below_row.length > 0:
curr_line.piece.insert(below_row.piece.get_text(), curr_line.length)
curr_line.length += below_row.length
cursor.want_col = cursor.col
else:
if curr_line.length >= 1:
curr_line.piece.delete(cursor.col, 1)
curr_line.length -= 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)
curr_line.piece.insert(text, cursor.col)
text_len = len(text)
curr_line.length += text_len
cursor.col += text_len
cursor.want_col = text_len

120
fide.py
View File

@ -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,47 +59,31 @@ def buffer_total_lines(buf: Buffer) -> int:
def buffer_line_len(buf: Buffer) -> int:
return buffer_line_current(buf).length
def buffer_insert_line_below(buf: Buffer, text):
line_num = buffer_line_num(buf)
buf.lines.insert(line_num + 1, line_create(text))
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-'
def buffer_newline_at_pos(buf):
curr_line = buffer_line_current(buf)
cursor = buf.cursor
if cursor.col < buffer_line_len(buf):
remainder = curr_line.length - cursor.col
line = curr_line.piece.string_at(cursor.col, remainder)
curr_line.piece.delete(cursor.col, remainder)
curr_line.length -= remainder
else:
line = ""
buffer_insert_line_below(buf, line)
cursor.line_num += 1
# Eventually when we need to be indentation aware
cursor.col = 0
cursor.want_col = 0
case 'right ctrl':
return 'RC-'
case 'right alt':
return 'RA-'
case 'right meta':
return 'RM-'
case 'right shift':
return 'RS-'
def buffer_delete_character_at_pos(buf):
cursor = buf.cursor
if cursor_pos(cursor) != (0,0):
if cursor.col == 0:
curr_line = buf.lines.pop(cursor.line_num)
cursor.line_num -= 1
above_row = buf.lines[cursor.line_num]
above_row.piece.insert(curr_line.piece.get_text(), above_row.length)
cursor.col = above_row.length
cursor.want_col = cursor.col
above_row.length += curr_line.length
else:
curr_line = buffer_line_current(buf)
curr_line.piece.delete(cursor.col - 1, 1)
curr_line.length -= 1
cursor.col = max(0, cursor.col - 1)
cursor.want_col = cursor.col
oswinx, oswiny = 1940,50
# 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()
@ -123,76 +105,56 @@ 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:])
# console.runcode()
elif event.key == pg.K_RETURN:
buffer_newline_at_pos(current_buffer)
buffer_newline_at_pos()
elif event.key == pg.K_BACKSPACE:
buffer_delete_character_at_pos(current_buffer)
buffer_delete_character_under_cursor()
elif event.key == pg.K_DELETE:
if cursor.col >= buffer_line_len(current_buffer):
if cursor.line_num < len(current_buffer.lines) - 1:
below_row = current_buffer.lines.pop(cursor.line_num+1)
if below_row.length > 0:
curr_line.piece.insert(below_row.piece.get_text(), curr_line.length)
curr_line.length += below_row.length
cursor.want_col = cursor.col
else:
if curr_line.length >= 1:
curr_line.piece.delete(cursor.col, 1)
curr_line.length -= 1
cursor.want_col = cursor.col
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:
buffer_delete_next_character()
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

View File

@ -104,16 +104,16 @@ proc main =
renderer.clear()
for i, line in buffer.lines:
if isEmptyOrWhitespace(line):
continue
let
textColor = color(0, 0, 0, 0)
surface = ttf.renderTextBlended(font, line, textColor)
texture = renderer.createTextureFromSurface(surface)
sdlFailIf surface.isNil: "Could not render text surface"
# var r = rect(0, cint(i * 20), surface.w, surface.h)
# echo "W ", *surface.w "H ", *surface.h
var r = rect(0, cint(i * 20), surface.w, surface.h)
var r = rect(0, cint(i * 20), 100, 20)
renderer.copy texture, nil, addr r
surface.freeSurface