From 5ff2c3954268c0d2d4b56cbc8fca8ce2b880ee1c Mon Sep 17 00:00:00 2001 From: evilchili Date: Sat, 17 Dec 2022 10:50:02 -0800 Subject: [PATCH] refactored console theme logic --- groove/console.py | 38 +++++++++++++++++++++++++++++--- groove/playlist.py | 41 ----------------------------------- groove/shell/playlist.py | 24 +++++++++++++++++++- themes/blue_train/console.cfg | 20 +++++++++++++++++ 4 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 themes/blue_train/console.cfg diff --git a/groove/console.py b/groove/console.py index 5a28ab4..dfe91aa 100644 --- a/groove/console.py +++ b/groove/console.py @@ -1,11 +1,13 @@ import os +from configparser import ConfigParser from pathlib import Path from textwrap import dedent from rich.console import Console as _Console from rich.markdown import Markdown from rich.theme import Theme +from rich.table import Table from prompt_toolkit import prompt as _toolkit_prompt from prompt_toolkit.formatted_text import ANSI @@ -21,14 +23,26 @@ BASE_STYLE = { } +def console_theme(theme_name=None): + cfg = ConfigParser() + cfg.read_dict({'styles': BASE_STYLE}) + cfg.read(theme( + theme_name or os.environ['DEFAULT_THEME']) / Path('console.cfg') + ) + return cfg['styles'] + + class Console(_Console): def __init__(self, *args, **kwargs): - if 'theme' not in kwargs: - theme_path = theme(os.environ['DEFAULT_THEME']) - kwargs['theme'] = Theme(BASE_STYLE).read(theme_path / Path('console.cfg'), inherit=False) + self._console_theme = console_theme(kwargs.get('theme', None)) + kwargs['theme'] = Theme(self._console_theme, inherit=False) super().__init__(*args, **kwargs) + @property + def theme(self): + return self._console_theme + def prompt(self, lines, **kwargs): for line in lines[:-1]: super().print(line) @@ -45,3 +59,21 @@ class Console(_Console): def error(self, txt, **kwargs): super().print(dedent(txt), style='error') + + def table(self, *cols, **params): + if os.environ['CONSOLE_THEMES']: + background_style = f"on {self.theme['background']}" + params.update( + header_style=background_style, + title_style=background_style, + border_style=background_style, + row_styles=[background_style], + caption_style=background_style, + style=background_style, + ) + width = os.environ.get('CONSOLE_WIDTH', 'auto') + if width == 'expand': + params['expand'] = True + elif width != 'auto': + params['width'] = int(width) + return Table(*cols, **params) diff --git a/groove/playlist.py b/groove/playlist.py index ca153b2..12c46b8 100644 --- a/groove/playlist.py +++ b/groove/playlist.py @@ -144,47 +144,6 @@ class Playlist: text += f" {tracknum+1:-3d}. {entry.artist} - {entry.title}\n" return text - @property - def as_richtext(self) -> str: - title = f"\n [b]:headphones: {self.name}[/b]" - if self.description: - title += f"\n [italic]{self.description}[/italic]\n" - params = dict( - box=box.HORIZONTALS, - title=title, - title_justify='left', - caption=f"[link]{self.url}[/link]", - caption_justify='right', - ) - if os.environ['CONSOLE_THEMES']: - params.update( - header_style='on #001321', - title_style='on #001321', - border_style='on #001321', - row_styles=['on #001321'], - caption_style='on #001321', - style='on #001321', - ) - width = os.environ.get('CONSOLE_WIDTH', 'auto') - if width == 'expand': - params['expand'] = True - elif width != 'auto': - params['width'] = int(width) - - table = Table( - Column('#', justify='right', width=4), - Column('Artist', justify='left'), - Column('Title', justify='left'), - **params - ) - for (num, entry) in enumerate(self.entries): - table.add_row( - f"[text]{num+1}[/text]", - f"[artist]{entry.artist}[/artist]", - f"[title]{entry.title}[/title]" - ) - return table - @property def as_yaml(self) -> str: template_vars = self.as_dict diff --git a/groove/shell/playlist.py b/groove/shell/playlist.py index 5275ab6..39df3d0 100644 --- a/groove/shell/playlist.py +++ b/groove/shell/playlist.py @@ -4,6 +4,8 @@ import os from sqlalchemy.exc import NoResultFound from textwrap import dedent, wrap +from rich.table import Column +from rich import box from groove import db from groove.exceptions import PlaylistValidationError @@ -95,7 +97,27 @@ class _playlist(BasePrompt): return True def show(self, *parts): - self.parent.console.print(self.parent.playlist.as_richtext) + pl = self.parent.playlist + title = f"\n [b]:headphones: {pl.name}[/b]" + if pl.description: + title += f"\n [italic]{pl.description}[/italic]\n" + table = self.parent.console.table( + Column('#', justify='right', width=4), + Column('Artist', justify='left'), + Column('Title', justify='left'), + box=box.HORIZONTALS, + title=title, + title_justify='left', + caption=f"[link]{pl.url}[/link]", + caption_justify='right', + ) + for (num, entry) in enumerate(pl.entries): + table.add_row( + f"[text]{num+1}[/text]", + f"[artist]{entry.artist}[/artist]", + f"[title]{entry.title}[/title]" + ) + self.parent.console.print(table) return True def add(self, *parts): diff --git a/themes/blue_train/console.cfg b/themes/blue_train/console.cfg new file mode 100644 index 0000000..8bafed2 --- /dev/null +++ b/themes/blue_train/console.cfg @@ -0,0 +1,20 @@ +[styles] +# default gray text #999999 +# light text #f1f2f6 +# dark gray #555555 +# light blue #9999ff +# green #70bc45 +# dark blue #017d93 +text = #999999 +bright = #f1f2f6 +bold = #f1f2f6 bold +dim = #555555 +link = #9999FF +prompt = #f1f2f6 bold +artist = #017D93 +title = #70bc45 +help = #999999 + +background = #001321 + +error = #FF8888