Skip to content

Commit

Permalink
color cell is subfunction
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoreno committed Dec 20, 2023
1 parent f0fc69c commit 6dd6950
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions cli/rtpmidid-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,44 @@ def get_latency(self, data):
##

def print_table(self):
def get_color_cell(row: dict, rown: int, coln: int):
if rown == 0:
if coln == self.selected_col_index:
return (
self.ANSI_BG_CYAN + self.ANSI_TEXT_WHITE + self.ANSI_TEXT_BOLD
)
return self.ANSI_BG_PURPLE + self.ANSI_TEXT_WHITE + self.ANSI_TEXT_BOLD

# we are in the table rows
rown -= 1
bg = self.ANSI_BG_BLACK
fg = self.ANSI_TEXT_WHITE

if rown == self.selected_row_index:
bg = self.ANSI_BG_WHITE
fg = self.ANSI_TEXT_BLACK

status = self.get_peer_status(row)
column = self.COLUMNS[coln]

bold = ""

if column["name"] == "Status":
if status == "CONNECTED":
fg = self.ANSI_TEXT_GREEN
elif status == "CONNECTING":
fg = self.ANSI_TEXT_YELLOW

if row.get("id") in self.selected_row.get("send_to", []):
bold = self.ANSI_TEXT_BOLD
fg = self.ANSI_TEXT_YELLOW

if self.selected_row.get("id") in row.get("send_to", []):
fg = self.ANSI_TEXT_YELLOW
bold = self.ANSI_TEXT_BOLD

return self.ANSI_RESET + fg + bg + bold

rows = self.status["router"]

def get_sort_key(row):
Expand All @@ -450,7 +488,7 @@ def get_sort_key(row):
max_cols = self.max_cols - 1

def print_cell(row: dict, rown: int, coln: int):
self.print(self.get_color_cell(row, rown, coln))
self.print(get_color_cell(row, rown, coln))
column = self.COLUMNS[coln]
width = column["width"]
value = column["get"](row)
Expand All @@ -468,7 +506,7 @@ def print_row(row: dict, rown: int):
self.print("\n")

for coln, column in enumerate(self.COLUMNS):
self.print(self.get_color_cell({}, 0, coln))
self.print(get_color_cell({}, 0, coln))
width = column["width"]
value = column["name"][:width]
if column.get("align") == "right":
Expand Down Expand Up @@ -499,42 +537,6 @@ def print_row(row: dict, rown: int):
self.selected_row_index = self.max_rows - 1
self.selected_row = self.status["router"][self.selected_row_index]

def get_color_cell(self, row: dict, rown: int, coln: int):
if rown == 0:
if coln == self.selected_col_index:
return self.ANSI_BG_CYAN + self.ANSI_TEXT_WHITE + self.ANSI_TEXT_BOLD
return self.ANSI_BG_PURPLE + self.ANSI_TEXT_WHITE + self.ANSI_TEXT_BOLD

# we are in the table rows
rown -= 1
bg = self.ANSI_BG_BLACK
fg = self.ANSI_TEXT_WHITE

if rown == self.selected_row_index:
bg = self.ANSI_BG_WHITE
fg = self.ANSI_TEXT_BLACK

status = self.get_peer_status(row)
column = self.COLUMNS[coln]

bold = ""

if column["name"] == "Status":
if status == "CONNECTED":
fg = self.ANSI_TEXT_GREEN
elif status == "CONNECTING":
fg = self.ANSI_TEXT_YELLOW

if row.get("id") in self.selected_row.get("send_to", []):
bold = self.ANSI_TEXT_BOLD
fg = self.ANSI_TEXT_YELLOW

if self.selected_row.get("id") in row.get("send_to", []):
fg = self.ANSI_TEXT_YELLOW
bold = self.ANSI_TEXT_BOLD

return self.ANSI_RESET + fg + bg + bold

def print_header(self):
# write a header with the rtpmidid client, version, with color until the end of line
self.print(self.ANSI_BG_BLUE + self.ANSI_TEXT_BOLD + self.ANSI_TEXT_WHITE)
Expand Down

0 comments on commit 6dd6950

Please sign in to comment.