🐞 Small fixes

This commit is contained in:
Maddie H 2022-04-17 13:32:35 +01:00
parent 6d81c1e8eb
commit 31bce0318c
4 changed files with 18 additions and 21 deletions

View File

@ -34,9 +34,9 @@ class Buffer:
instance.buffer.data[instance.cursor[0]][instance.cursor[1]:] instance.buffer.data[instance.cursor[0]][instance.cursor[1]:]
def open_file(file_name): def open_file(file_path):
# Open the file # Open the file
with open(file_name) as f: with open(file_path) as f:
# Convert it into a list of lines # Convert it into a list of lines
lines = f.readlines() lines = f.readlines()
@ -63,7 +63,7 @@ def load_file(file_path=None):
# Only if the file actually exists # Only if the file actually exists
if os.path.exists(file_path): if os.path.exists(file_path):
# Open the file as a list of lines # Open the file as a list of lines
file_data = open_file(file_name) file_data = open_file(file_path)
# Return a dictionary which will become all the data about the buffer # Return a dictionary which will become all the data about the buffer
return Buffer(file_path, file_name, file_data) return Buffer(file_path, file_name, file_data)

View File

@ -6,6 +6,7 @@ import traceback
from pathlib import Path from pathlib import Path
from core.colors import Codes as Col from core.colors import Codes as Col
from core import cursors
def gracefully_exit(): def gracefully_exit():
@ -29,15 +30,15 @@ def pause(message: str):
input(f"{message}\n\n Press enter to continue...") input(f"{message}\n\n Press enter to continue...")
def load_file(file: str) -> dict: def load_file(file_path: 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_path, "r") as f:
return json.load(f) return json.load(f)
def save_file(instance, file: str, data: list): def save_file(instance, file_path: str, data: list):
# Save the data to the file # Save the data to the file
with open(file, "w") as f: with open(file_path, "w") as f:
try: try:
for index, line in enumerate(data): for index, line in enumerate(data):
if index == len(data) - 1: if index == len(data) - 1:
@ -46,17 +47,17 @@ def save_file(instance, file: str, data: list):
f.write(f"{line}\n") f.write(f"{line}\n")
except Exception: except Exception:
error(instance, f"File {file} could not be saved.") error(instance, f"File {file_path} 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_path = 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_path):
# Return the loaded config # Return the loaded config
return load_file(config_file) return load_file(config_file_path)
def welcome(screen): def welcome(screen):
@ -125,13 +126,10 @@ def prompt(instance, message: str, color: int = 1) -> (list, None):
else: else:
# If any other key is typed, append it # If any other key is typed, append it
# As long as the key is in the valid list # As long as the key is in the valid list
valid = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ!" valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!/_-0123456789 "
if chr(key) in valid and len(inp) < (instance.width - 2): if chr(key) in valid and len(inp) < (instance.width - 2):
inp.append(chr(key)) inp.append(chr(key))
# Refresh the screen
instance.screen.refresh()
# Refresh the screen # Refresh the screen
instance.refresh() instance.refresh()
@ -147,7 +145,7 @@ def prompt(instance, message: str, color: int = 1) -> (list, None):
def press_key_to_continue(instance, message: str, color: int = None): def press_key_to_continue(instance, message: str, color: int = None):
# Hide the cursor # Hide the cursor
curses.curs_set(0) cursors.mode("hidden")
# Clear the bottom of the screen # Clear the bottom of the screen
clear(instance, instance.height - 1, 0) clear(instance, instance.height - 1, 0)
@ -163,7 +161,7 @@ def press_key_to_continue(instance, message: str, color: int = None):
clear(instance, instance.height - 1, 0) clear(instance, instance.height - 1, 0)
# Show the cursor # Show the cursor
curses.curs_set(1) cursors.mode("visible")
def error(instance, message: str): def error(instance, message: str):

1
mode/TODO.md Normal file
View File

@ -0,0 +1 @@
## TODO

View File

@ -1,6 +1,4 @@
import curses from core import cursors, modes
from core import cursors, modes, utils
def execute(instance, key): def execute(instance, key):
@ -8,7 +6,7 @@ def execute(instance, key):
# Switch to normal mode # Switch to normal mode
modes.activate(instance, "normal") modes.activate(instance, "normal")
elif key == 127: # Backspace elif key in (curses.KEY_BACKSPACE, 127, '\b'): # Backspace
if instance.cursor[1] > 0: if instance.cursor[1] > 0:
# Delete the character before the cursor # Delete the character before the cursor
instance.buffer.remove_char(instance) instance.buffer.remove_char(instance)