Velocity/lambda.py

100 lines
2.6 KiB
Python
Raw Normal View History

2022-03-17 18:46:04 +00:00
from core import colors, cursor, mode, files, buffer, welcome
2022-03-15 22:12:52 +00:00
import os
import curses
import argparse
2022-03-17 21:47:19 +00:00
def start(screen, buffer_name, buffer_list):
2022-03-15 22:12:52 +00:00
# Initialise data before starting
data = {
"cursor_y": 0,
"cursor_x": 0,
"height": 0,
"width": 0,
"mode": "normal",
"icon": "λ",
"info_bar": [" "],
"buffer_name": buffer_name,
"buffer_list": buffer_list,
2022-03-17 21:47:19 +00:00
"visible_y": 0,
"visible_x": 0,
2022-03-15 22:12:52 +00:00
"statusbar_theme": "filled"
}
# Initialise colors
colors.init_colors()
# Change the cursor shape
cursor.cursor_mode("block")
# Start the screen
if data["buffer_name"] == "[No Name]":
2022-03-17 21:47:19 +00:00
welcome.start_screen(screen)
2022-03-15 22:12:52 +00:00
# Main loop
while True:
# Get the height and width of the screen
2022-03-17 21:47:19 +00:00
data["height"], data["width"] = screen.getmaxyx()
2022-03-15 22:12:52 +00:00
# Write the buffer to the screen
2022-03-17 21:47:19 +00:00
buffer.write_buffer(screen, data)
2022-03-15 22:12:52 +00:00
# Activate the next mode
2022-03-17 21:47:19 +00:00
data = mode.activate(screen, data)
# Write the buffer to the screen
buffer.write_buffer(screen, data)
2022-03-15 22:12:52 +00:00
# Refresh and clear the screen
2022-03-17 21:47:19 +00:00
screen.refresh()
screen.clear()
# Write the buffer to the screen
buffer.write_buffer(screen, data)
2022-03-15 22:12:52 +00:00
def main():
2022-03-17 18:46:04 +00:00
# Arguments
2022-03-16 17:23:37 +00:00
parser = argparse.ArgumentParser(description="Next generation hackable text editor for nerds.")
2022-03-15 22:12:52 +00:00
parser.add_argument("file", metavar="file", type=str, nargs="?",
2022-03-16 17:23:37 +00:00
help="The name of a file for lambda to open")
2022-03-15 22:12:52 +00:00
2022-03-17 18:46:04 +00:00
# Collect the arguments
2022-03-15 22:12:52 +00:00
args = parser.parse_args()
2022-03-17 18:46:04 +00:00
# Check if a file name has actually been inputted
2022-03-15 22:12:52 +00:00
if args.file:
2022-03-17 18:46:04 +00:00
# Set the buffer name
buffer_name = os.path.basename(args.file)
2022-03-15 22:12:52 +00:00
2022-03-16 17:23:37 +00:00
# Only if the file exists
if os.path.exists(args.file):
# Load the file into the buffer
2022-03-17 18:46:04 +00:00
buffer_list = files.open_file(args.file)
2022-03-16 17:23:37 +00:00
# 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:
2022-03-17 18:46:04 +00:00
# New file being created, no content
2022-03-16 17:23:37 +00:00
buffer_list = [[""]]
2022-03-15 22:12:52 +00:00
else:
2022-03-17 18:46:04 +00:00
# Buffer has no name
2022-03-15 22:12:52 +00:00
buffer_name = "[No Name]"
2022-03-17 18:46:04 +00:00
# Buffer has no value
2022-03-15 22:12:52 +00:00
buffer_list = [[""]]
# Change the escape delay to 25ms
2022-03-17 18:46:04 +00:00
# Fixes an issue where esc takes way too long to press
2022-03-15 22:12:52 +00:00
os.environ.setdefault("ESCDELAY", "25")
# Initialise the screen
curses.wrapper(start, buffer_name, buffer_list)
if __name__ == "__main__":
main()