1+ using LocalCoverage: PackageCoverage, format_line
2+ using PrettyTables: PrettyTables, pretty_table
3+
4+ """
5+ WrappedPackageCoverage(summary::PackageCoverage)
6+
7+ Structure wrapping the `PackageCoverage` struct to add a custom `show` method for fixing PrettyTables issues until https://github.com/JuliaCI/LocalCoverage.jl/pull/68 is merged.
8+ """
9+ struct WrappedPackageCoverage
10+ summary:: PackageCoverage
11+ end
12+
13+ function Base. show (io:: IO , wrapped:: WrappedPackageCoverage )
14+ (; summary) = wrapped
15+ (; files, package_dir) = summary
16+ row_data = map (format_line, files)
17+ push! (row_data, format_line (summary))
18+ row_coverage = map (x -> x. coverage_percentage, row_data)
19+ rows = map (row_data) do row
20+ (; name, total, hit, missed, coverage_percentage, gaps) = row
21+ percentage = isnan (coverage_percentage) ? " -" : " $(round (Int, coverage_percentage)) %"
22+ (; name, total, hit, missed, percentage, gaps)
23+ end
24+ header = [" Filename" , " Lines" , " Hit" , " Miss" , " %" ]
25+ percentage_column = length (header)
26+ alignment = [:l , :r , :r , :r , :r ]
27+ columns_width = fill (- 1 , 5 ) # We need strictly negative number to autosize in PrettyTables 3.0, but this also works in v2
28+ if get (io, :print_gaps , false )
29+ push! (header, " Gaps" )
30+ push! (alignment, :l )
31+ display_cols = last (get (io, :displaysize , 100 ))
32+ push! (columns_width, display_cols - 45 )
33+ else
34+ rows = map (row -> Base. structdiff (row, NamedTuple{(:gaps ,)}), rows)
35+ end
36+ # PrettyTables 3.0 changed Highlighter to TextHighlighter, which up to currently published version (v3.10) does not provide the kwargs constructor (despite having it documented). We create here a patch to handle both cases
37+ Highlighter (f; kwargs... ) = @static if pkgversion (PrettyTables) < v " 3.0.0"
38+ PrettyTables. Highlighter (f; kwargs... )
39+ else
40+ PrettyTables. TextHighlighter (f, PrettyTables. Crayon (;kwargs... ))
41+ end
42+
43+ highlighters = (
44+ Highlighter (
45+ (data, i, j) -> j == percentage_column && row_coverage[i] <= 50 ,
46+ bold = true ,
47+ foreground = :red ,
48+ ),
49+ Highlighter ((data, i, j) -> j == percentage_column && row_coverage[i] <= 70 ,
50+ foreground = :yellow ),
51+ Highlighter ((data, i, j) -> j == percentage_column && row_coverage[i] >= 90 ,
52+ foreground = :green ),
53+ )
54+
55+ # Kwargs of `pretty_table` itself also changed in PrettyTables 3.0, so we have to branch here as well
56+ @static if pkgversion (PrettyTables) < v " 3.0.0"
57+ pretty_table (
58+ io,
59+ rows;
60+ title = " Coverage of $(package_dir) " ,
61+ header,
62+ alignment,
63+ crop = :none ,
64+ linebreaks = true ,
65+ columns_width,
66+ autowrap = true ,
67+ highlighters,
68+ body_hlines = [length (rows) - 1 ],
69+ )
70+ else
71+ pretty_table (
72+ io,
73+ rows;
74+ title = " Coverage of $(package_dir) " ,
75+ column_labels = [header],
76+ alignment,
77+ # The crop kwarg is not present anymore, split into the next two ones
78+ fit_table_in_display_horizontally = false ,
79+ fit_table_in_display_vertically = false ,
80+ line_breaks = true ,
81+ fixed_data_column_widths = columns_width,
82+ auto_wrap = true ,
83+ highlighters = collect (highlighters), # v3 expects a vector instead of a Tuple
84+ table_format = PrettyTables. TextTableFormat (;
85+ horizontal_lines_at_data_rows = [length (rows) - 1 ],
86+ ),
87+ )
88+ end
89+ end
0 commit comments