📝 Quality & Cursor changes

This commit is contained in:
Maddie H 2022-03-19 16:34:48 +00:00
parent dd2e5a49af
commit 4721cee438
4 changed files with 44 additions and 29 deletions

View File

@ -1,7 +1,7 @@
import curses
def cursor_mode(mode):
def cursor_mode(mode: str):
if mode == "block":
print("\033[2 q")
@ -15,26 +15,36 @@ def cursor_mode(mode):
curses.curs_set(1)
def cursor_push(cursor: list, direction: (int, str)) -> list:
def cursor_push(instance, direction: (int, str)):
if direction in (0, "up", "north"):
# Decrease the y position
cursor[0] -= 1
instance.cursor[0] -= 1
elif direction in (1, "right", "east"):
# Increase the x position
cursor[1] += 1
instance.cursor[1] += 1
elif direction in (2, "down", "south"):
# Increase the y position
cursor[0] += 1
instance.cursor[0] += 1
elif direction in (3, "left", "west"):
# Decrease the x position
cursor[1] -= 1
instance.cursor[1] -= 1
def check_cursor(instance, cursor: list):
cursor[1] = max(2, cursor[1])
cursor[1] = min(instance.width - 1, cursor[1])
cursor[0] = max(0, cursor[0])
cursor[0] = min(instance.height - 2 - len(instance.components.components["bottom"]), cursor[0])
return cursor
def cursor_move(screen, cursor: list):
def cursor_move(instance):
# Run a final check to see if the cursor is valid
instance.cursor = check_cursor(instance, instance.cursor)
# Moves the cursor to anywhere on the screen
screen.move(cursor[0], cursor[1])
instance.screen.move(instance.cursor[0], instance.cursor[1])

View File

@ -1,3 +1,5 @@
from core.colors import Codes as c
import traceback
import curses
import sys
@ -107,3 +109,16 @@ def error(instance, message: str):
# Clear the bottom of the screen
clear(instance, instance.height - 1, 0)
def fatal_error(exception: Exception):
# Clean up the screen
curses.endwin()
# Print the error message and traceback
print(f"{c.red}FATAL ERROR:{c.end} "
f"{c.yellow}{exception}{c.end}\n")
print(traceback.format_exc())
# Exit, with an error exit code
sys.exit(0)

22
main.py
View File

@ -1,7 +1,6 @@
from core import colors, cursors, buffers, modes, utils
from core.buffers import Buffer
from core.components import Components
import traceback
import argparse
import curses
import sys
@ -35,9 +34,13 @@ class Lambda:
self.components.render(self)
# Move the cursor
cursors.cursor_move(self.screen, self.cursor)
cursors.cursor_move(self)
def start(self):
# Change the escape key delay to 25ms
# Fixes an issue where the "esc" key takes way too long to press
os.environ.setdefault("ESCDELAY", "25")
# Initialise colors
colors.init_colors()
@ -86,10 +89,6 @@ def main():
# Load the file into a Buffer object
buffer = buffers.load_file(args.file)
# Change the escape delay to 25ms
# Fixes an issue where esc takes way too long to press
os.environ.setdefault("ESCDELAY", "25")
# Load lambda with the buffer object
screen = Lambda(buffer)
@ -107,16 +106,7 @@ def main():
# Excepts *any* errors that occur
except Exception as exception:
# Clean up the screen
curses.endwin()
# Print the error message and traceback
print(f"{colors.Codes.red}FATAL ERROR:{colors.Codes.end} "
f"{colors.Codes.yellow}{exception}{colors.Codes.end}\n")
print(traceback.format_exc())
# Exit, with an error code
sys.exit(0)
utils.fatal_error(exception)
if __name__ == "__main__":

View File

@ -6,19 +6,19 @@ from mode import command
def execute(instance, key):
if key == ord("j"):
# Move the cursor down
instance.cursor = cursors.cursor_push(instance.cursor, "down")
cursors.cursor_push(instance, "down")
elif key == ord("k"):
# Move the cursor up
instance.cursor = cursors.cursor_push(instance.cursor, "up")
cursors.cursor_push(instance, "up")
elif key == ord("l"):
# Move the cursor right
instance.cursor = cursors.cursor_push(instance.cursor, "right")
cursors.cursor_push(instance, "right")
elif key == ord("h"):
# Move the cursor left
instance.cursor = cursors.cursor_push(instance.cursor, "left")
cursors.cursor_push(instance, "left")
elif key == ord("i"):
# Activate insert mode
@ -26,7 +26,7 @@ def execute(instance, key):
elif key == ord("I"):
# Move the cursor to the right
instance.cursor = cursors.cursor_push(instance.cursor, "right")
cursors.cursor_push(instance, "right")
# Then activate insert mode
modes.activate(instance, "insert")