Skip to content

Commit de978e1

Browse files
authored
[wasm-metadata] pretty-print cli output (#1946)
* [wasm-metadata] pretty-print cli output * disable crossterm dep * rebase on main * update snapshot tests * Move print logic to cli tools
1 parent 1bcecbc commit de978e1

File tree

11 files changed

+216
-93
lines changed

11 files changed

+216
-93
lines changed

Cargo.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ anyhow = "1.0.58"
7474
arbitrary = "1.1.0"
7575
clap = { version = "4.0.0", features = ["derive"] }
7676
clap_complete = "4.4.7"
77+
comfy-table = { version = "7.1.3", default-features = false }
7778
criterion = { version = "0.5.1", default-features = false }
7879
env_logger = "0.11"
7980
indexmap = { version = "2.0.0", default-features = false }
@@ -122,6 +123,7 @@ env_logger = { workspace = true }
122123
log = { workspace = true }
123124
clap = { workspace = true, features = ['wrap_help'] }
124125
clap_complete = { workspace = true, optional = true }
126+
comfy-table = { workspace = true }
125127
tempfile = "3.2.0"
126128
wat = { workspace = true, features = ['dwarf', 'component-model'] }
127129
termcolor = { workspace = true }

crates/wasm-metadata/src/payload.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt::{self, Display};
21
use std::ops::Range;
32

43
use anyhow::Result;
@@ -181,48 +180,4 @@ impl Payload {
181180
Self::Component { children, .. } => children.push(child),
182181
}
183182
}
184-
185-
fn display(&self, f: &mut fmt::Formatter, indent: usize) -> fmt::Result {
186-
let spaces = std::iter::repeat(" ").take(indent).collect::<String>();
187-
match self {
188-
Self::Module(Metadata {
189-
name, producers, ..
190-
}) => {
191-
if let Some(name) = name {
192-
writeln!(f, "{spaces}module {name}:")?;
193-
} else {
194-
writeln!(f, "{spaces}module:")?;
195-
}
196-
if let Some(producers) = producers {
197-
producers.display(f, indent + 4)?;
198-
}
199-
Ok(())
200-
}
201-
Self::Component {
202-
children,
203-
metadata: Metadata {
204-
name, producers, ..
205-
},
206-
} => {
207-
if let Some(name) = name {
208-
writeln!(f, "{spaces}component {name}:")?;
209-
} else {
210-
writeln!(f, "{spaces}component:")?;
211-
}
212-
if let Some(producers) = producers {
213-
producers.display(f, indent + 4)?;
214-
}
215-
for c in children {
216-
c.display(f, indent + 4)?;
217-
}
218-
Ok(())
219-
}
220-
}
221-
}
222-
}
223-
224-
impl Display for Payload {
225-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226-
self.display(f, 0)
227-
}
228183
}

crates/wasm-metadata/src/producers.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::Result;
22
use indexmap::{map::Entry, IndexMap};
33
use serde_derive::Serialize;
4-
use std::fmt;
54
use wasm_encoder::Encode;
65
use wasmparser::{BinaryReader, KnownCustom, Parser, ProducersSectionReader};
76

@@ -150,27 +149,6 @@ impl Producers {
150149
pub fn add_to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
151150
rewrite_wasm(&None, self, &None, &None, &None, &None, &None, input)
152151
}
153-
154-
pub(crate) fn display(&self, f: &mut fmt::Formatter, indent: usize) -> fmt::Result {
155-
let indent = std::iter::repeat(" ").take(indent).collect::<String>();
156-
for (fieldname, fieldvalues) in self.0.iter() {
157-
writeln!(f, "{indent}{fieldname}:")?;
158-
for (name, version) in fieldvalues {
159-
if version.is_empty() {
160-
writeln!(f, "{indent} {name}")?;
161-
} else {
162-
writeln!(f, "{indent} {name}: {version}")?;
163-
}
164-
}
165-
}
166-
Ok(())
167-
}
168-
}
169-
170-
impl fmt::Display for Producers {
171-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
172-
self.display(f, 0)
173-
}
174152
}
175153

176154
/// Contents of a producers field

src/bin/wasm-tools/metadata.rs

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use anyhow::Result;
21
use std::io::Write;
32

3+
use anyhow::Result;
4+
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
5+
use comfy_table::presets::UTF8_FULL;
6+
use comfy_table::{ContentArrangement, Table};
7+
use termcolor::WriteColor;
8+
use wasm_metadata::{Metadata, Payload};
9+
410
/// Manipulate metadata (module name, producers) to a WebAssembly file.
511
#[derive(clap::Parser)]
612
pub enum Opts {
@@ -44,11 +50,11 @@ impl ShowOpts {
4450
let input = self.io.parse_input_wasm()?;
4551
let mut output = self.io.output_writer()?;
4652

47-
let metadata = wasm_metadata::Payload::from_binary(&input)?;
53+
let payload = wasm_metadata::Payload::from_binary(&input)?;
4854
if self.json {
49-
write!(output, "{}", serde_json::to_string(&metadata)?)?;
55+
write!(output, "{}", serde_json::to_string(&payload)?)?;
5056
} else {
51-
write!(output, "{metadata}")?;
57+
fmt_payload(&payload, &mut output)?;
5258
}
5359
Ok(())
5460
}
@@ -82,3 +88,75 @@ impl AddOpts {
8288
Ok(())
8389
}
8490
}
91+
92+
fn fmt_payload(payload: &Payload, f: &mut Box<dyn WriteColor>) -> Result<()> {
93+
let mut table = Table::new();
94+
table
95+
.load_preset(UTF8_FULL)
96+
.apply_modifier(UTF8_ROUND_CORNERS)
97+
.set_content_arrangement(ContentArrangement::Dynamic)
98+
.set_width(80)
99+
.set_header(vec!["KIND", "VALUE"]);
100+
let Metadata {
101+
name,
102+
author,
103+
description,
104+
producers,
105+
licenses,
106+
source,
107+
homepage,
108+
range,
109+
} = payload.metadata();
110+
111+
// Print the basic information
112+
let kind = match payload {
113+
Payload::Component { .. } => "component",
114+
Payload::Module(_) => "module",
115+
};
116+
table.add_row(vec!["kind", &kind]);
117+
let name = name.as_deref().unwrap_or("<unknown>");
118+
table.add_row(vec!["name", &name]);
119+
table.add_row(vec![
120+
"range",
121+
&format!("0x{:x}..0x{:x}", range.start, range.end),
122+
]);
123+
124+
// Print the OCI annotations
125+
if let Some(description) = description {
126+
table.add_row(vec!["description", &description.to_string()]);
127+
}
128+
if let Some(licenses) = licenses {
129+
table.add_row(vec!["licenses", &licenses.to_string()]);
130+
}
131+
if let Some(author) = author {
132+
table.add_row(vec!["author", &author.to_string()]);
133+
}
134+
if let Some(source) = source {
135+
table.add_row(vec!["source", &source.to_string()]);
136+
}
137+
if let Some(homepage) = homepage {
138+
table.add_row(vec!["homepage", &homepage.to_string()]);
139+
}
140+
141+
if let Some(producers) = producers {
142+
for (name, pairs) in producers.iter() {
143+
for (field, version) in pairs.iter() {
144+
match version.len() {
145+
0 => table.add_row(vec![name, &format!("{field}")]),
146+
_ => table.add_row(vec![name, &format!("{field} [{version}]")]),
147+
};
148+
}
149+
}
150+
}
151+
152+
// Write the table to the writer
153+
writeln!(f, "{table}")?;
154+
155+
if let Payload::Component { children, .. } = payload {
156+
for payload in children {
157+
fmt_payload(payload, f)?;
158+
}
159+
}
160+
161+
Ok(())
162+
}
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
module:
2-
language:
3-
foo: 3
4-
bar: 1
5-
sdk:
6-
foo: 2
1+
╭──────────┬───────────╮
2+
│ KIND ┆ VALUE │
3+
╞══════════╪═══════════╡
4+
│ kind ┆ module │
5+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
6+
│ name ┆ <unknown> │
7+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
8+
│ range ┆ 0x0..0x36 │
9+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
10+
│ language ┆ foo [3] │
11+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
12+
│ language ┆ bar [1] │
13+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
14+
│ sdk ┆ foo [2] │
15+
╰──────────┴───────────╯
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
module foo:
1+
╭───────┬───────────╮
2+
│ KIND ┆ VALUE │
3+
╞═══════╪═══════════╡
4+
│ kind ┆ module │
5+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
6+
│ name ┆ foo │
7+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
8+
│ range ┆ 0x0..0x15 │
9+
╰───────┴───────────╯

tests/cli/add-metadata.wat.stdout

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
module foo:
2-
language:
3-
bar: 1
4-
processed-by:
5-
baz: 1
6-
sdk:
7-
my-sdk: 2
1+
╭──────────────┬────────────╮
2+
│ KIND ┆ VALUE │
3+
╞══════════════╪════════════╡
4+
│ kind ┆ module │
5+
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
6+
│ name ┆ foo │
7+
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
8+
│ range ┆ 0x0..0x54 │
9+
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
10+
│ language ┆ bar [1] │
11+
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
12+
│ processed-by ┆ baz [1] │
13+
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
14+
│ sdk ┆ my-sdk [2] │
15+
╰──────────────┴────────────╯
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
component foo:
2-
language:
3-
foo: 1
4-
module:
1+
╭──────────┬───────────╮
2+
│ KIND ┆ VALUE │
3+
╞══════════╪═══════════╡
4+
│ kind ┆ component │
5+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
6+
│ name ┆ foo │
7+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
8+
│ range ┆ 0x0..0x65 │
9+
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
10+
│ language ┆ foo [1] │
11+
╰──────────┴───────────╯
12+
╭───────┬───────────╮
13+
│ KIND ┆ VALUE │
14+
╞═══════╪═══════════╡
15+
│ kind ┆ module │
16+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
17+
│ name ┆ <unknown> │
18+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
19+
│ range ┆ 0xa..0x31 │
20+
╰───────┴───────────╯
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1-
component my-name:
2-
module submodule:
3-
module another submodule:
1+
╭───────┬───────────╮
2+
│ KIND ┆ VALUE │
3+
╞═══════╪═══════════╡
4+
│ kind ┆ component │
5+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
6+
│ name ┆ my-name │
7+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
8+
│ range ┆ 0x0..0x88 │
9+
╰───────┴───────────╯
10+
╭───────┬───────────╮
11+
│ KIND ┆ VALUE │
12+
╞═══════╪═══════════╡
13+
│ kind ┆ module │
14+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
15+
│ name ┆ submodule │
16+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
17+
│ range ┆ 0xa..0x25 │
18+
╰───────┴───────────╯
19+
╭───────┬───────────────────╮
20+
│ KIND ┆ VALUE │
21+
╞═══════╪═══════════════════╡
22+
│ kind ┆ module │
23+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
24+
│ name ┆ another submodule │
25+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
26+
│ range ┆ 0x27..0x4a │
27+
╰───────┴───────────────────╯

0 commit comments

Comments
 (0)