Updated nano package
This commit is contained in:
parent
e95470f384
commit
ab7d379424
10
new.py
Normal file
10
new.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def main(message: str) -> None:
|
||||||
|
print(f"{message}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(input("What would you like to say? "))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
|||||||
#["nano", "packages.nano", ["nano"]]
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import curses
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.lexers import PythonLexer
|
from pygments.lexers import PythonLexer
|
||||||
from pygments.formatters import TerminalFormatter
|
from pygments.formatters import TerminalFormatter
|
||||||
@ -9,6 +8,7 @@ class TextEditor:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.lines = []
|
self.lines = []
|
||||||
self.file = ""
|
self.file = ""
|
||||||
|
self.cursor = (0, 0) # (line, column)
|
||||||
|
|
||||||
def load_file(self, filename):
|
def load_file(self, filename):
|
||||||
try:
|
try:
|
||||||
@ -23,43 +23,81 @@ class TextEditor:
|
|||||||
with open(filename, "w") as file:
|
with open(filename, "w") as file:
|
||||||
file.writelines(self.lines)
|
file.writelines(self.lines)
|
||||||
|
|
||||||
def edit(self):
|
def edit(self, stdscr):
|
||||||
|
self.stdscr = stdscr
|
||||||
|
curses.curs_set(1) # Show cursor
|
||||||
|
self.stdscr.clear()
|
||||||
|
self.display()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
os.system('cls' if os.name == 'nt' else 'clear')
|
key = self.stdscr.getch()
|
||||||
self.display()
|
|
||||||
|
|
||||||
action = input("\nCommands: [e]dit, [n]ewline, [s]ave, [q]uit: ").lower()
|
if key == curses.KEY_UP:
|
||||||
|
self.move_cursor(-1)
|
||||||
if action == "e":
|
elif key == curses.KEY_DOWN:
|
||||||
self.edit_lines()
|
self.move_cursor(1)
|
||||||
elif action == "n":
|
elif key == curses.KEY_LEFT:
|
||||||
|
self.move_cursor(0, -1)
|
||||||
|
elif key == curses.KEY_RIGHT:
|
||||||
|
self.move_cursor(0, 1)
|
||||||
|
elif key == ord('\n'):
|
||||||
self.newline()
|
self.newline()
|
||||||
elif action == "s":
|
elif key == curses.KEY_BACKSPACE or key == 127:
|
||||||
|
self.delete_character()
|
||||||
|
elif key == 19: # Ctrl + S
|
||||||
filename = input("Enter filename to save: ")
|
filename = input("Enter filename to save: ")
|
||||||
self.save_file(filename)
|
self.save_file(filename)
|
||||||
print(f"File '{filename}' saved.")
|
print(f"File '{filename}' saved.")
|
||||||
elif action == "q":
|
elif key == 17: # Ctrl + Q
|
||||||
print("Exiting the text editor.")
|
print("Exiting the text editor.")
|
||||||
break
|
break
|
||||||
else:
|
elif key >= 32 and key <= 126:
|
||||||
print("Invalid command.")
|
self.insert_character(chr(key))
|
||||||
|
elif key == 27: # Escape key
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.stdscr.clear()
|
||||||
|
self.display()
|
||||||
|
|
||||||
def display(self):
|
def display(self):
|
||||||
print("Editing: " + self.file)
|
self.stdscr.addstr(0, 0, "Editing: " + self.file)
|
||||||
for i, line in enumerate(self.lines):
|
for i, line in enumerate(self.lines):
|
||||||
highlighted_line = highlight(line, PythonLexer(), TerminalFormatter())
|
highlighted_line = highlight(line, PythonLexer(), TerminalFormatter())
|
||||||
print(f"{i + 1}: {highlighted_line}", end="")
|
if i == self.cursor[0]:
|
||||||
|
cursor_column = self.cursor[1]
|
||||||
|
self.stdscr.addstr(i + 1, 0, f"{i + 1}: {highlighted_line[:cursor_column]}{highlighted_line[cursor_column:]}")
|
||||||
|
else:
|
||||||
|
self.stdscr.addstr(i + 1, 0, f"{i + 1}: {highlighted_line}")
|
||||||
|
|
||||||
|
def move_cursor(self, line_change=0, column_change=0):
|
||||||
|
new_line = self.cursor[0] + line_change
|
||||||
|
new_column = self.cursor[1] + column_change
|
||||||
|
|
||||||
|
if 0 <= new_line < len(self.lines):
|
||||||
|
line_length = len(self.lines[new_line])
|
||||||
|
new_column = max(0, min(new_column, line_length))
|
||||||
|
self.cursor = (new_line, new_column)
|
||||||
|
|
||||||
def newline(self):
|
def newline(self):
|
||||||
self.lines.append("\n")
|
line, column = self.cursor
|
||||||
|
self.lines[line] = self.lines[line][:column] + '\n' + self.lines[line][column:]
|
||||||
|
self.move_cursor(1, 0)
|
||||||
|
|
||||||
def edit_lines(self):
|
def delete_character(self):
|
||||||
line_number = int(input("Enter line number to edit: ")) - 1
|
line, column = self.cursor
|
||||||
if 0 <= line_number < len(self.lines):
|
if column > 0:
|
||||||
new_line = input("Enter new text: ")
|
self.lines[line] = self.lines[line][:column - 1] + self.lines[line][column:]
|
||||||
self.lines[line_number] = new_line + "\n"
|
self.move_cursor(0, -1)
|
||||||
else:
|
elif line > 0:
|
||||||
print("Invalid line number.")
|
prev_line_len = len(self.lines[line - 1])
|
||||||
|
self.lines[line - 1] = self.lines[line - 1][:-1] + self.lines[line]
|
||||||
|
self.lines.pop(line)
|
||||||
|
self.move_cursor(-1, prev_line_len)
|
||||||
|
|
||||||
|
def insert_character(self, char):
|
||||||
|
line, column = self.cursor
|
||||||
|
self.lines[line] = self.lines[line][:column] + char + self.lines[line][column:]
|
||||||
|
self.move_cursor(0, 1)
|
||||||
|
|
||||||
|
|
||||||
def nano(args):
|
def nano(args):
|
||||||
@ -72,4 +110,4 @@ def nano(args):
|
|||||||
editor = TextEditor()
|
editor = TextEditor()
|
||||||
editor.file = filename
|
editor.file = filename
|
||||||
editor.load_file(filename)
|
editor.load_file(filename)
|
||||||
editor.edit()
|
curses.wrapper(editor.edit)
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pygments
|
||||||
|
requests
|
||||||
|
keyboard
|
Loading…
Reference in New Issue
Block a user