cursor follows buffer

This commit is contained in:
Maddie H 2022-03-17 18:46:04 +00:00
parent ab9e88bad5
commit 208e66d9fc
6 changed files with 49 additions and 34 deletions

Binary file not shown.

View File

@ -13,6 +13,12 @@ def cursor_mode(mode):
def check_cursor(data):
if data["cursor_y"] == len(data["buffer_list"]):
data["cursor_y"] -= 1
elif data["cursor_x"] > len(data["buffer_list"][data["cursor_y"]]):
data["cursor_x"] = len(data["buffer_list"][data["cursor_y"]])
data["cursor_x"] = max(2, data["cursor_x"])
data["cursor_x"] = min(data["width"] - 1, data["cursor_x"])
data["cursor_y"] = max(0, data["cursor_y"])

29
core/welcome.py Normal file
View File

@ -0,0 +1,29 @@
import curses
def start_screen(stdscr):
# Get window height and width
height, width = stdscr.getmaxyx()
# Startup text
title = "λ Lambda"
subtext = [
"Next generation hackable text editor for nerds",
"",
"Type :h to open the README.md document",
"Type :o <file> to open a file and edit",
"Type :q or <C-c> to quit lambda.py"
]
# Centering calculations
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
start_y = int((height // 2) - 2)
# Rendering title
stdscr.addstr(start_y, start_x_title, title, curses.color_pair(7) | curses.A_BOLD)
# Print the subtext
for text in subtext:
start_y += 1
start_x = int((width // 2) - (len(text) // 2) - len(text) % 2)
stdscr.addstr(start_y, start_x, text)

View File

@ -1,37 +1,9 @@
from core import colors, cursor, mode, files, buffer
from core import colors, cursor, mode, files, buffer, welcome
import os
import curses
import argparse
def start_screen(stdscr):
# Get window height and width
height, width = stdscr.getmaxyx()
# Startup text
title = "λ Lambda"
subtext = [
"Next generation hackable text editor for nerds",
"",
"Type :h to open the README.md document",
"Type :o <file> to open a file and edit",
"Type :q or <C-c> to quit lambda.py"
]
# Centering calculations
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
start_y = int((height // 2) - 2)
# Rendering title
stdscr.addstr(start_y, start_x_title, title, curses.color_pair(7) | curses.A_BOLD)
# Print the subtext
for text in subtext:
start_y += 1
start_x = int((width // 2) - (len(text) // 2) - len(text) % 2)
stdscr.addstr(start_y, start_x, text)
def start(stdscr, buffer_name, buffer_list):
# Initialise data before starting
data = {
@ -55,7 +27,7 @@ def start(stdscr, buffer_name, buffer_list):
# Start the screen
if data["buffer_name"] == "[No Name]":
start_screen(stdscr)
welcome.start_screen(stdscr)
# Main loop
while True:
@ -74,33 +46,41 @@ def start(stdscr, buffer_name, buffer_list):
def main():
# Arguments
parser = argparse.ArgumentParser(description="Next generation hackable text editor for nerds.")
parser.add_argument("file", metavar="file", type=str, nargs="?",
help="The name of a file for lambda to open")
# Collect the arguments
args = parser.parse_args()
# Check if a file name has been inputted
# Check if a file name has actually been inputted
if args.file:
buffer_name = args.file
# Set the buffer name
buffer_name = os.path.basename(args.file)
# Only if the file exists
if os.path.exists(args.file):
# Load the file into the buffer
buffer_list = files.open_file(buffer_name)
buffer_list = files.open_file(args.file)
# Convert each line into a list of lists with each element of the sublist representing one character
for index, line in enumerate(buffer_list):
buffer_list[index] = list(line)
else:
# New file being created, no content
buffer_list = [[""]]
else:
# Buffer has no name
buffer_name = "[No Name]"
# Buffer has no value
buffer_list = [[""]]
# Change the escape delay to 25ms
# Fixes an issue where esc takes too long to press
# Fixes an issue where esc takes way too long to press
os.environ.setdefault("ESCDELAY", "25")
# Initialise the screen