starting work on actually saving settings
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentSwitcher Vertical {
|
#sidebar-switcher Vertical {
|
||||||
padding: 1;
|
padding: 1;
|
||||||
Static {
|
Static {
|
||||||
margin-bottom: 1;
|
margin-bottom: 1;
|
||||||
|
|||||||
@@ -14,5 +14,4 @@ theme_mappings = {
|
|||||||
"yaml": "yaml",
|
"yaml": "yaml",
|
||||||
"md": "markdown",
|
"md": "markdown",
|
||||||
"gitignore": "markdown",
|
"gitignore": "markdown",
|
||||||
"lua": "lua"
|
|
||||||
}
|
}
|
||||||
22
main.py
22
main.py
@@ -1,4 +1,4 @@
|
|||||||
from textual.app import App, ComposeResult
|
from textual.app import App, ComposeResult, SystemCommand
|
||||||
from textual.widgets import Header, Footer, ContentSwitcher, DirectoryTree, Static, Button, TextArea, Tabs, Tab, RichLog, Input
|
from textual.widgets import Header, Footer, ContentSwitcher, DirectoryTree, Static, Button, TextArea, Tabs, Tab, RichLog, Input
|
||||||
from textual.containers import HorizontalGroup, Vertical
|
from textual.containers import HorizontalGroup, Vertical
|
||||||
from textual.binding import Binding
|
from textual.binding import Binding
|
||||||
@@ -36,7 +36,8 @@ class Berry(App):
|
|||||||
Binding("ctrl+n", "new", "New File"),
|
Binding("ctrl+n", "new", "New File"),
|
||||||
Binding("ctrl+s", "save", "Save"),
|
Binding("ctrl+s", "save", "Save"),
|
||||||
Binding("ctrl+shift+s", "save_as", "Save As...", priority=True),
|
Binding("ctrl+shift+s", "save_as", "Save As...", priority=True),
|
||||||
Binding("ctrl+f", "find", "Find", priority=True)
|
Binding("ctrl+f", "find", "Find", priority=True),
|
||||||
|
Binding("ctrl+f1", "settings", "Settings")
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, path: str):
|
def __init__(self, path: str):
|
||||||
@@ -50,7 +51,7 @@ class Berry(App):
|
|||||||
yield Button("📂")
|
yield Button("📂")
|
||||||
yield Button("🔍")
|
yield Button("🔍")
|
||||||
|
|
||||||
with ContentSwitcher(initial="files"):
|
with ContentSwitcher(initial="files", id="sidebar-switcher"):
|
||||||
with Vertical(id="files"):
|
with Vertical(id="files"):
|
||||||
yield Static("EXPLORER")
|
yield Static("EXPLORER")
|
||||||
yield DirectoryTree(self.path, id="directory")
|
yield DirectoryTree(self.path, id="directory")
|
||||||
@@ -63,7 +64,7 @@ class Berry(App):
|
|||||||
first_tab,
|
first_tab,
|
||||||
id="file-tabs"
|
id="file-tabs"
|
||||||
)
|
)
|
||||||
yield TextArea.code_editor(placeholder="This file is empty.", theme="css", id="code-editor", disabled=True)
|
yield TextArea.code_editor(placeholder="This file is empty.", theme="css", id="code-editor", disabled=True, soft_wrap=True)
|
||||||
|
|
||||||
#if os.name == "nt":
|
#if os.name == "nt":
|
||||||
with Vertical(id="console-container"):
|
with Vertical(id="console-container"):
|
||||||
@@ -75,6 +76,13 @@ class Berry(App):
|
|||||||
yield Footer()
|
yield Footer()
|
||||||
yield PluginLoader()
|
yield PluginLoader()
|
||||||
|
|
||||||
|
def action_settings(self):
|
||||||
|
self.push_screen(SettingsScreen())
|
||||||
|
|
||||||
|
def get_system_commands(self, screen):
|
||||||
|
yield from super().get_system_commands(screen)
|
||||||
|
yield SystemCommand("Settings", "Open the settings menu", self.action_settings)
|
||||||
|
|
||||||
def on_input_submitted(self, event: Input.Submitted):
|
def on_input_submitted(self, event: Input.Submitted):
|
||||||
if event.input.id != "console-input":
|
if event.input.id != "console-input":
|
||||||
return
|
return
|
||||||
@@ -301,8 +309,8 @@ class Berry(App):
|
|||||||
def done_saving(self, result):
|
def done_saving(self, result):
|
||||||
if result is None: return
|
if result is None: return
|
||||||
|
|
||||||
with open(result, "wb") as f:
|
with open(result, "w", encoding="utf-8") as f:
|
||||||
f.write(self.query_one("#code-editor").text.encode())
|
f.write(self.query_one("#code-editor").text)
|
||||||
|
|
||||||
tabs: Tabs = self.query_one("#file-tabs")
|
tabs: Tabs = self.query_one("#file-tabs")
|
||||||
tabs.active_tab.label = os.path.basename(result)
|
tabs.active_tab.label = os.path.basename(result)
|
||||||
@@ -350,8 +358,6 @@ class Berry(App):
|
|||||||
#else:
|
#else:
|
||||||
# self.query_one("#terminal").start()
|
# self.query_one("#terminal").start()
|
||||||
|
|
||||||
self.push_screen(SettingsScreen())
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = Berry("./")
|
app = Berry("./")
|
||||||
app.run()
|
app.run()
|
||||||
@@ -51,8 +51,8 @@ class PluginLoader(Window):
|
|||||||
def create_widget(self, widget_type: str, *args):
|
def create_widget(self, widget_type: str, *args):
|
||||||
return getattr(textual.widgets, widget_type)(*args)
|
return getattr(textual.widgets, widget_type)(*args)
|
||||||
|
|
||||||
def run_on_event(self, event, function):
|
def run_on_message(self, event, function):
|
||||||
raise NotImplementedError("runOnEvent is not implemented yet.")
|
raise NotImplementedError("onMessage is not implemented yet.")
|
||||||
|
|
||||||
def create_window(self, title: str, icon: str = "", show_title: bool = True, can_close: bool = True, can_maximize: bool = True, can_resize: bool = True, start_horizontal: str = "center", start_vertical: str = "middle"):
|
def create_window(self, title: str, icon: str = "", show_title: bool = True, can_close: bool = True, can_maximize: bool = True, can_resize: bool = True, start_horizontal: str = "center", start_vertical: str = "middle"):
|
||||||
new_window = Window(
|
new_window = Window(
|
||||||
@@ -91,7 +91,7 @@ class PluginLoader(Window):
|
|||||||
"createWindow": self.create_window,
|
"createWindow": self.create_window,
|
||||||
"createWidget": self.create_widget,
|
"createWidget": self.create_widget,
|
||||||
"app": self.app,
|
"app": self.app,
|
||||||
"runOnEvent": self.run_on_event
|
"onMessage": self.run_on_message
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
local plugin = {}
|
|
||||||
|
|
||||||
return plugin
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "0.0.1",
|
|
||||||
"name": "Test Plugin",
|
|
||||||
"author": "SpookyDervish",
|
|
||||||
"dependencies": []
|
|
||||||
}
|
|
||||||
21
settings.py
21
settings.py
@@ -1,5 +1,5 @@
|
|||||||
from textual.screen import ModalScreen
|
from textual.screen import ModalScreen
|
||||||
from textual.widgets import Label, Select, TabbedContent, TabPane, Checkbox
|
from textual.widgets import Label, Select, TabbedContent, TabPane, Switch
|
||||||
from textual.containers import Vertical, HorizontalGroup, VerticalGroup
|
from textual.containers import Vertical, HorizontalGroup, VerticalGroup
|
||||||
from textual.binding import Binding
|
from textual.binding import Binding
|
||||||
|
|
||||||
@@ -24,23 +24,25 @@ class SettingsScreen(ModalScreen):
|
|||||||
|
|
||||||
.setting {
|
.setting {
|
||||||
padding: 0 2;
|
padding: 0 2;
|
||||||
background: red;
|
content-align: center middle;
|
||||||
|
|
||||||
.setting-name {
|
.setting-name {
|
||||||
text-style: bold;
|
text-style: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
.setting-desc {
|
.setting-desc {
|
||||||
text-style: dim;
|
text-style: dim;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
min-width: 30;
|
min-width: 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
VerticalGroup {
|
VerticalGroup {
|
||||||
width: 50%;
|
width: 50%;
|
||||||
min-width: 20;
|
min-width: 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
Select {
|
Select {
|
||||||
width: 25;
|
max-width: 30;
|
||||||
padding: 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,11 +64,16 @@ class SettingsScreen(ModalScreen):
|
|||||||
with VerticalGroup():
|
with VerticalGroup():
|
||||||
yield Label("Colour Theme", classes="setting-name")
|
yield Label("Colour Theme", classes="setting-name")
|
||||||
yield Label("Colour theme used for the entire Berry app.", classes="setting-desc")
|
yield Label("Colour theme used for the entire Berry app.", classes="setting-desc")
|
||||||
yield Select.from_values(["theme 1", "theme 2", "theme 3"], allow_blank=False)
|
yield Select.from_values((theme_name for theme_name in self.app._registered_themes.keys() if theme_name != "textual-ansi"), allow_blank=False)
|
||||||
with TabPane("Editor"):
|
with TabPane("Editor"):
|
||||||
with HorizontalGroup(classes="setting"):
|
with HorizontalGroup(classes="setting"):
|
||||||
with VerticalGroup():
|
with VerticalGroup():
|
||||||
yield Label("Word Wrap", classes="setting-name")
|
yield Label("Word Wrap", classes="setting-name")
|
||||||
yield Label("Enable word wrap in the code editor.", classes="setting-desc")
|
yield Label("Enable word wrap in the code editor.", classes="setting-desc")
|
||||||
yield Checkbox(value=True)
|
yield Switch(value=True)
|
||||||
yield TabPane("Plugins")
|
with TabPane("Plugins"):
|
||||||
|
with HorizontalGroup(classes="setting"):
|
||||||
|
with VerticalGroup():
|
||||||
|
yield Label("Plugins Enabled", classes="setting-name")
|
||||||
|
yield Label("Enable or disable Lua plugins. This requires a restart.", classes="setting-desc")
|
||||||
|
yield Switch(value=True)
|
||||||
|
|||||||
40
settings_store.py
Normal file
40
settings_store.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
import os, subprocess
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigHandler:
|
||||||
|
def __init__(self):
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
|
||||||
|
self.config_dir = self.ensure_hidden_config_dir()
|
||||||
|
self.load_settings(self.config_dir)
|
||||||
|
|
||||||
|
def ensure_hidden_config_dir(self):
|
||||||
|
config_dir = Path.home() / ".berry"
|
||||||
|
config_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# If Windows, apply hidden attribute
|
||||||
|
if os.name == "nt":
|
||||||
|
subprocess.run(["attrib", "+h", str(config_dir)], shell=True)
|
||||||
|
|
||||||
|
return config_dir
|
||||||
|
|
||||||
|
def load_settings(self):
|
||||||
|
if os.path.isfile(self.config_dir / "config.ini"):
|
||||||
|
with open(self.config_dir / "config.ini", "r") as configfile:
|
||||||
|
self.config.read(configfile)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.config["editor"] = {
|
||||||
|
"word_wrap": True
|
||||||
|
}
|
||||||
|
self.config["plugins"] = {
|
||||||
|
"enabled": True
|
||||||
|
}
|
||||||
|
self.config["appearance"] = {
|
||||||
|
"colour_theme": "textual-dark"
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(self.config_dir / "config.ini") as configfile:
|
||||||
|
self.config.write(configfile)
|
||||||
Reference in New Issue
Block a user