diff --git a/cheuph/euphoria/room_widget.py b/cheuph/euphoria/room_widget.py index 77019ab..9abc3b6 100644 --- a/cheuph/euphoria/room_widget.py +++ b/cheuph/euphoria/room_widget.py @@ -9,6 +9,7 @@ from ..cursor_rendering import CursorTreeRenderer from ..cursor_tree_widget import CursorTreeWidget from ..element import Message, RenderedMessage from ..element_supply import InMemorySupply +from .euph_config import EuphConfig from ..markup import AT, AttributedText, Attributes from .edit_widgets import EditWidget from .euph_renderer import EuphRenderer @@ -164,9 +165,12 @@ class RoomWidget(urwid.WidgetWrap): def __init__(self, roomname: str, + config: EuphConfig, log_amount: int = 200, ) -> None: + self.c = config + if log_amount < 1: raise ValueError() # TODO add better text self._log_amount = log_amount @@ -226,7 +230,7 @@ class RoomWidget(urwid.WidgetWrap): def _create_connecting_widget(self) -> Any: text = ( AT("Connecting to ") + - AT("&" + self._room.name) + + AT("&" + self._room.name, style=self.c.room_style) + AT("...") ) # Centered vertically and horizontally @@ -235,14 +239,17 @@ class RoomWidget(urwid.WidgetWrap): def _create_connection_failed_widget(self) -> Any: text = ( AT("Could not connect to ") + - AT("&" + self._room.name) + + AT("&" + self._room.name, style=self.c.room_style) + AT("...") ) # Centered vertically and horizontally return urwid.Filler(ATWidget(text, align=urwid.CENTER)) def _create_room_name_widget(self) -> Any: - return urwid.Text("&" + self._room.name, align=urwid.CENTER) + return urwid.Text( + (self.c.room_style, "&" + self._room.name), + align=urwid.CENTER, + ) def _create_tree_widget(self) -> Any: return CursorTreeWidget(self._tree) diff --git a/cheuph/euphoria/single_room_application.py b/cheuph/euphoria/single_room_application.py index 801f394..59c9112 100644 --- a/cheuph/euphoria/single_room_application.py +++ b/cheuph/euphoria/single_room_application.py @@ -1,10 +1,15 @@ +import asyncio +import logging +from pathlib import Path from typing import Any, Optional import urwid +import yaml from ..attributed_text_widget import ATWidget -from ..markup import Attributes, AT +from ..markup import AT, Attributes from .edit_widgets import EditWidget +from .euph_config import EuphConfig, EuphLoader from .room_widget import RoomWidget __all__ = ["SingleRoomApplication"] @@ -13,13 +18,13 @@ class ChooseRoomWidget(urwid.WidgetWrap): def __init__(self, room_style: Optional[str] = None, - error_attrs: Attributes = {}, - error_room_attrs: Attributes = {}, + error_style: Optional[str] = None, + error_room_style: Optional[str] = None, ) -> None: self._room_style = room_style - self._error_attrs = error_attrs - self._error_room_attrs = error_room_attrs + self._error_style = error_style + self._error_room_style = error_room_style self.error = None self.edit = EditWidget("Choose a room:", caption="&", style=room_style) @@ -49,9 +54,9 @@ class ChooseRoomWidget(urwid.WidgetWrap): self._w = self.filler def could_not_connect(self, roomname: str) -> None: - text = AT("Could not connect to ", attributes=self._error_attrs) - text += AT("&" + roomname, attributes=self._error_room_attrs) - text += AT(".\n", attributes=self._error_attrs) + text = AT("Could not connect to ", style=self._error_style) + text += AT("&" + roomname, style=self._error_room_style) + text += AT(".\n", style=self._error_style) self.set_error(ATWidget(text, align=urwid.CENTER)) @@ -74,8 +79,14 @@ class SingleRoomApplication(urwid.WidgetWrap): "home", "end", } - def __init__(self) -> None: - self.choose_room = ChooseRoomWidget() + def __init__(self, config: EuphConfig) -> None: + self.c = config + + self.choose_room = ChooseRoomWidget( + room_style=self.c.room_style, + error_style=self.c.error_style, + error_room_style=self.c.error_room_style, + ) super().__init__(self.choose_room) def selectable(self) -> bool: @@ -96,7 +107,7 @@ class SingleRoomApplication(urwid.WidgetWrap): roomname = self.choose_room.edit.text if roomname: - room = RoomWidget(roomname) + room = RoomWidget(roomname, self.c) urwid.connect_signal(room, "close", self.switch_to_choose) self._w = room room.connect() @@ -117,3 +128,32 @@ class SingleRoomApplication(urwid.WidgetWrap): return super().keypress(size, key) return key + +def run_single_room_application(config_file: Path) -> None: + logging.disable() + + config_yaml: Any + try: + with open(config_file) as f: + config_yaml = yaml.load(f.read(), Loader=yaml.SafeLoader) + except FileNotFoundError: + config_yaml = {} + + loader = EuphLoader() + defaults = loader.defaults() + config = EuphConfig(defaults) + loader.load_to(config, config_yaml) + + loop = asyncio.get_event_loop() + + main_loop = urwid.MainLoop( + SingleRoomApplication(config), + event_loop=urwid.AsyncioEventLoop(loop=loop), + palette=config.palette, + ) + + main_loop.run() + +if __name__ == "__main__": + config_path = Path("~/.cheuph.conf").expanduser() + run_single_room_application(config_path)