Skip to content

Commit 0c18aa0

Browse files
authored
Simplify and colorize images replicate table (#659)
1 parent a25b388 commit 0c18aa0

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

linodecli/overrides.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@
1111
from rich.align import Align
1212
from rich.console import Console
1313
from rich.table import Table
14+
from rich.theme import Theme
1415

1516
from linodecli.output.output_handler import OutputMode
1617

1718
OUTPUT_OVERRIDES = {}
1819

20+
REPLICA_STATUS_THEME = {
21+
"available": "bright_green",
22+
"creating": "bright_yellow",
23+
"pending": "yellow",
24+
"pending replication": "yellow",
25+
"pending deletion": "red",
26+
"replicating": "bright_yellow",
27+
}
28+
1929

2030
def output_override(command: str, action: str, output_mode: OutputMode):
2131
"""
@@ -157,7 +167,12 @@ def build_replicas_output(replicas: List) -> Table:
157167
for replica in replicas:
158168
row = []
159169
for h in replicas_headers:
160-
row.append(Align(str(replica[h]), align="left"))
170+
if h == "status" and replica[h] in REPLICA_STATUS_THEME:
171+
row.append(
172+
Align(str(replica[h]), align="left", style=replica[h])
173+
)
174+
else:
175+
row.append(Align(str(replica[h]), align="left"))
161176
replicas_output.add_row(*row)
162177

163178
return replicas_output
@@ -167,24 +182,27 @@ def image_replicate_output(json_data) -> bool:
167182
"""
168183
Parse and format the image replicate output table.
169184
"""
185+
console = Console(theme=Theme(REPLICA_STATUS_THEME))
186+
170187
output = Table(
171188
header_style="bold",
172189
show_lines=True,
173190
)
174191

175192
row = []
176-
for header in json_data.keys():
177-
if header == "regions" and len(json_data[header]) > 0:
178-
# leverage `replicas` in output for readability
179-
output.add_column("replicas", justify="center")
180-
row.append(build_replicas_output(json_data[header]))
181-
elif json_data[header] is not None:
182-
output.add_column(header, justify="center")
183-
row.append(Align(str(json_data[header]), align="left"))
193+
headers = ["id", "label", "status", "total_size", "regions"]
194+
for header in headers:
195+
if header in json_data:
196+
if header == "regions" and len(json_data[header]) > 0:
197+
# leverage `replicas` in output for readability
198+
output.add_column("replicas", justify="center")
199+
row.append(build_replicas_output(json_data[header]))
200+
elif json_data[header] is not None:
201+
output.add_column(header, justify="center")
202+
row.append(Align(str(json_data[header]), align="left"))
184203

185204
output.add_row(*row)
186205

187-
console = Console()
188206
console.print(output)
189207

190208
return False

0 commit comments

Comments
 (0)