forked from espressif/esp32-arduino-lib-builder
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy patheditor.py
executable file
·86 lines (77 loc) · 3.19 KB
/
editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from textual import on
from textual.app import ComposeResult
from textual.binding import Binding
from textual.containers import Container, VerticalScroll, Horizontal
from textual.screen import Screen
from textual.events import ScreenResume
from textual.widgets import DirectoryTree, Header, TextArea, Button, Footer
class EditorScreen(Screen):
# Configuration file editor screen
# Set the key bindings
BINDINGS = [
Binding("ctrl+s", "save", "Save", priority=True),
Binding("escape", "app.pop_screen", "Discard")
]
# Current file being edited
current_file = ""
def action_save(self) -> None:
code_view = self.query_one("#code", TextArea)
current_text = code_view.text
try:
file = open(self.curent_file, "w")
file.write(current_text)
file.close()
except Exception:
print("Error saving file: " + self.curent_file)
self.sub_title = "ERROR"
else:
print("File saved: " + self.curent_file)
self.sub_title = self.curent_file
self.dismiss()
def on_button_pressed(self, event: Button.Pressed) -> None:
# Event handler called when a button is pressed
if event.button.id == "save-editor-button" and self.curent_file != "":
print("Save button pressed. Trying to save file: " + self.curent_file)
self.action_save()
elif event.button.id == "cancel-editor-button":
print("Cancel button pressed")
self.dismiss()
def on_directory_tree_file_selected(self, event: DirectoryTree.FileSelected) -> None:
# Called when the user click a file in the directory tree
event.stop()
code_view = self.query_one("#code", TextArea)
code_view.clear()
self.curent_file = str(event.path)
try:
print("Opening file: " + self.curent_file)
file = open(self.curent_file, "r")
file_content = file.read()
file.close()
except Exception:
print("Error opening file: " + self.curent_file)
self.sub_title = "ERROR"
else:
print("File opened: " + self.curent_file)
code_view.insert(file_content)
self.sub_title = self.curent_file
@on(ScreenResume)
def on_resume(self) -> None:
# Event handler called every time the screen is activated
print("Editor screen resumed. Clearing code view")
self.sub_title = "Select a file"
self.query_one(DirectoryTree).focus()
self.query_one(TextArea).clear()
self.curent_file = ""
def compose(self) -> ComposeResult:
# Compose editor screen
path = os.path.join(self.app.ROOT_PATH, 'configs')
yield Header()
with Container():
yield DirectoryTree(path, id="tree-view")
with VerticalScroll(id="code-view"):
yield TextArea.code_editor("", id="code")
with Horizontal(id="editor-buttons-container"):
yield Button("Save", id="save-editor-button", classes="editor-button")
yield Button("Cancel", id="cancel-editor-button", classes="editor-button")
yield Footer()