def test_from_ansi(): text = Text.from_ansi("Hello, \033[1mWorld!\033[0m") assert str(text) == "Hello, World!" assert text._spans == [Span(7, 13, Style(bold=True))] text = Text.from_ansi("Hello, \033[1m\nWorld!\033[0m") assert str(text) == "Hello, \nWorld!" assert text._spans == [Span(8, 14, Style(bold=True))]
def test_from_ansi(): text = Text.from_ansi("Hello, \033[1mWorld!\033[0m") assert str(text) == "Hello, World!" assert text._spans == [Span(7, 13, Style(bold=True))] text = Text.from_ansi("Hello, \033[1m\nWorld!\033[0m") assert str(text) == "Hello, \nWorld!" assert text._spans == [Span(8, 14, Style(bold=True))] text = Text.from_ansi("\033[1mBOLD\033[m not bold") assert str(text) == "BOLD not bold" assert text._spans == [Span(0, 4, Style(bold=True))] text = Text.from_ansi("\033[1m\033[Kfoo barmbaz") assert str(text) == "foo barmbaz" assert text._spans == [Span(0, 11, Style(bold=True))]
def test_decode_example(): ansi_bytes = b"\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[m\x1b[K In function '\x1b[01m\x1b[Kmain\x1b[m\x1b[K':\n\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[m\x1b[K \x1b[01;35m\x1b[Kwarning: \x1b[m\x1b[Kunused variable '\x1b[01m\x1b[Ka\x1b[m\x1b[K' [\x1b[01;35m\x1b[K-Wunused-variable\x1b[m\x1b[K]\n 3 | int \x1b[01;35m\x1b[Ka\x1b[m\x1b[K=1;\n | \x1b[01;35m\x1b[K^\x1b[m\x1b[K\n" ansi_text = ansi_bytes.decode("utf-8") text = Text.from_ansi(ansi_text) console = Console(force_terminal=True, legacy_windows=False, color_system="truecolor") with console.capture() as capture: console.print(text) result = capture.get() print(repr(result)) expected = "\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[0m In function '\x1b[1mmain\x1b[0m':\n\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[0m \x1b[1;35mwarning: \x1b[0munused variable '\x1b[1ma\x1b[0m' \n[\x1b[1;35m-Wunused-variable\x1b[0m]\n 3 | int \x1b[1;35ma\x1b[0m=1;\n | \x1b[1;35m^\x1b[0m\n" assert result == expected
): fd = open(mail_path, "rb") parsed = email_parser.parse(fd) n = {} n["from"] = parsed.get("From") n["date"] = parsedate_to_datetime(parsed.get("Date")).date().isoformat() n["subject"] = parsed.get("Subject") fd.close() m.append(n) mail[account] = m # Do some text preprocessing calendar = calendar.split("\n\n") calendar = [event.rstrip().split("\n", 1) for event in calendar] fetch = Text.from_ansi(fetch) console = Console() # Construct the looped panels events = Columns([Panel(event[1], title=event[0][:-1]) for event in calendar]) # Have a panel for each account with panels on mail inside the account mails = Columns( [ Panel( Columns( [ Panel("\n".join([x["from"], x["subject"]]), title=x["date"]) for x in y ], expand=True,
class HelpCommand(CLICommand): """ Usage: help [command] Parameter: command [optional]: if given shows the help for a specific command Show help text for a command or general help information. """ with open(os.path.dirname(__file__) + "/../static/ck-unicode-truecolor.ans", "r", encoding="utf-8") as log_file: ck = Text.from_ansi(log_file.read()) def __init__(self, dependencies: CLIDependencies, parts: List[CLICommand], aliases: Dict[str, str]): super().__init__(dependencies) self.all_parts = {p.name: p for p in parts + [self]} self.parts = {p.name: p for p in parts + [self] if not isinstance(p, InternalPart)} self.aliases = {a: n for a, n in aliases.items() if n in self.parts and a not in self.parts} @property def name(self) -> str: return "help" def info(self) -> str: return "Shows available commands, as well as help for any specific command." def parse(self, arg: Optional[str] = None, ctx: CLIContext = EmptyContext, **kwargs: Any) -> CLISource: def overview() -> str: all_parts = sorted(self.parts.values(), key=lambda p: p.name) parts = (p for p in all_parts if isinstance(p, CLICommand)) indent = " " # required for dedent to work properly available = "\n".join(f"{indent}- `{part.name}` - {part.info()}" for part in parts) aliases = "\n".join( f"{indent}- `{alias}` (`{cmd}`) - {self.parts[cmd].info()}" for alias, cmd in self.aliases.items() ) replacements = "\n".join( f"{indent}- `@{key}@` -> {value}" for key, value in CLI.replacements(**ctx.env).items() ) result = dedent( f""" ## Valid placeholder string: \n{replacements} ## Available Aliases: \n{aliases} ## Available Commands: \n{available} *Note* that you can pipe commands using the pipe character (|) and chain multiple commands using the semicolon (;)." Use `help <command>` to show help for a specific command. """ ) headline = ctx.render_console(f"# resotocore CLI ({version()})") # ck mascot is centered (rendered if color is enabled) middle = ( int((ctx.console_renderer.width - 22) / 2) if ctx.console_renderer is not None and ctx.console_renderer.width is not None else 0 ) logo = ctx.render_console(Padding(self.ck, pad=(0, 0, 0, middle))) if ctx.supports_color() else "" return headline + logo + ctx.render_console(result) def help_command() -> Stream: if not arg: result = overview() elif arg and arg in self.all_parts: result = self.all_parts[arg].rendered_help(ctx) elif arg and arg in self.aliases: alias = self.aliases[arg] explain = f"{arg} is an alias for {alias}\n\n" result = explain + self.all_parts[alias].rendered_help(ctx) else: result = f"No command found with this name: {arg}" return stream.just(result) return CLISource.single(help_command)