def tele_ls(): try: path = sys.argv[2] sub_path = __sub_path(path, False) except IndexError: sub_path = __sub_path('./', False) server_target = _choose_server_target() if server_target: from . import SshProtocol res = SshProtocol.command(server_target['user'], server_target['host'], server_target['path'], server_target['port'], f'ls -lah {sub_path}').strip().split('\n')[3:] res = [i.strip().split() for i in res] from rich.table import Table from rich.box import SIMPLE_HEAVY from rich.color import Color from rich.text import Text from rich.style import Style table = Table(title=f'{sub_path}', box=SIMPLE_HEAVY, show_header=True, header_style='bold magenta') table.add_column('permission' if user_lang != 'zh' else '权限码', justify='center') table.add_column('size' if user_lang != 'zh' else '尺寸', justify='center') table.add_column('owner' if user_lang != 'zh' else '拥有者', justify='center') table.add_column('time' if user_lang != 'zh' else '修改时间', justify='center') table.add_column('name' if user_lang != 'zh' else '文件名', justify='right') for i in res: is_dir = True if i[0][0] == 'd' else False color = Color.from_rgb(112, 87, 250) if is_dir else None p_color = Color.from_rgb(171, 157, 242) permission = ''.join(['1' if j != '-' else '0' for j in i[0][1:]]) permission = '%d%d%d' % (int(permission[:3], 2), int(permission[3: 6], 2), int(permission[6:], 2)) table.add_row(*[Text(permission, style=Style(color=p_color)), f'[green]{i[4]}', '[bold yellow]' + i[2], '[blue]' + ' '.join(i[5:8]), Text(" ".join(i[8:]), style=Style(color=color, bold=is_dir))]) QproDefaultConsole.print(table, justify='center')
def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: for y in range(0, 5): for x in range(options.max_width): h = x / options.max_width l = 0.1 + ((y / 5) * 0.7) r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0) r2, g2, b2 = colorsys.hls_to_rgb(h, l + 0.7 / 10, 1.0) bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255) color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255) yield Segment("▄", Style(color=color, bgcolor=bgcolor)) yield Segment.line()
def __rich_console__(self, console: Console, options: ConsoleOptions) -> Iterable[Segment]: height = console.size.height - 3 for y in range(0, height): for x in range(options.max_width): h = x / options.max_width l = y / (height + 1) r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0) r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2), 1.0) bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255) color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255) yield Segment("▄", Style(color=color, bgcolor=bgcolor)) yield Segment.line()
def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: for y in range(0, 5): for x in range(options.max_width): h = x / options.max_width l = 0.1 + ((y / 5) * 0.7) r, g, b = colorsys.hls_to_rgb(h, l, 1.0) yield Segment( "█", Style(color=Color.from_rgb(r * 255, g * 255, b * 255))) yield Segment.line()
def __rich__(self) -> "Table": from rich.color import Color from rich.style import Style from rich.text import Text from rich.table import Table table = Table( "index", "RGB", "Color", title="Palette", caption=f"{len(self._colors)} colors", highlight=True, caption_justify="right", ) for index, color in enumerate(self._colors): table.add_row( str(index), repr(color), Text(" " * 16, style=Style(bgcolor=Color.from_rgb(*color))), ) return table
elif (code := BASE_COLOR_MAP.get(c, None)): setattr(mark, "color", Color.from_ansi(code)) elif (code := BASE_COLOR_MAP.get(c.lower(), None)): setattr(mark, "bgcolor", Color.from_ansi(code)) else: pass # I dunno what we got passed, but it ain't relevant. elif g == BgMode.FG: if mode == "numbers": setattr(mark, "color", Color.from_ansi(data)) elif mode == "name": if (found := COLORS.get(data)): setattr(mark, "color", Color.from_ansi(found["xterm"])) elif mode in ("rgb", "hex1", "hex2"): setattr(mark, "color", Color.from_rgb(data["red"], data["green"], data["blue"])) elif g == BgMode.BG: if mode == "numbers": setattr(mark, "bgcolor", Color.from_ansi(data)) elif mode == "name": if (found := COLORS.get(data)): setattr(mark, "bgcolor", Color.from_ansi(found["xterm"])) elif mode in ("rgb", "hex1", "hex2"): setattr( mark, "bgcolor", Color.from_rgb(data["red"], data["green"], data["blue"]), ) def apply_rules(mark: ProtoStyle, rules: str):
def test_from_rgb() -> None: assert Color.from_rgb(0x10, 0x20, 0x30) == Color("#102030", ColorType.TRUECOLOR, None, ColorTriplet(0x10, 0x20, 0x30))
""" Use Bar to renderer a sort-of circle. """ import math from rich.align import Align from rich.bar import Bar from rich.color import Color from rich import print SIZE = 40 for row in range(SIZE): y = (row / (SIZE - 1)) * 2 - 1 x = math.sqrt(1 - y * y) color = Color.from_rgb((y + 1) * 127, 0, 0) bar = Bar(2, width=SIZE * 2, begin=1 - x, end=1 + x, color=color) print(Align.center(bar))
""" Use Bar to renderer a sort-of circle. """ import math from rich.align import Align from rich.bar import Bar from rich.color import Color from rich import print SIZE = 40 for row in range(SIZE): y = (row / (SIZE - 1)) * 2 - 1 x = math.sqrt(1 - y * y) color = Color.from_rgb((1 + y) * 127.5, 0, 0) bar = Bar(2, width=SIZE * 2, begin=1 - x, end=1 + x, color=color) print(Align.center(bar))
def run(feed_url): """ Pull a podcast rss feed from a given url and print a table with all episodes sorted by time between episodes :raises ValueError: If the feed does not have all of the required data """ console = Console(width=150) # get the feed and parse it parsed_feed = [] for url in feed_url: parsed_feed.extend(get_parsed_feed(url)) # Draw weekday distribution heatmap = Table(title="Weekday Heatmap") heat = { 0: Style(bgcolor=Color.from_rgb(red=153, green=0, blue=13)), 1: Style(bgcolor=Color.from_rgb(red=203, green=24, blue=29)), 2: Style(bgcolor=Color.from_rgb(red=239, green=59, blue=44)), 3: Style(bgcolor=Color.from_rgb(red=251, green=106, blue=74), color="black"), 4: Style(bgcolor=Color.from_rgb(red=252, green=146, blue=114), color="black"), 5: Style(bgcolor=Color.from_rgb(red=252, green=187, blue=161), color="black"), 6: Style(bgcolor=Color.from_rgb(red=254, green=229, blue=217), color="black"), } count = weekday_distribution(parsed_feed) result = {} for i, item in enumerate(count.most_common()): result[item[0]] = {"value": str(item[1]), "style": heat[i]} row = [] for d in ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]: day = result.get(d, {"value": "0", "style": ""}) heatmap.add_column(d, style=day["style"], justify="center") row.append(day["value"]) heatmap.add_row(*row) console.print(heatmap) table = Table( "Podcast", "Title", "Date published", "Days since last", title="Episodes", ) for episode in sorted(parsed_feed, key=lambda x: x["published_datetime"], reverse=True): table.add_row( episode["podcast"], " ".join(episode["title"].split()), episode["published_datetime"].in_timezone( pendulum.local_timezone()).to_day_datetime_string(), str(episode["time_since_last"].days), ) console.print(table)