Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/commands/output.rs
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
use std::fmt::Display;
7
8
use std::io::{BufWriter, Write};
9
10
use super::args::OutputFormat;
11
12
pub struct Column {
13
max_width: usize,
14
heading: &'static str,
15
data: Vec<String>,
16
}
17
18
impl Column {
19
pub fn new(heading: &'static str) -> Self {
20
Column {
21
max_width: heading.len(),
22
heading,
23
data: vec![],
24
}
25
}
26
27
pub fn add_row(&mut self, row: String) {
28
self.max_width = std::cmp::max(self.max_width, row.len());
29
self.data.push(row);
30
}
31
}
32
33
impl OutputFormat {
34
pub fn print_table(&self, table: OutputTable) -> Result<(), std::io::Error> {
35
match *self {
36
OutputFormat::Json => JsonTablePrinter().print(table, &mut std::io::stdout()),
37
OutputFormat::Text => TextTablePrinter().print(table, &mut std::io::stdout()),
38
}
39
}
40
}
41
42
pub struct OutputTable {
43
cols: Vec<Column>,
44
}
45
46
impl OutputTable {
47
pub fn new(cols: Vec<Column>) -> Self {
48
OutputTable { cols }
49
}
50
}
51
52
trait TablePrinter {
53
fn print(&self, table: OutputTable, out: &mut dyn std::io::Write)
54
-> Result<(), std::io::Error>;
55
}
56
57
pub struct JsonTablePrinter();
58
59
impl TablePrinter for JsonTablePrinter {
60
fn print(
61
&self,
62
table: OutputTable,
63
out: &mut dyn std::io::Write,
64
) -> Result<(), std::io::Error> {
65
let mut bw = BufWriter::new(out);
66
bw.write_all(b"[")?;
67
68
if !table.cols.is_empty() {
69
let data_len = table.cols[0].data.len();
70
for i in 0..data_len {
71
if i > 0 {
72
bw.write_all(b",{")?;
73
} else {
74
bw.write_all(b"{")?;
75
}
76
for col in &table.cols {
77
serde_json::to_writer(&mut bw, col.heading)?;
78
bw.write_all(b":")?;
79
serde_json::to_writer(&mut bw, &col.data[i])?;
80
}
81
}
82
}
83
84
bw.write_all(b"]")?;
85
bw.flush()
86
}
87
}
88
89
/// Type that prints the output as an ASCII, markdown-style table.
90
pub struct TextTablePrinter();
91
92
impl TablePrinter for TextTablePrinter {
93
fn print(
94
&self,
95
table: OutputTable,
96
out: &mut dyn std::io::Write,
97
) -> Result<(), std::io::Error> {
98
let mut bw = BufWriter::new(out);
99
100
let sizes = table.cols.iter().map(|c| c.max_width).collect::<Vec<_>>();
101
102
// print headers
103
write_columns(&mut bw, table.cols.iter().map(|c| c.heading), &sizes)?;
104
// print --- separators
105
write_columns(
106
&mut bw,
107
table.cols.iter().map(|c| "-".repeat(c.max_width)),
108
&sizes,
109
)?;
110
// print each column
111
if !table.cols.is_empty() {
112
let data_len = table.cols[0].data.len();
113
for i in 0..data_len {
114
write_columns(&mut bw, table.cols.iter().map(|c| &c.data[i]), &sizes)?;
115
}
116
}
117
118
bw.flush()
119
}
120
}
121
122
fn write_columns<T>(
123
mut w: impl Write,
124
cols: impl Iterator<Item = T>,
125
sizes: &[usize],
126
) -> Result<(), std::io::Error>
127
where
128
T: Display,
129
{
130
w.write_all(b"|")?;
131
for (i, col) in cols.enumerate() {
132
write!(w, " {:width$} |", col, width = sizes[i])?;
133
}
134
w.write_all(b"\r\n")
135
}
136
137