165 lines
4.5 KiB
Python
165 lines
4.5 KiB
Python
"""
|
|
Silique (https://www.silique.fr)
|
|
Copyright (C) 2024-2025
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
under the terms of the GNU Lesser General Public License as published by the
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
from typing import List
|
|
|
|
from ..i18n import _
|
|
from ..utils import dump, CommonFormater, ENTER
|
|
|
|
|
|
class Formater(CommonFormater):
|
|
"""The markdown (for github) formater"""
|
|
|
|
name = "console"
|
|
level = 10
|
|
enter_table = "\n"
|
|
titles_color = {
|
|
"title1": "bright_cyan underline bold",
|
|
"title2": "bright_green underline bold",
|
|
"title3": "green1 underline bold",
|
|
"title4": "green3 underline bold",
|
|
"title5": "dark_green underline bold",
|
|
}
|
|
|
|
def __init__(self, with_family: bool) -> None:
|
|
from rich.table import Table
|
|
from rich.theme import Theme
|
|
from rich.console import Console
|
|
|
|
self.rich_table = Table
|
|
self.rich_console = Console
|
|
self.custom_theme = Theme(self.titles_color)
|
|
self.max_line = 0
|
|
super().__init__(with_family)
|
|
|
|
def run(self, dico: dict, level: int, *, dico_is_already_treated=False) -> str:
|
|
|
|
if not dico_is_already_treated:
|
|
dico = self.dict_to_dict(dico, level)
|
|
console = self.rich_console(theme=self.custom_theme)
|
|
with console.capture() as capture:
|
|
for data in dico:
|
|
console.print(data)
|
|
return capture.get()
|
|
|
|
def title(
|
|
self,
|
|
title: str,
|
|
level: int,
|
|
) -> str:
|
|
"""Display family name as a title"""
|
|
space = " " * (2 * (level - 1))
|
|
return f"{ENTER}{space}[title{level}]{title}[/title{level}]{ENTER}"
|
|
|
|
def join(
|
|
self,
|
|
lst: List[str],
|
|
) -> str:
|
|
"""Display line in table from a list"""
|
|
return self.enter_table.join(lst)
|
|
|
|
def bold(
|
|
self,
|
|
msg: str,
|
|
) -> str:
|
|
"""Set a text to bold"""
|
|
return f"[bold]{msg}[/bold]"
|
|
|
|
def italic(
|
|
self,
|
|
msg: str,
|
|
) -> str:
|
|
"""Set a text to italic"""
|
|
return f"[italic]{msg}[/italic]"
|
|
|
|
def delete(
|
|
self,
|
|
msg: str,
|
|
) -> str:
|
|
"""Set a text to delete"""
|
|
return f"[strike]{msg}[/strike]"
|
|
|
|
def underline(
|
|
self,
|
|
msg: str,
|
|
) -> str:
|
|
"""Set a text to underline"""
|
|
return f"[underline]{msg}[/underline]"
|
|
|
|
def stripped(
|
|
self,
|
|
text: str,
|
|
) -> str:
|
|
"""Return stripped text (as help)"""
|
|
return text
|
|
|
|
def list(
|
|
self,
|
|
choices,
|
|
) -> str:
|
|
"""Display a liste of element"""
|
|
char = f"{self.enter_table}- "
|
|
ret = ""
|
|
for choice in choices:
|
|
if not isinstance(choice, str):
|
|
choice = dump(choice)
|
|
ret += char + choice
|
|
return ret
|
|
|
|
def prop(
|
|
self,
|
|
prop: str,
|
|
italic: bool,
|
|
) -> str:
|
|
"""Display property"""
|
|
prop = f"[reverse][bold] {prop} [/bold][/reverse]"
|
|
if italic:
|
|
prop = self.italic(prop)
|
|
return prop
|
|
|
|
def yaml(self, _dump):
|
|
"""Dump yaml part of documentation"""
|
|
return f"```yaml\n---\n{dump(_dump)}\n```\n"
|
|
|
|
def link(
|
|
self,
|
|
comment: str,
|
|
link: str,
|
|
) -> str:
|
|
"""Add a link"""
|
|
return self.prop(comment, False)
|
|
# return f"{comment} ({link})"
|
|
|
|
def columns(
|
|
self,
|
|
col: List[str],
|
|
) -> None:
|
|
"""count columns length"""
|
|
for line in col:
|
|
for l in line.split(self.enter_table):
|
|
self.max_line = max(self.max_line, len(l) + 1)
|
|
|
|
def table(self, datas: list, with_header: bool=True) -> str:
|
|
"""Transform list to a table in string format"""
|
|
table = self.rich_table(show_lines=True)
|
|
if with_header:
|
|
table.add_column(_("Variable"), width=self.max_line)
|
|
table.add_column(_("Description"), width=self.max_line)
|
|
for data in datas:
|
|
table.add_row(str(data[0]), data[1])
|
|
return table
|