44import typer
55from rich import print
66from rich .console import Console
7+ from rich .text import Text
78
89from codelimit .common .ScanResultTable import ScanResultTable
910from codelimit .common .ScanTotals import ScanTotals
@@ -22,15 +23,16 @@ class ReportFormat(str, Enum):
2223
2324
2425def report_command (path : Path , full : bool , totals : bool , fmt : ReportFormat ):
26+ stdout = Console ()
2527 report = read_report (path )
2628 if totals :
2729 scan_totals = ScanTotals (report .codebase .totals )
2830 if fmt == ReportFormat .markdown :
29- print (_report_totals_markdown (scan_totals ))
31+ stdout . print (_report_totals_markdown (scan_totals ), soft_wrap = True )
3032 else :
31- print (ScanResultTable (scan_totals ))
33+ stdout . print (ScanResultTable (scan_totals ), soft_wrap = True )
3234 else :
33- _report_units (report , path , full , fmt )
35+ _report_functions (report , path , full , fmt , stdout )
3436
3537
3638def _report_totals_markdown (st : ScanTotals ) -> str :
@@ -60,10 +62,10 @@ def _report_totals_markdown(st: ScanTotals) -> str:
6062 return result
6163
6264
63- def _report_units (report : Report , path : Path , full : bool , fmt ):
65+ def _report_functions (report : Report , path : Path , full : bool , fmt , console : Console ):
6466 units = report .all_report_units_sorted_by_length_asc (30 )
6567 if len (units ) == 0 :
66- print (
68+ console . print (
6769 "[bold]Refactoring not necessary, :sparkles: happy coding! :sparkles:[/bold]"
6870 )
6971 return
@@ -73,9 +75,9 @@ def _report_units(report: Report, path: Path, full: bool, fmt):
7375 report_units = units [0 :REPORT_LENGTH ]
7476 root = get_root (path )
7577 if fmt == ReportFormat .markdown :
76- print (_print_functions_markdown (root , report_units ))
78+ console . print (_report_functions_markdown (root , report_units ), soft_wrap = True )
7779 else :
78- _print_functions_text ( root , units , report_units , full )
80+ console . print ( _report_functions_text ( root , units , report_units , full ), soft_wrap = True )
7981
8082
8183def get_root (path : Path ) -> Path | None :
@@ -101,20 +103,17 @@ def read_report(path: Path) -> Report:
101103 return ReportReader .from_json (report_data )
102104
103105
104- def _print_functions_text (root , units , report_units , full ):
105- stdout = Console ()
106+ def _report_functions_text (root , units , report_units , full ) -> Text :
107+ result = Text ()
106108 for unit in report_units :
107109 file_path = unit .file if root is None else root .joinpath (unit .file )
108- stdout .print (
109- format_measurement (str (file_path ), unit .measurement ), soft_wrap = True
110- )
110+ result .append (format_measurement (str (file_path ), unit .measurement ).append ("\n " ))
111111 if not full and len (units ) > REPORT_LENGTH :
112- print (
113- f"[bold]{ len (units ) - REPORT_LENGTH } more rows, use --full option to get all rows[/bold]"
114- )
112+ result .append (f"[bold]{ len (units ) - REPORT_LENGTH } more rows, use --full option to get all rows[/bold]\n " )
113+ return result
115114
116115
117- def _print_functions_markdown (root : Path | None , report_units : list [ReportUnit ]) -> str :
116+ def _report_functions_markdown (root : Path | None , report_units : list [ReportUnit ]) -> str :
118117 result = ""
119118 result += "| **File** | **Line** | **Column** | **Length** | **Function** |\n "
120119 result += "| --- | ---: | ---: | ---: | --- |\n "
0 commit comments