|
1 | 1 | import io |
| 2 | +from collections import Counter |
2 | 3 |
|
3 | 4 | from rich import box |
4 | 5 | from rich.console import Console |
5 | 6 | from rich.table import Table |
6 | 7 |
|
7 | | -from datacontract.model.changelog import ChangelogResult, ChangelogType |
| 8 | +from datacontract.model.changelog import ChangelogEntry, ChangelogResult, ChangelogType |
8 | 9 |
|
9 | 10 | _VAL_W = 30 |
10 | 11 |
|
| 12 | +_CHANGE_COLOR = { |
| 13 | + ChangelogType.added: "green", |
| 14 | + ChangelogType.changed: "yellow", |
| 15 | + ChangelogType.removed: "red", |
| 16 | +} |
| 17 | + |
| 18 | +_BADGE_ORDER = [ChangelogType.added, ChangelogType.changed, ChangelogType.removed] |
| 19 | + |
11 | 20 |
|
12 | 21 | def write_text_changelog_results(result: ChangelogResult, console: Console): |
13 | 22 | _print_summary(result, console) |
14 | 23 | _print_table(result, console) |
15 | 24 |
|
16 | 25 |
|
17 | | -def _badges(entries: list) -> str: |
18 | | - removed = sum(1 for e in entries if e.type == ChangelogType.removed) |
19 | | - changed = sum(1 for e in entries if e.type == ChangelogType.changed) |
20 | | - added = sum(1 for e in entries if e.type == ChangelogType.added) |
| 26 | +def _badges(entries: list[ChangelogEntry]) -> str: |
| 27 | + counts = Counter(e.type for e in entries) |
21 | 28 | parts = [] |
22 | | - if removed: |
23 | | - parts.append(f"[ {removed} Removed ]") |
24 | | - if changed: |
25 | | - parts.append(f"[ {changed} Changed ]") |
26 | | - if added: |
27 | | - parts.append(f"[ {added} Added ]") |
| 29 | + for ct in _BADGE_ORDER: |
| 30 | + n = counts[ct] |
| 31 | + if n: |
| 32 | + color = _CHANGE_COLOR[ct] |
| 33 | + parts.append(f"[ [{color}]{n} {ct.value.capitalize()}[/{color}] ]") |
28 | 34 | return " ".join(parts) |
29 | 35 |
|
30 | 36 |
|
@@ -66,12 +72,9 @@ def _print_table(result: ChangelogResult, console: Console): |
66 | 72 |
|
67 | 73 |
|
68 | 74 | def _with_markup(changelog_type: ChangelogType) -> str: |
69 | | - if changelog_type == ChangelogType.added: |
70 | | - return "[green]added[/green]" |
71 | | - if changelog_type == ChangelogType.removed: |
72 | | - return "[red]removed[/red]" |
73 | | - if changelog_type == ChangelogType.changed: |
74 | | - return "[yellow]changed[/yellow]" |
| 75 | + color = _CHANGE_COLOR.get(changelog_type) |
| 76 | + if color: |
| 77 | + return f"[{color}]{changelog_type.value}[/{color}]" |
75 | 78 | return changelog_type.value |
76 | 79 |
|
77 | 80 |
|
|
0 commit comments