implementing more settings
This commit is contained in:
33
settings.py
33
settings.py
@@ -3,6 +3,10 @@ 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
|
||||||
|
|
||||||
|
from settings_store import ConfigHandler
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class SettingsScreen(ModalScreen):
|
class SettingsScreen(ModalScreen):
|
||||||
border_title = "Settings"
|
border_title = "Settings"
|
||||||
@@ -52,9 +56,24 @@ class SettingsScreen(ModalScreen):
|
|||||||
Binding("escape", "close", "Close")
|
Binding("escape", "close", "Close")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.config_handler = ConfigHandler()
|
||||||
|
|
||||||
def action_close(self):
|
def action_close(self):
|
||||||
self.dismiss()
|
self.dismiss()
|
||||||
|
|
||||||
|
def on_switch_changed(self, event: Switch.Changed):
|
||||||
|
if event.switch.id == "word-wrap":
|
||||||
|
self.app.query_one("#code-editor").soft_wrap = event.value
|
||||||
|
self.config_handler.set("editor", "word_wrap", str(int(event.value)))
|
||||||
|
self.notify(str(int(event.value)))
|
||||||
|
|
||||||
|
def on_select_changed(self, event: Select.Changed):
|
||||||
|
if event.select.id == "colour-theme":
|
||||||
|
self.app.theme = event.value
|
||||||
|
self.config_handler.set("appearance", "colour_theme", str(event.value))
|
||||||
|
|
||||||
def compose(self):
|
def compose(self):
|
||||||
with Vertical(id="window") as window:
|
with Vertical(id="window") as window:
|
||||||
window.border_title = "Settings"
|
window.border_title = "Settings"
|
||||||
@@ -63,14 +82,22 @@ class SettingsScreen(ModalScreen):
|
|||||||
with HorizontalGroup(classes="setting"):
|
with HorizontalGroup(classes="setting"):
|
||||||
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. You can get more themes with plugins!", classes="setting-desc")
|
||||||
yield Select.from_values((theme_name for theme_name in self.app._registered_themes.keys() if theme_name != "textual-ansi"), 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,
|
||||||
|
id="colour-theme",
|
||||||
|
value=self.app.theme
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
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 Switch(value=True)
|
yield Switch(value=bool(int(self.config_handler.get("editor", "word_wrap"))), id="word-wrap")
|
||||||
with TabPane("Plugins"):
|
with TabPane("Plugins"):
|
||||||
with HorizontalGroup(classes="setting"):
|
with HorizontalGroup(classes="setting"):
|
||||||
with VerticalGroup():
|
with VerticalGroup():
|
||||||
|
|||||||
@@ -1,40 +1,44 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os, subprocess
|
import os
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
class ConfigHandler:
|
class ConfigHandler:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = configparser.ConfigParser()
|
self.config: configparser.ConfigParser = configparser.ConfigParser()
|
||||||
|
|
||||||
self.config_dir = self.ensure_hidden_config_dir()
|
self.config_dir: str = self.ensure_hidden_config_dir()
|
||||||
self.load_settings(self.config_dir)
|
self.load_settings()
|
||||||
|
|
||||||
def ensure_hidden_config_dir(self):
|
def ensure_hidden_config_dir(self):
|
||||||
config_dir = Path.home() / ".berry"
|
config_dir = Path.home() / ".berry"
|
||||||
config_dir.mkdir(parents=True, exist_ok=True)
|
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
|
return config_dir
|
||||||
|
|
||||||
|
def get(self, section: str, option: str):
|
||||||
|
return self.config.get(section, option)
|
||||||
|
|
||||||
|
def set(self, section: str, option: str, new_value: str):
|
||||||
|
return self.config.set(section, option, new_value)
|
||||||
|
|
||||||
def load_settings(self):
|
def load_settings(self):
|
||||||
if os.path.isfile(self.config_dir / "config.ini"):
|
if os.path.isfile(self.config_dir / "config.ini"):
|
||||||
with open(self.config_dir / "config.ini", "r") as configfile:
|
self.config.read(self.config_dir / "config.ini")
|
||||||
self.config.read(configfile)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.config["config"] = {
|
||||||
|
"version": "1"
|
||||||
|
}
|
||||||
self.config["editor"] = {
|
self.config["editor"] = {
|
||||||
"word_wrap": True
|
"word_wrap": "0"
|
||||||
}
|
}
|
||||||
self.config["plugins"] = {
|
self.config["plugins"] = {
|
||||||
"enabled": True
|
"enabled": "1"
|
||||||
}
|
}
|
||||||
self.config["appearance"] = {
|
self.config["appearance"] = {
|
||||||
"colour_theme": "textual-dark"
|
"colour_theme": "textual-dark"
|
||||||
}
|
}
|
||||||
|
|
||||||
with open(self.config_dir / "config.ini") as configfile:
|
with open(self.config_dir / "config.ini", "w") as configfile:
|
||||||
self.config.write(configfile)
|
self.config.write(configfile)
|
||||||
Reference in New Issue
Block a user