refactored console theme logic

This commit is contained in:
evilchili 2022-12-17 10:50:02 -08:00
parent b7ec4259a6
commit 5ff2c39542
4 changed files with 78 additions and 45 deletions

View File

@ -1,11 +1,13 @@
import os import os
from configparser import ConfigParser
from pathlib import Path from pathlib import Path
from textwrap import dedent from textwrap import dedent
from rich.console import Console as _Console from rich.console import Console as _Console
from rich.markdown import Markdown from rich.markdown import Markdown
from rich.theme import Theme from rich.theme import Theme
from rich.table import Table
from prompt_toolkit import prompt as _toolkit_prompt from prompt_toolkit import prompt as _toolkit_prompt
from prompt_toolkit.formatted_text import ANSI 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): class Console(_Console):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if 'theme' not in kwargs: self._console_theme = console_theme(kwargs.get('theme', None))
theme_path = theme(os.environ['DEFAULT_THEME']) kwargs['theme'] = Theme(self._console_theme, inherit=False)
kwargs['theme'] = Theme(BASE_STYLE).read(theme_path / Path('console.cfg'), inherit=False)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@property
def theme(self):
return self._console_theme
def prompt(self, lines, **kwargs): def prompt(self, lines, **kwargs):
for line in lines[:-1]: for line in lines[:-1]:
super().print(line) super().print(line)
@ -45,3 +59,21 @@ class Console(_Console):
def error(self, txt, **kwargs): def error(self, txt, **kwargs):
super().print(dedent(txt), style='error') 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)

View File

@ -144,47 +144,6 @@ class Playlist:
text += f" {tracknum+1:-3d}. {entry.artist} - {entry.title}\n" text += f" {tracknum+1:-3d}. {entry.artist} - {entry.title}\n"
return text 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 @property
def as_yaml(self) -> str: def as_yaml(self) -> str:
template_vars = self.as_dict template_vars = self.as_dict

View File

@ -4,6 +4,8 @@ import os
from sqlalchemy.exc import NoResultFound from sqlalchemy.exc import NoResultFound
from textwrap import dedent, wrap from textwrap import dedent, wrap
from rich.table import Column
from rich import box
from groove import db from groove import db
from groove.exceptions import PlaylistValidationError from groove.exceptions import PlaylistValidationError
@ -95,7 +97,27 @@ class _playlist(BasePrompt):
return True return True
def show(self, *parts): 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 return True
def add(self, *parts): def add(self, *parts):

View File

@ -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