Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / src / editor / configuration.luc

## Validate both files before applying a new configuration. Invalid edits stay
## on disk for correction; a failed revision is reported once until it changes.
from configuration.store import Store
from configuration.settings import Settings
from configuration.theme import Appearance
from luce_ai.config import AiConfig
from editor.options import Options
from luce_ui.ui import Application, Font, Layout
from workspace.controller import Workspace
from commands.runner import Runner

pub class Configuration:
    pub let store: Store
    let font: Font
    let app: Application
    let root: Layout
    let workspace: Workspace
    let runner: Runner
    let options: Options
    var settings_text: str
    var theme_text: str
    var ai_text: str
    var ai_config: AiConfig
    var family: str
    var elapsed: float = 0.0

    pub func init(self, store: Store, font: Font, app: Application, root: Layout,
                  workspace: Workspace, runner: Runner, options: Options, family: str) -> unit!:
        self.store = store
        self.font = font
        self.app = app
        self.root = root
        self.workspace = workspace
        self.runner = runner
        self.options = options
        self.settings_text = store.read_settings()
        self.theme_text = store.read_theme()
        self.ai_text = store.read_ai()
        self.ai_config = AiConfig(self.ai_text) catch failure:
            recover AiConfig()
        self.family = family

    ## The current AI settings; always a valid object, defaults when the file is
    ## malformed until a corrected version is applied.
    pub func ai(self) -> AiConfig:
        return self.ai_config

    pub func edit_settings(self) -> unit!:
        self.workspace.open(self.store.settings_path)

    pub func edit_theme(self) -> unit!:
        self.workspace.open(self.store.theme_path)

    pub func edit_ai(self) -> unit!:
        self.workspace.open(self.store.ai_path)

    pub func reload(self) -> unit!:
        self.apply(self.store.read_settings(), self.store.read_theme(), self.store.read_ai()) catch failure:
            self.workspace.report("Configuration: " + failure.message)

    func apply(self, settings_text: str, theme_text: str, ai_text: str) -> unit!:
        self.settings_text = settings_text
        self.theme_text = theme_text
        self.ai_text = ai_text
        let settings = Settings(settings_text) catch failure:
            error(failure.code, "settings.toml: " + failure.message)
        let appearance = Appearance(theme_text) catch failure:
            error(failure.code, "theme.toml: " + failure.message)
        self.ai_config = AiConfig(ai_text) catch failure:
            error(failure.code, "ai.toml: " + failure.message)
        if self.font.size() != settings.font_size or self.family != settings.font_family:
            self.font.configure(settings.font_size, settings.font_family)
            self.family = settings.font_family
        self.workspace.set_syntax(appearance.syntax)
        self.workspace.set_theme_colors(appearance.theme)
        self.app.set_theme(appearance.theme)
        self.root.invalidate()
        self.runner.configure(self.options.luce if self.options.luce_override else settings.luce,
            self.options.base if self.options.base_override else settings.base)
        self.workspace.report("Configuration reloaded. Window dimensions apply on next launch.")

    pub func poll(self, elapsed: float) -> unit!:
        self.elapsed += elapsed
        if self.elapsed < 0.5:
            return
        self.elapsed = 0.0
        self.changed() catch failure:
            self.workspace.report("Configuration: " + failure.message)

    func changed(self) -> unit!:
        let settings = self.store.read_settings()
        let theme = self.store.read_theme()
        let ai = self.store.read_ai()
        if settings != self.settings_text or theme != self.theme_text or ai != self.ai_text:
            self.apply(settings, theme, ai)