Skip to content

Commit

Permalink
loading themes from config
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Sep 10, 2024
1 parent 518028c commit 414426c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 57 deletions.
19 changes: 10 additions & 9 deletions elia_chat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from elia_chat.chats_manager import ChatsManager
from elia_chat.models import ChatData, ChatMessage
from elia_chat.config import EliaChatModel, LaunchConfig, launch_config
from elia_chat.config import EliaChatModel, LaunchConfig
from elia_chat.runtime_config import RuntimeConfig
from elia_chat.screens.chat_screen import ChatScreen
from elia_chat.screens.help_screen import HelpScreen
Expand All @@ -35,7 +35,12 @@ class Elia(App[None]):

def __init__(self, config: LaunchConfig, startup_prompt: str = ""):
self.launch_config = config
launch_config.set(config)

available_themes: dict[str, Theme] = BUILTIN_THEMES.copy()
available_themes |= load_user_themes()

self.themes: dict[str, Theme] = available_themes

self._runtime_config = RuntimeConfig(
selected_model=config.default_model_object,
system_prompt=config.system_prompt,
Expand All @@ -53,11 +58,6 @@ def __init__(self, config: LaunchConfig, startup_prompt: str = ""):
put users into the chat window, rather than going to the home screen.
"""

available_themes: dict[str, Theme] = {"galaxy": BUILTIN_THEMES["galaxy"]}
available_themes |= load_user_themes()

self.themes: dict[str, Theme] = available_themes

super().__init__()

theme: Reactive[str | None] = reactive(None, init=False)
Expand Down Expand Up @@ -130,10 +130,11 @@ def get_css_variables(self) -> dict[str, str]:
return {**super().get_css_variables(), **color_system}

def watch_theme(self, theme: str | None) -> None:
print("theme changed:", theme)
print("theme object:", self.theme_object)
print("themes:", self.themes)
self.refresh_css(animate=False)
self.screen._update_styles()
if theme:
print(self.theme_object)

@property
def theme_object(self) -> Theme | None:
Expand Down
6 changes: 3 additions & 3 deletions elia_chat/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from contextvars import ContextVar
import os
from pydantic import AnyHttpUrl, BaseModel, ConfigDict, Field, SecretStr

Expand Down Expand Up @@ -182,5 +181,6 @@ def default_model_object(self) -> EliaChatModel:

return get_model(self.default_model, self)


launch_config: ContextVar[LaunchConfig] = ContextVar("launch_config")
@classmethod
def get_current(cls) -> "LaunchConfig":
return cls()
58 changes: 31 additions & 27 deletions elia_chat/elia.scss
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
$main-lighten-2: #9061F9;
$main-lighten-1: #7E3AF2;
$main: #6C2BD9;
$main-darken-1: #5521B5;
$main-darken-2: #4A1D96;
$main-border-text-color: greenyellow 70%;
$main-lighten-2: $primary-lighten-2;
$main-lighten-1: $primary-lighten-1;
$main: $primary;
$main-darken-1: $primary-darken-1;
$main-darken-2: $primary-darken-2;
$main-border-text-color: $accent 70%;
$main-border-color: $main-lighten-1 90%;
$main-border-color-focus: $main-lighten-2 100%;

$left-border-trim: vkey $main-lighten-2 15%;

* {
scrollbar-color: $panel;
scrollbar-color-hover: $main-lighten-1 40%;
scrollbar-color-active: $main-darken-1;
scrollbar-background: #0E0E0E;
scrollbar-background-active: $background-darken-1;
scrollbar-background-hover: #0C0C0C;
scrollbar-color: $accent 30%;
scrollbar-color-hover: $accent 80%;
scrollbar-color-active: $accent;
scrollbar-background: $surface-darken-1;
scrollbar-background-hover: $surface-darken-1;
scrollbar-background-active: $surface-darken-1;
scrollbar-size-vertical: 1;
link-style: none;
link-color-hover: greenyellow;
link-color-hover: $accent;
link-background-hover: $main 0%;
link-style-hover: u not dim bold;
}
Expand Down Expand Up @@ -178,13 +179,16 @@ AppHeader {
padding: 0 2 1 2;
}

& .app-title {
& #elia-title {
color: $text-muted;
}

& .app-title {
color: $secondary;
}

& .app-subtitle {
color: greenyellow 50%;
text-style: bold;
color: $accent;
}

& Horizontal {
Expand Down Expand Up @@ -222,27 +226,27 @@ Chatbox {
padding: 0 2;

&.assistant-message.response-in-progress {
background: greenyellow 3%;
background: $accent 3%;
min-width: 30%;
}

&.assistant-message {
border: round greenyellow 60%;
border: round $accent 60%;
&:focus-within {
border: round greenyellow;
border-left: thick greenyellow 50%;
border: round $accent;
border-left: thick $accent 50%;
}

& TextArea {
& .text-area--selection {
background: greenyellow 23%;
background: $accent 23%;
color: white 93%;
}
}

& SelectionTextArea.visual-mode {
& .text-area--cursor {
background: greenyellow;
background: $accent;
}
}

Expand Down Expand Up @@ -271,7 +275,7 @@ Footer {
}

.footer-key--key {
color: greenyellow;
color: $accent;
background: transparent;
}
}
Expand Down Expand Up @@ -467,23 +471,23 @@ OptionsModal #form-scrollable {

.code_inline {
text-style: none;
color: greenyellow;
color: $accent;
}

RadioSet:focus > RadioButton.-on {
& > .toggle--label {
text-style: bold not dim;
color: greenyellow;
color: $accent;
}
}

RadioSet > RadioButton.-on {
text-style: bold not dim;
color: greenyellow;
color: $accent;
}

RadioButton .toggle--button {
color: greenyellow 80%;
color: $accent 80%;
background: $background-lighten-1;
}

Expand Down
6 changes: 4 additions & 2 deletions elia_chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from typing import TYPE_CHECKING


from elia_chat.config import LaunchConfig, EliaChatModel, launch_config
from elia_chat.config import LaunchConfig, EliaChatModel

from textual._context import active_app

if TYPE_CHECKING:
from litellm.types.completion import ChatCompletionMessageParam
Expand All @@ -23,7 +25,7 @@ def get_model(
Models are looked up by ID first.
"""
if config is None:
config = launch_config.get()
config = active_app.get().launch_config
try:
return {model.id: model for model in config.all_models}[model_id_or_name]
except KeyError:
Expand Down
10 changes: 2 additions & 8 deletions elia_chat/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ def load_user_themes() -> dict[str, Theme]:


BUILTIN_THEMES: dict[str, Theme] = {
"posting": Theme(
name="posting",
"textual": Theme(
name="textual",
primary="#004578",
secondary="#0178D4",
warning="#ffa62b",
error="#ba3c5b",
success="#4EBF71",
accent="#ffa62b",
dark=True,
syntax="posting",
),
"monokai": Theme(
name="monokai",
Expand All @@ -78,7 +77,6 @@ def load_user_themes() -> dict[str, Theme]:
surface="#3E3D32", # Slightly lighter gray-green
panel="#3E3D32", # Same as surface for consistency
dark=True,
syntax="monokai",
),
"solarized-light": Theme(
name="solarized-light",
Expand All @@ -91,7 +89,6 @@ def load_user_themes() -> dict[str, Theme]:
background="#fdf6e3",
surface="#eee8d5",
panel="#eee8d5",
syntax="github_light",
),
"nautilus": Theme(
name="nautilus",
Expand All @@ -105,7 +102,6 @@ def load_user_themes() -> dict[str, Theme]:
background="#001F3F", # Dark Blue (deep ocean)
surface="#003366", # Navy Blue (shallower water)
panel="#005A8C", # Steel Blue (water surface)
syntax="posting",
),
"galaxy": Theme(
name="galaxy",
Expand All @@ -119,7 +115,6 @@ def load_user_themes() -> dict[str, Theme]:
background="#0F0F1F", # Very Dark Blue, almost black
surface="#1E1E3F", # Dark Blue-Purple
panel="#2D2B55", # Slightly Lighter Blue-Purple
syntax="monokai",
),
"nebula": Theme(
name="nebula",
Expand All @@ -133,7 +128,6 @@ def load_user_themes() -> dict[str, Theme]:
background="#0A0A23", # Dark Navy, closer to a night sky
surface="#1C1C3C", # Dark Blue-Purple
panel="#2E2E5E", # Slightly Lighter Blue-Purple
syntax="dracula",
),
"alpine": Theme(
name="alpine",
Expand Down
10 changes: 5 additions & 5 deletions elia_chat/widgets/app_header.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import TYPE_CHECKING, cast
from importlib.metadata import version
from rich.markup import escape
from rich.style import Style
from textual.app import ComposeResult
from textual.containers import Horizontal, Vertical
from textual.signal import Signal
Expand Down Expand Up @@ -40,17 +39,18 @@ def on_config_change(config: RuntimeConfig) -> None:
self.config_signal.subscribe(self, on_config_change)

def compose(self) -> ComposeResult:
title_style = self.get_component_rich_style("app-title")
subtitle_style = self.get_component_rich_style("app-subtitle")
title_style = self.get_component_rich_style("app-title", partial=True)
subtitle_style = self.get_component_rich_style("app-subtitle", partial=True)

with Horizontal():
with Vertical(id="cl-header-container"):
yield Label(
Text.assemble(
("elia ", title_style + Style(bold=True)),
("elia ", title_style),
("///", subtitle_style),
(f" {version('elia_chat')}", title_style),
)
),
id="elia-title",
)
model_name_or_id = (
self.elia.runtime_config.selected_model.id
Expand Down
4 changes: 1 addition & 3 deletions elia_chat/widgets/chatbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from elia_chat.config import EliaChatModel
from elia_chat.models import ChatMessage
from elia_chat.config import launch_config


class SelectionTextArea(TextArea):
Expand Down Expand Up @@ -353,8 +352,7 @@ def markdown(self) -> Markdown:
if not isinstance(content, str):
content = ""

config = launch_config.get()
return Markdown(content, code_theme=config.message_code_theme)
return Markdown(content, code_theme=self.app.launch_config.message_code_theme)

def render(self) -> RenderableType:
if self.selection_mode:
Expand Down

0 comments on commit 414426c

Please sign in to comment.