From 208e66d9fce16324cb6bfe4fa253f65191dffd88 Mon Sep 17 00:00:00 2001 From: spy Date: Thu, 17 Mar 2022 18:46:04 +0000 Subject: [PATCH] cursor follows buffer --- core/__pycache__/cursor.cpython-310.pyc | Bin 818 -> 934 bytes core/__pycache__/files.cpython-310.pyc | Bin 311 -> 311 bytes core/__pycache__/welcome.cpython-310.pyc | Bin 0 -> 768 bytes core/cursor.py | 6 +++ core/welcome.py | 29 ++++++++++++++ lambda.py | 48 +++++++---------------- 6 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 core/__pycache__/welcome.cpython-310.pyc create mode 100644 core/welcome.py diff --git a/core/__pycache__/cursor.cpython-310.pyc b/core/__pycache__/cursor.cpython-310.pyc index e726a47e009dcf09e749e73ebdb70eca9ff79403..751bcf6fe19647918f1ec676ae82ccd171b7808b 100644 GIT binary patch delta 429 zcmYjNIZnes6#X;fjYJ8O6C&F}P`f}BP*9{pv}x#QBE@#Fg^8t^35ygdLb*YF2YM(t z36*c)TkyXP2_wy4-tuSpwBGH0(d%^p*Z14SY%4zcIa{09UgK6@7*-H7A2em|!-hR@ zIUvjgA7XI@0>-ci5Ha?u*3+mBdG_bsqof{#X0h4&HvAW%Y^`aqM+f4_9mQfB5xh!+ zFtn4{LG3}K{l~1}7Q)V?a-yYFg`B=t-Pv+JPvkVul&%@RYRA3QoLciai*;IK3Dv+# zlk7g#wZJYEmB^DtdCD(67yBV|*)jK+%LA+eW^j2_M*QZ^K~i?ffe5C`cr2chkXM${ nv5KS|l7POp4Hi`+3@5mifx+RPXpl)Lq)jxL;yd+Q^r~SkD6FIscNMWpDNMVv>Xl4S@HVh>o zKC>i43X22-nhZk%lAB+as>yVVt+*tmIJu}u2&8}&POyPkB0xd{h>JnK=U|dx28;M< UG8eIfq(SBsiA*kI)@S4d0RK`xMgRZ+ diff --git a/core/__pycache__/files.cpython-310.pyc b/core/__pycache__/files.cpython-310.pyc index b9961936b67e87400940e06c326b73d6fee37e81..b7fd14a5079bfb53d8a1ee5b6b82817117add6ac 100644 GIT binary patch delta 20 acmdnaw4I4NpO=@50SGJ;jW%-YFaiK9n*+uG delta 20 acmdnaw4I4NpO=@50SFF#H`vIn!w3K~)CA)I diff --git a/core/__pycache__/welcome.cpython-310.pyc b/core/__pycache__/welcome.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..600f4df1a573b05102684e90c5cd3acd348c4715 GIT binary patch literal 768 zcmYjP&59F25bmCyzbpwXDvO9 zh~Wf{5VIKhhDMm-_o!`e071hLn{Brmu=^l=5cv~SiLTKaeY7fESqxWX3LM{YWh-0) z*8`-il3Zb9-M_Vuz`nwj135LqNbS48MM}4HN}}v*huLea?9c8gTH6?v_5gi)RC%S# zoENBa_xQa%{@UL0`#V0r^wtEU`E!O~DtB0zA%6d&=RrDQLEBk%hJ2yulxI8-R4g(Y z1>u)ql5naFz*($BPA@(81MpeY=yR(zQo3l>JC=+3k@=V?2 z3EI1e6aMNpJ)p*?LB?wK)#EMSoI;IW|7lof&Wl*lq;9e^TW-vQZ9JpQgS0*EPq|8i z#d4uZoGDEbo@qB=OsZVFVUbHN^+6~Sk&kCV46&2(>D%)`yP;jFm<%Bj@pu|3?Oeu8 zMcPrZO8Cf=#l#FgYDpF3YP=ZV#A|n{?HcuV=ns_NfrkDbcO#MVuAD8qjDPKJ8}Ei9 z=iN)5gn+>my16|C)j7{>I46%lH7%R?__%S19}?eK_z>Iwa`QFYR&G9WZP$hN)Lj6y S-%AB6;C9WmNJHtPCixHK&&Zts literal 0 HcmV?d00001 diff --git a/core/cursor.py b/core/cursor.py index c0b6069..09dc967 100644 --- a/core/cursor.py +++ b/core/cursor.py @@ -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"]) diff --git a/core/welcome.py b/core/welcome.py new file mode 100644 index 0000000..e12ae98 --- /dev/null +++ b/core/welcome.py @@ -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 to open a file and edit", + "Type :q or 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) diff --git a/lambda.py b/lambda.py index e6a4a91..3e1f1ed 100755 --- a/lambda.py +++ b/lambda.py @@ -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 to open a file and edit", - "Type :q or 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