Skip to content

Commit

Permalink
added context highlighting support
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-dash committed Dec 26, 2023
1 parent a3091f5 commit 5e7f75d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
22 changes: 18 additions & 4 deletions rtab/base_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from rich.console import Console
from rich.table import Table

from rtab.context_highlighter import Dispatcher

# Initialize console object for print
console = Console()

Expand All @@ -18,6 +20,7 @@ def __init__(self, **kwargs) -> None:
self.table = None
self.wrap = False
self.lines = False
self.rule = ""
for k, v in kwargs.items():
setattr(self, k, v)

Expand All @@ -29,20 +32,20 @@ def load(self, data) -> dict: # pylint: disable=missing-type-doc
"""
return

def create_table(self) -> Table:
def create_table(self) -> None:
"""Create a rich table object."""
# pylint: disable=maybe-no-member
self.table = Table(box=box.ROUNDED, highlight=not self.quiet, show_lines=self.lines)

def add_column(self, column: str = ""):
def add_column(self, column: str = "") -> None:
"""Add a column to a rich table object.
:param column: A string to add to the table as a column header
"""
# pylint: disable=maybe-no-member
(self.table).add_column(column, overflow="fold" if self.wrap else "ellipsis")

def add_row(self, row: list = None):
def add_row(self, row: list = None) -> None:
"""Add a row to a rich table object.
:param row: A list to add to the table as a row
Expand All @@ -66,6 +69,15 @@ def pre_run(self, stdin_data, skip_load: bool = False): # pylint: disable=missi
console.print(f"Can not load stdin into {type(self).__name__}")
return data, type(data)

def console_print(self) -> None:
"""Get custom rich console object and print.
If rule is not specified as a cli flag, then the default rich console object is used.
"""
dc = Dispatcher()
custom_console = dc.get_console(self.rule)
custom_console.print(self.table)

def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=missing-type-doc
"""Load stdin as json and transform into rich table.
Expand Down Expand Up @@ -96,5 +108,7 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
console.print(f"Unsupported type {data_type}")
return 1

console.print(self.table)
# Print the table
self.console_print()

return 0
16 changes: 11 additions & 5 deletions rtab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from rtab.json_helper import JsonToRichTable
from rtab.yaml_helper import YamlToRichTable

OPENSTACK_RED = "bold #F20B4B"
OPENSTACK_BLUE = "bold #19ADE5"
JUJU = "bold #E95420"
KUBERNETES = "bold #3970E4"

cli = typer.Typer(
name="rtab",
rich_markup_mode="rich",
Expand Down Expand Up @@ -105,13 +110,14 @@ def main( # pylint: disable=too-many-arguments
typer.Option(
"-r",
"--rule",
help="""
Add special highlighting ruels such as [bold blue]openstack[/bold blue],\n
[bold red]juju[/bold red], etc...
help=f"""
Add special highlighting ruels such as
[{OPENSTACK_RED}]open[/{OPENSTACK_RED}][{OPENSTACK_BLUE}]stack[/{OPENSTACK_BLUE}],\n
[{JUJU}]juju[/{JUJU}], [{KUBERNETES}]kubernetes[/{KUBERNETES}], etc...
""",
rich_help_panel="Context Highlighting",
),
] = None,
] = "",
file: Annotated[
Optional[str],
typer.Argument(
Expand All @@ -126,7 +132,7 @@ def main( # pylint: disable=too-many-arguments
:param is_csv: Bool flag to format csv into rich tables
:param quiet: Suppress highlighting
:param separator: Specify a separator, only applies with table input '-t'
:param rule: Add special highlighting ruels such as openstack, juju, etc...
:param rule: Add special highlighting ruels such as openstack, juju, kubernetes, etc...
:param wrap: Allow wrapping of text in rows, helpful if row is long
:param lines: Show lines between rows
:param file: Specify a file (file extension matters!)
Expand Down
24 changes: 5 additions & 19 deletions rtab/context_highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,13 @@ def get_console(self, rule: str = "") -> Console:
:param rule: string to match a specific highlighting rule
"""
theme = self.get_theme()
match rule:
case "openstack":
highlighter_object = OpenstackRegexHighlighter()
return Console(highlighter=OpenstackRegexHighlighter(), theme=theme)
case "juju":
highlighter_object = JujuRegexHighlighter()
return Console(highlighter=JujuRegexHighlighter(), theme=theme)
case "kubernetes":
highlighter_object = KubernetesRegexHighlighter()
return Console(highlighter=KubernetesRegexHighlighter(), theme=theme)
case _:
highlighter_object = None
if highlighter_object:
return Console(highlighter=highlighter_object, theme=self.get_theme())
return Console()


# if __name__ == "__main__":
# string = "Send cloud disabled lost XXX funds :) to [email protected] "
# string += "downbadxxx xxx and active make it ACTIVE lol but not 2023-10-10 down"

# dc = Dispatcher()
# console = dc.get_console("openstack")
# console.print(string)

# console = dc.get_console()
# console.print(string)
return Console()
4 changes: 3 additions & 1 deletion rtab/csv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
console.print(f"Csv reader can't handle {data_type}")
return 1

console.print(self.table)
# Print the table
self.console_print()

return 0

0 comments on commit 5e7f75d

Please sign in to comment.