✏️ File editing & writing & small changes

This commit is contained in:
Maddie H 2022-04-17 12:54:37 +01:00
parent 9fa83dd52b
commit 6d81c1e8eb
5 changed files with 54 additions and 20 deletions

View File

@ -21,14 +21,17 @@ class Buffer:
instance.components.components["left"]) + len(line), " " * (instance.safe_width - len(line))) instance.components.components["left"]) + len(line), " " * (instance.safe_width - len(line)))
@staticmethod @staticmethod
def remove_char(string: str, index: int) -> str: def remove_char(instance):
# Remove a character from a string at a given index # Remove a character from a string at a given index
return string[:index] + string[index + 1:] instance.buffer.data[instance.cursor[0]] = instance.buffer.data[instance.cursor[0]][:(instance.cursor[1] - 1)] \
+ instance.buffer.data[instance.cursor[0]][(instance.cursor[1] - 1) + 1:]
@staticmethod @staticmethod
def insert_char(string: str, index: int, char: (str, chr)) -> str: def insert_char(instance, char: (str, chr)):
# Insert a character into a string at a given index # Insert a character into a string at a given index
return string[:index] + char + string[index:] instance.buffer.data[instance.cursor[0]] = instance.buffer.data[instance.cursor[0]][:instance.cursor[1]] + \
char + \
instance.buffer.data[instance.cursor[0]][instance.cursor[1]:]
def open_file(file_name): def open_file(file_name):

View File

@ -8,11 +8,6 @@ from pathlib import Path
from core.colors import Codes as Col from core.colors import Codes as Col
def clear(instance, y: int, x: int):
# Clear the line at the screen at position y, x
instance.screen.insstr(y, x, " " * (instance.width - x))
def gracefully_exit(): def gracefully_exit():
# Close the curses window # Close the curses window
curses.endwin() curses.endwin()
@ -21,6 +16,11 @@ def gracefully_exit():
sys.exit() sys.exit()
def clear(instance, y: int, x: int):
# Clear the line at the screen at position y, x
instance.screen.insstr(y, x, " " * (instance.width - x))
def pause(message: str): def pause(message: str):
# End the curses session # End the curses session
curses.endwin() curses.endwin()
@ -29,20 +29,34 @@ def pause(message: str):
input(f"{message}\n\n Press enter to continue...") input(f"{message}\n\n Press enter to continue...")
def load_json(file: str) -> dict: def load_file(file: str) -> dict:
# Load the json file with read permissions # load the json file with read permissions
with open(file, "r") as f: with open(file, "r") as f:
return json.load(f) return json.load(f)
def save_file(instance, file: str, data: list):
# Save the data to the file
with open(file, "w") as f:
try:
for index, line in enumerate(data):
if index == len(data) - 1:
f.write(line)
else:
f.write(f"{line}\n")
except Exception:
error(instance, f"File {file} could not be saved.")
def load_config() -> dict: def load_config() -> dict:
# Parse the path of the config file # Parse the path of the config file
config_file = f"{Path.home()}/.config/lambda/config.json" config_file = f"{Path.home()}/.config/lambda/config.json"
# Only if the config file exists, attempt to load it # Only if the config file exists, attempt to load it
if os.path.exists(config_file): if os.path.exists(config_file):
# Return the loaded config as a dictionary # Return the loaded config
return load_json(config_file) return load_file(config_file)
def welcome(screen): def welcome(screen):

View File

@ -9,10 +9,6 @@ from core.components import Components
class Lambda: class Lambda:
def __init__(self, buffer: Buffer = None, config: dict = None): def __init__(self, buffer: Buffer = None, config: dict = None):
self.screen = curses.initscr()
self.config = config or {"icon": "λ"}
self.buffer = buffer or [""]
self.mode = "normal"
self.cursor = [0, 0] self.cursor = [0, 0]
self.raw_cursor = [0, 0] self.raw_cursor = [0, 0]
self.offset = [0, 0] self.offset = [0, 0]
@ -20,6 +16,10 @@ class Lambda:
self.width = 0 self.width = 0
self.safe_height = 0 self.safe_height = 0
self.safe_width = 0 self.safe_width = 0
self.mode = "normal"
self.config = config or {"icon": "λ"}
self.buffer = buffer or [""]
self.screen = curses.initscr()
self.components = Components(self) self.components = Components(self)
def update_dimensions(self): def update_dimensions(self):

View File

@ -20,7 +20,7 @@ def execute(instance, commands: list):
elif command == "w": # Write elif command == "w": # Write
# Write to the file # Write to the file
pass utils.save_file(instance, instance.buffer.path, instance.buffer.data)
elif command == "q": # Quit elif command == "q": # Quit
# Create a goodbye prompt # Create a goodbye prompt

View File

@ -1,11 +1,28 @@
from core import cursors, modes import curses
from core import cursors, modes, utils
def execute(instance, key): def execute(instance, key):
if key == 27: # Enter if key == 27: # Escape
# Switch to normal mode # Switch to normal mode
modes.activate(instance, "normal") modes.activate(instance, "normal")
elif key == 127: # Backspace
if instance.cursor[1] > 0:
# Delete the character before the cursor
instance.buffer.remove_char(instance)
# Move the cursor one to the left
cursors.push(instance, 3)
else:
# Insert the character
instance.buffer.insert_char(instance, chr(key))
# Move the cursor one to the right
cursors.push(instance, 1)
def activate(): def activate():
# Switch the cursor to a line # Switch the cursor to a line