Skip to content

Commit

Permalink
supernano.py
Browse files Browse the repository at this point in the history
Signed-off-by: LcferShell <71859305+LcfherShell@users.noreply.github.com>
  • Loading branch information
LcfherShell authored Aug 31, 2024
1 parent bf961bd commit 7d47192
Showing 1 changed file with 86 additions and 25 deletions.
111 changes: 86 additions & 25 deletions supernano.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from libs.filemanager import (
StreamFile,
ModuleInspector,
validate_file,
create_file_or_folder,
resolve_relative_path_v2,
resolve_relative_path,
Expand All @@ -29,6 +30,7 @@
from .filemanager import (
StreamFile,
ModuleInspector,
validate_file,
create_file_or_folder,
resolve_relative_path_v2,
resolve_relative_path,
Expand All @@ -44,6 +46,7 @@
from filemanager import (
StreamFile,
ModuleInspector,
validate_file,
create_file_or_folder,
resolve_relative_path_v2,
resolve_relative_path,
Expand Down Expand Up @@ -147,6 +150,52 @@ class PlainButton(urwid.Button):
button_right = urwid.Text("")


class NumberedEdit(urwid.WidgetWrap):
def __init__(self, edit_text="", multiline=True):
self.edit = urwid.Edit(edit_text, multiline=multiline)
self.line_numbers = urwid.Text("")
self.update_line_numbers()

columns = urwid.Columns([("fixed", 4, self.line_numbers), self.edit])
super().__init__(urwid.AttrMap(columns, None, focus_map="reversed"))

# Connect the 'change' signal from the internal Edit widget
urwid.connect_signal(self.edit, "change", self._on_change)

def update_line_numbers(self):
text = self.edit.get_edit_text()
lines = text.splitlines()
line_numbers = "\n".join([f"{i+1:>3}" for i in range(len(lines))])
self.line_numbers.set_text(line_numbers)

def keypress(self, size, key):
key = super().keypress(size, key)
self.update_line_numbers()
return key

def mouse_event(self, size, event, button, col, row, focus):
handled = super().mouse_event(size, event, button, col, row, focus)
if handled:
self.update_line_numbers()
return handled

def _on_change(self, edit, new_text):
self.update_line_numbers()
urwid.emit_signal(self, "change", self, new_text)

def set_edit_text(self, text):
"""Set the text of the edit widget and update line numbers."""
self.edit.set_edit_text(text)
self.update_line_numbers()

def get_edit_text(self):
"""Get the text from the edit widget."""
return self.edit.get_edit_text()


urwid.register_signal(NumberedEdit, ["change"])


class SuperNano:
"""
Kelas SuperNano yang sedang Anda kembangkan adalah text editor berbasis console yang menggunakan Python 3.6 ke atas dengan dukungan urwid[curses].
Expand Down Expand Up @@ -268,11 +317,11 @@ def setup_main_menu(self):
# Define widgets
self.file_list = urwid.SimpleFocusListWalker(self.get_file_list())
self.file_list_box = urwid.ListBox(self.file_list)
self.text_editor = urwid.Edit(multiline=True)
self.text_editor = NumberedEdit(multiline=True)
self.current_focus = 0 # 0 for textbox1, 1 for textbox2
# Wrap text_editor with BoxAdapter for scrollable content
self.text_editor_scrollable = urwid.LineBox(
urwid.Filler(self.text_editor, valign="top")
urwid.Filler(self.text_editor, valign="top"), title="TextBox"
)

# Define menu widgets
Expand Down Expand Up @@ -353,9 +402,7 @@ def setup_main_menu(self):
(
"weight",
3,
urwid.LineBox(
urwid.Pile([self.text_editor_scrollable]), title="TextBox"
),
self.text_editor_scrollable,
),
]
),
Expand Down Expand Up @@ -389,19 +436,25 @@ def inspect_module(self, button, module_name):
if result["classes"]:
result_text += "\n\nClass:\n"
for cls in result["classes"]:
if cls['name']:
if cls["name"]:
result_text += f"Class: {cls['name']}\n"
result_text += " Variables:\n"
result_text += " " + "\n > ".join(cls["variables"]) + "\n\n"
result_text += (
" " + "\n > ".join(cls["variables"]) + "\n\n"
)
if cls["functions"]:
result_text += " Function:\n"
for func in cls["functions"]:
result_text += f" > {func['name']}{func['params']}\n\n"
result_text += (
f" > {func['name']}{func['params']}\n\n"
)
for funcs in result["functions"]:
if funcs['name']:
if funcs["name"]:
result_text += f"\nFunction: {funcs['name']}\n"
result_text += f" > {funcs['name']}{funcs['params']}\n\n"
self.Text_Deinspect_modules_from_package_Python.set_text(result_text)
self.Text_Deinspect_modules_from_package_Python.set_text(
result_text
)
else:
self.Text_Deinspect_modules_from_package_Python.set_text(
"Error inspecting module."
Expand Down Expand Up @@ -526,12 +579,13 @@ def handle_input(self, key):
self.switch_to_secondary_layout()
elif key in ("f1", "ctrl e", "ctrl E"):
self.current_focus = 1 if self.current_focus == 0 else 0
self.status_msg_footer_text.set_text(f"focus {self.current_focus}")

@complex_handle_errors(loggering=logging, nomessagesNormal=False)
def get_current_edit(self):
"Mengembalikan widget edit yang sedang difokuskan (text editor atau search edit)."
if self.current_focus == 0:
return self.text_editor.base_widget
return self.text_editor.edit.base_widget
elif self.current_focus == 1:
return self.search_edit.base_widget
return None
Expand Down Expand Up @@ -605,7 +659,9 @@ def open_file(self, button, file_name):
else:
self.status_msg_footer_text.set_text("Folder access denied!")
else:
if validate_folder(os.path.dirname(file_path)):
if validate_folder(os.path.dirname(file_path)) and validate_file(
file_path, os.path.getsize(file_path) or 20, 6
):
try:
with open(
file_path, "r+", encoding=sys.getfilesystemencoding()
Expand All @@ -622,20 +678,22 @@ def open_file(self, button, file_name):
if modulefile:
if modulefile != self.module_package_Python.curents:
self.listmodules_from_package_Python[
:
] = self.create_modules_menus(modulefile)
:
] = self.create_modules_menus(modulefile)
self.module_package_Python.curents = modulefile

else:
self.listmodules_from_package_Python[
:
] = self.create_modules_menus(self.module_package_PythonC)
:
] = self.create_modules_menus(self.module_package_PythonC)

self.current_file_name = file_name # Track the current file name
# if str(ext).lower() in ( ".pyx", ".pyz", ".py"):
# self.listmodules_from_package_Python[:] = self.modules_menus(self.current_path)
if self.module_package_Python.languages:
self.Inspect_modules_from_package_Python.body.set_title(self.module_package_Python.languages["languages"]+" Modules")
self.Inspect_modules_from_package_Python.body.set_title(
self.module_package_Python.languages["languages"] + " Modules"
)
self.main_layout.body.contents[2][0].set_title(file_name)

else:
Expand All @@ -651,13 +709,16 @@ def save_file(self):
"Menyimpan perubahan yang dilakukan pada file saat ini dan mengembalikan status keberhasilan."
if self.current_file_name:
file_path = os.path.join(self.current_path, self.current_file_name)
try:
with open(file_path, "w+", encoding=sys.getfilesystemencoding()) as f:
f.write(self.text_editor.get_edit_text())
except:
with open(file_path, "w+", encoding="latin-1") as f:
f.write(self.text_editor.get_edit_text())
self.status_msg_footer_text.set_text("File saved successfully!")
if os.path.isfile(file_path):
try:
with open(
file_path, "w+", encoding=sys.getfilesystemencoding()
) as f:
f.write(self.text_editor.get_edit_text())
except:
with open(file_path, "w+", encoding="latin-1") as f:
f.write(self.text_editor.get_edit_text())
self.status_msg_footer_text.set_text("File saved successfully!")
return True

@complex_handle_errors(loggering=logging, nomessagesNormal=False)
Expand Down

0 comments on commit 7d47192

Please sign in to comment.