88 "strings"
99
1010 "github.com/charmbracelet/lipgloss"
11+ "github.com/charmbracelet/lipgloss/table"
1112
1213 "github.com/mfridman/tparse/internal/utils"
1314 "github.com/mfridman/tparse/parse"
@@ -44,16 +45,25 @@ type packageTests struct {
4445
4546func (c * consoleWriter ) testsTable (packages []* parse.Package , option TestTableOptions ) {
4647 // Print passed tests, sorted by elapsed DESC. Grouped by alphabetically sorted packages.
47- var tableString strings.Builder
48- tbl := newTableWriter (& tableString , c .format )
49-
48+ tbl := newTable (c .format , func (style lipgloss.Style , row , col int ) lipgloss.Style {
49+ switch row {
50+ case table .HeaderRow :
51+ default :
52+ if col == 2 || col == 3 {
53+ // Test name and package name
54+ style = style .Align (lipgloss .Left )
55+ }
56+ }
57+ return style
58+ })
5059 header := testRow {
5160 status : "Status" ,
5261 elapsed : "Elapsed" ,
5362 testName : "Test" ,
5463 packageName : "Package" ,
5564 }
56- tbl .SetHeader (header .toRow ())
65+ tbl .Headers (header .toRow ()... )
66+ data := table .NewStringData ()
5767
5868 names := make ([]string , 0 , len (packages ))
5969 for _ , pkg := range packages {
@@ -95,40 +105,40 @@ func (c *consoleWriter) testsTable(packages []*parse.Package, option TestTableOp
95105 testName : testName ,
96106 packageName : packageName ,
97107 }
98- tbl .Append (row .toRow ())
108+ data .Append (row .toRow ())
99109 }
100110 if i != (len (packages ) - 1 ) {
101- // TODO(mf): is it possible to add a custom separator with tablewriter instead of empty space?
102- tbl .Append (testRow {}.toRow ())
111+ // Add a blank row between packages.
112+ data .Append (testRow {}.toRow ())
103113 }
104114 }
105115
106- if tbl .NumLines () > 0 {
107- // The table gets written to a strings builder so we can further modify the output
108- // with lipgloss.
109- tbl .Render ()
110- output := tableString .String ()
111- if c .format == OutputFormatBasic {
112- output = lipgloss .NewStyle ().
113- Border (lipgloss .NormalBorder ()).
114- Render (strings .TrimSuffix (output , "\n " ))
115- }
116- fmt .Fprintln (c .w , output )
116+ if data .Rows () > 0 {
117+ fmt .Fprintln (c .w , tbl .Data (data ).Render ())
117118 }
118119}
119120
120121func (c * consoleWriter ) testsTableMarkdown (packages []* parse.Package , option TestTableOptions ) {
121122 for _ , pkg := range packages {
122123 // Print passed tests, sorted by elapsed DESC. Grouped by alphabetically sorted packages.
123- var tableString strings.Builder
124- tbl := newTableWriter (& tableString , c .format )
125-
124+ tbl := newTable (c .format , func (style lipgloss.Style , row , col int ) lipgloss.Style {
125+ switch row {
126+ case table .HeaderRow :
127+ default :
128+ if col == 2 {
129+ // Test name
130+ style = style .Align (lipgloss .Left )
131+ }
132+ }
133+ return style
134+ })
126135 header := []string {
127136 "Status" ,
128137 "Elapsed" ,
129138 "Test" ,
130139 }
131- tbl .SetHeader (header )
140+ tbl .Headers (header ... )
141+ data := table .NewStringData ()
132142
133143 // Discard packages where we cannot generate a sensible test summary.
134144 if pkg .NoTestFiles || pkg .NoTests || pkg .HasPanic {
@@ -154,30 +164,34 @@ func (c *consoleWriter) testsTableMarkdown(packages []*parse.Package, option Tes
154164 case parse .ActionFail :
155165 status = c .red (status )
156166 }
157- tbl .Append ([]string {
167+ data .Append ([]string {
158168 status ,
159169 strconv .FormatFloat (t .Elapsed (), 'f' , 2 , 64 ),
160170 testName ,
161171 })
162172 }
163- if tbl .NumLines () > 0 {
164- tbl .Render ()
165-
173+ if data .Rows () > 0 {
166174 fmt .Fprintf (c .w , "## 📦 Package **`%s`**\n " , pkg .Summary .Package )
167175 fmt .Fprintln (c .w )
168- fmt .Fprintf (c .w ,
169- "**%d passed** tests (out of %d) | **%d skipped** tests (out of %d)\n " ,
170- len (pkgTests .passed ),
176+
177+ msg := fmt .Sprintf ("Tests: ✓ %d passed | %d skipped\n " ,
171178 pkgTests .passedCount ,
172- len (pkgTests .skipped ),
173179 pkgTests .skippedCount ,
174180 )
181+ if option .Slow > 0 && option .Slow < pkgTests .passedCount {
182+ msg += fmt .Sprintf ("↓ Slowest %d passed tests shown (of %d)\n " ,
183+ option .Slow ,
184+ pkgTests .passedCount ,
185+ )
186+ }
187+ fmt .Fprint (c .w , msg )
188+
175189 fmt .Fprintln (c .w )
176190 fmt .Fprintln (c .w , "<details>" )
177191 fmt .Fprintln (c .w )
178192 fmt .Fprintln (c .w , "<summary>Click for test summary</summary>" )
179193 fmt .Fprintln (c .w )
180- fmt .Fprintln (c .w , tableString . String ())
194+ fmt .Fprintln (c .w , tbl . Data ( data ). Render ())
181195 fmt .Fprintln (c .w , "</details>" )
182196 fmt .Fprintln (c .w )
183197 }
0 commit comments