Skip to content

Commit 6cef52e

Browse files
committed
Refactor CSV writing
1 parent 9a535a0 commit 6cef52e

1 file changed

Lines changed: 16 additions & 38 deletions

File tree

src/SeqCli/Csv/CsvWriter.cs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,29 @@ public static void WriteQueryResult(QueryResultPart result, ConsoleTheme theme,
2121
var first = true;
2222
QueryResultHelper.Flatten(result, row =>
2323
{
24-
if (first)
24+
var firstCol = true;
25+
foreach (var value in row)
2526
{
26-
first = false;
27-
var firstCol = true;
28-
foreach (var heading in row)
29-
{
30-
if (firstCol)
31-
{
32-
firstCol = false;
33-
}
34-
else
35-
{
36-
theme.Set(output, ConsoleThemeStyle.TertiaryText);
37-
output.Write(',');
38-
theme.Reset(output);
39-
}
40-
41-
WriteCell(output, theme, heading, isHeadingRow: true);
42-
}
43-
}
44-
else
45-
{
46-
var firstCol = true;
47-
foreach (var value in row)
48-
{
49-
if (firstCol)
50-
{
51-
firstCol = false;
52-
}
53-
else
54-
{
55-
theme.Set(output, ConsoleThemeStyle.TertiaryText);
56-
output.Write(',');
57-
theme.Reset(output);
58-
}
59-
60-
WriteCell(output, theme, value);
61-
}
27+
WriteCell(output, theme, value, ref firstCol, isHeadingRow: first);
6228
}
29+
first = false;
6330
output.WriteLine();
6431
});
6532
}
6633

67-
static void WriteCell(TextWriter output, ConsoleTheme theme, object? value, bool isHeadingRow = false)
34+
static void WriteCell(TextWriter output, ConsoleTheme theme, object? value, ref bool firstCol, bool isHeadingRow = false)
6835
{
36+
if (firstCol)
37+
{
38+
firstCol = false;
39+
}
40+
else
41+
{
42+
theme.Set(output, ConsoleThemeStyle.TertiaryText);
43+
output.Write(',');
44+
theme.Reset(output);
45+
}
46+
6947
theme.Set(output, ConsoleThemeStyle.TertiaryText);
7048
output.Write('"');
7149
theme.Reset(output);

0 commit comments

Comments
 (0)