Repository: py-cov-action/python-coverage-comment-action (python)

Issue:
Long numbers are reducing readability
![image](https://github.com/py-cov-action/python-coverage-comment-action/assets/1457576/6a0d81e1-d1f8-4ac1-a307-549650a5af20)

We should probably abbreviate large numbers:
- 1 to 999: unchanged (all digits)
- 1_000 to 9_999: 1.0k to 9.9k (1 decimal digit, truncated)
- 10_000 to 999_999: 10k to 999k (no decimal digit, truncated)
- 1_000_000 and more: 1M / 10M / 100M / 1000M etc (no decimal digit, truncated, no upper limit on numbers)

Note: it's worse with long filenames:
![image](https://github.com/py-cov-action/python-coverage-comment-action/assets/1457576/dfeec9b8-e354-45e9-a170-d3a2a937ba63)

Maybe we should show just the start and end of filenames if they're too long


Pull request description:
Abbreviate large numbers
Closes #372 

Patch:
diff --git a/coverage_comment/template.py b/coverage_comment/template.py
index 7f682aa..ed680cf 100644
--- a/coverage_comment/template.py
+++ b/coverage_comment/template.py
@@ -74,6 +74,16 @@ def delta(val: int) -> str:
     return f"({sign(val)}{val})"
 
 
+def compact(val: int) -> str:
+    if val < 1_000:
+        return str(val)
+    if val < 10_000:
+        return f"{val / 1_000:.1f}k"
+    if val < 1_000_000:
+        return f"{val / 1_000:.0f}k"
+    return f"{val / 1_000_000:.0f}M"
+
+
 def remove_exponent(val: decimal.Decimal) -> decimal.Decimal:
     # From https://docs.python.org/3/library/decimal.html#decimal-faq
     return (
@@ -136,6 +146,7 @@ def get_comment_markdown(
     env.filters["get_evolution_color"] = badge.get_evolution_badge_color
     env.filters["generate_badge"] = badge.get_static_badge_url
     env.filters["pluralize"] = pluralize
+    env.filters["compact"] = compact
     env.filters["file_url"] = functools.partial(
         get_file_url, repo_name=repo_name, pr_number=pr_number
     )
diff --git a/coverage_comment/template_files/comment.md.j2 b/coverage_comment/template_files/comment.md.j2
index 6fb2fcf..53816f9 100644
--- a/coverage_comment/template_files/comment.md.j2
+++ b/coverage_comment/template_files/comment.md.j2
@@ -45,7 +45,7 @@
 {% set color = "007ec6" -%}
 {% set message = statements_count %}
 {% endif -%}
-<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ statements_count | string | generate_badge(message=(message | delta), color=color) }}"></a></td>
+<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ statements_count | compact | generate_badge(message=(message | delta), color=color) }}"></a></td>
 
 {%- endmacro -%}
 
@@ -65,7 +65,7 @@
 {%- set message = missing_lines_count -%}
 {%- endif -%}
 {%- set color = message | get_evolution_color(up_is_good=false) -%}
-<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ missing_lines_count | string | generate_badge(message=(message | delta), color=color) }}"></a></td>
+<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ missing_lines_count | compact | generate_badge(message=(message | delta), color=color) }}"></a></td>
 
 {%- endmacro -%}
 
@@ -80,10 +80,10 @@
 {%- set text = "This PR doesn't change the coverage rate in " ~ path ~ ", which is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ ")." -%}
 {%- endif -%}
 {%- set color = coverage_diff | get_evolution_color() -%}
-{%- set message = "(" ~ previous_covered_statements_count ~ "/" ~ previous_statements_count ~ " > " ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%}
+{%- set message = "(" ~ previous_covered_statements_count | compact ~ "/" ~ previous_statements_count | compact ~ " > " ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%}
 {%- else -%}
 {%- set text = "The coverage rate of " ~ path ~ " is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ "). The file did not seem to exist on the base branch." -%}
-{%- set message = "(" ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%}
+{%- set message = "(" ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%}
 {%- set color = percent_covered | x100 | get_badge_color -%}
 {%- endif -%}
 <td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ percent_covered | pct(precision=0) | generate_badge(message=message, color=color) }}"></a></td>
@@ -94,7 +94,7 @@
 {% if added_statements_count -%}
 {% set text = "In this PR, " ~ (added_statements_count) ~ " new statements are added to " ~ path ~ ", " ~ covered_statements_count ~ " of which are covered (" ~ (percent_covered | pct) ~ ")." -%}
 {% set label = (percent_covered | pct(precision=0)) -%}
-{% set message = "(" ~ covered_statements_count ~ "/" ~ added_statements_count ~ ")" -%}
+{% set message = "(" ~ covered_statements_count | compact ~ "/" ~ added_statements_count | compact ~ ")" -%}
 {%- set color = (percent_covered | x100 | get_badge_color()) -%}
 {% else -%}
 {% set text = "This PR does not seem to add statements to " ~ path ~ "." -%}


Test patch:
diff --git a/tests/unit/test_template.py b/tests/unit/test_template.py
index 1e60c18..6282042 100644
--- a/tests/unit/test_template.py
+++ b/tests/unit/test_template.py
@@ -836,3 +836,20 @@ def test_get_log_message():
         subproject_id="foo",
     )
     assert result.startswith("Coverage info for foo:")
+
+
+@pytest.mark.parametrize(
+    "value, expected",
+    [
+        (0, "0"),
+        (1, "1"),
+        (999, "999"),
+        (1_042, "1.0k"),
+        (9_900, "9.9k"),
+        (12_345, "12k"),
+        (999_999, "1000k"),
+        (1_234_567, "1M"),
+    ],
+)
+def test_compact(value, expected):
+    assert template.compact(value) == expected