Velocity/core/cursors.py

75 lines
2.6 KiB
Python
Raw Normal View History

2022-03-19 15:13:28 +00:00
import curses
2022-03-19 19:19:25 +00:00
def mode(to_mode: str):
if to_mode == "block":
2022-03-19 15:13:28 +00:00
print("\033[2 q")
2022-03-19 19:19:25 +00:00
elif to_mode == "line":
2022-03-19 15:13:28 +00:00
print("\033[6 q")
2022-03-19 19:19:25 +00:00
elif to_mode == "hidden":
2022-03-19 15:13:28 +00:00
curses.curs_set(0)
2022-03-19 19:19:25 +00:00
elif to_mode == "visible":
2022-03-19 15:13:28 +00:00
curses.curs_set(1)
2022-03-19 19:19:25 +00:00
def push(instance, direction: (int, str)):
2022-03-19 15:13:28 +00:00
if direction in (0, "up", "north"):
2022-03-19 19:19:25 +00:00
# If the cursor is at the top of the file
if instance.cursor[0] == 0 and not instance.offset[0] == 0:
# Move the buffer up
instance.offset[0] -= 1
elif instance.cursor[0] != 0:
# Decrease the y position of the cursor
instance.cursor[0] -= 1
# Jump to the end of the line
if instance.cursor[1] > len(instance.buffer.data[instance.current_line - 1]) - 2:
instance.cursor[1] = len(instance.buffer.data[instance.current_line - 1]) - 2
2022-03-19 15:13:28 +00:00
elif direction in (1, "right", "east"):
2022-03-19 19:19:25 +00:00
# Increase the x position of the cursor
if instance.cursor[1] < len(instance.buffer.data[instance.current_line]) - 2:
instance.cursor[1] += 1
2022-03-19 15:13:28 +00:00
elif direction in (2, "down", "south"):
2022-03-19 19:19:25 +00:00
# Check if the cursor is at the bottom of the screen
if instance.cursor[0] == instance.safe_height - 2 and not instance.current_line == len(instance.buffer.data):
# Move the buffer down
instance.offset[0] += 1
elif instance.cursor[0] != instance.safe_height - 2:
# Increase the y position of the cursor
instance.cursor[0] += 1
# Jump to the end of the line
if instance.cursor[1] > len(instance.buffer.data[instance.current_line + 1]) - 2:
instance.cursor[1] = len(instance.buffer.data[instance.current_line + 1]) - 2
2022-03-19 15:13:28 +00:00
elif direction in (3, "left", "west"):
2022-03-19 19:19:25 +00:00
# Decrease the x position of the cursor
if instance.cursor[1] != 0:
instance.cursor[1] -= 1
2022-03-19 16:34:48 +00:00
2022-03-19 19:19:25 +00:00
def check(instance, cursor: list):
# Prevent any values out of bounds (especially important when resizing)
cursor[1] = max(0, cursor[1])
cursor[1] = min(instance.safe_width - 1, cursor[1])
2022-03-19 16:34:48 +00:00
cursor[0] = max(0, cursor[0])
cursor[0] = min(instance.height - 2 - len(instance.components.components["bottom"]), cursor[0])
2022-03-19 15:13:28 +00:00
return cursor
2022-03-19 19:19:25 +00:00
def move(instance):
2022-03-19 16:34:48 +00:00
# Run a final check to see if the cursor is valid
2022-03-19 19:19:25 +00:00
instance.cursor = check(instance, instance.cursor)
2022-03-19 16:34:48 +00:00
2022-03-19 15:13:28 +00:00
# Moves the cursor to anywhere on the screen
2022-03-19 19:19:25 +00:00
instance.screen.move(instance.cursor[0], instance.cursor[1] +
instance.components.get_component_width(instance.components.components["left"]))