✏️ File editing & writing & small changes
This commit is contained in:
parent
9fa83dd52b
commit
6d81c1e8eb
@ -21,14 +21,17 @@ class Buffer:
|
||||
instance.components.components["left"]) + len(line), " " * (instance.safe_width - len(line)))
|
||||
|
||||
@staticmethod
|
||||
def remove_char(string: str, index: int) -> str:
|
||||
def remove_char(instance):
|
||||
# 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
|
||||
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
|
||||
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):
|
||||
|
@ -8,11 +8,6 @@ from pathlib import Path
|
||||
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():
|
||||
# Close the curses window
|
||||
curses.endwin()
|
||||
@ -21,6 +16,11 @@ def gracefully_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):
|
||||
# End the curses session
|
||||
curses.endwin()
|
||||
@ -29,20 +29,34 @@ def pause(message: str):
|
||||
input(f"{message}\n\n Press enter to continue...")
|
||||
|
||||
|
||||
def load_json(file: str) -> dict:
|
||||
# Load the json file with read permissions
|
||||
def load_file(file: str) -> dict:
|
||||
# load the json file with read permissions
|
||||
with open(file, "r") as 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:
|
||||
# Parse the path of the config file
|
||||
config_file = f"{Path.home()}/.config/lambda/config.json"
|
||||
|
||||
# Only if the config file exists, attempt to load it
|
||||
if os.path.exists(config_file):
|
||||
# Return the loaded config as a dictionary
|
||||
return load_json(config_file)
|
||||
# Return the loaded config
|
||||
return load_file(config_file)
|
||||
|
||||
|
||||
def welcome(screen):
|
||||
|
8
main.py
8
main.py
@ -9,10 +9,6 @@ from core.components import Components
|
||||
|
||||
class Lambda:
|
||||
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.raw_cursor = [0, 0]
|
||||
self.offset = [0, 0]
|
||||
@ -20,6 +16,10 @@ class Lambda:
|
||||
self.width = 0
|
||||
self.safe_height = 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)
|
||||
|
||||
def update_dimensions(self):
|
||||
|
@ -20,7 +20,7 @@ def execute(instance, commands: list):
|
||||
|
||||
elif command == "w": # Write
|
||||
# Write to the file
|
||||
pass
|
||||
utils.save_file(instance, instance.buffer.path, instance.buffer.data)
|
||||
|
||||
elif command == "q": # Quit
|
||||
# Create a goodbye prompt
|
||||
|
@ -1,11 +1,28 @@
|
||||
from core import cursors, modes
|
||||
import curses
|
||||
|
||||
from core import cursors, modes, utils
|
||||
|
||||
|
||||
def execute(instance, key):
|
||||
if key == 27: # Enter
|
||||
if key == 27: # Escape
|
||||
# Switch to normal mode
|
||||
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():
|
||||
# Switch the cursor to a line
|
||||
|
Loading…
Reference in New Issue
Block a user