diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-04-11 13:50:07 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-04-12 12:41:51 +0200 |
commit | b8742923d134c5c3a91a2521556ecba2085cece7 (patch) | |
tree | 02179ee4cedd951c4633c21e43771faa91cfb038 | |
parent | 24c637f1d665c1dfeafc05f214ab33c9a7f26e8a (diff) |
tool: Improve packet dumper.
- Indent the hexdumps, but don't exceed a target width.
-rw-r--r-- | tool/src/commands/decrypt.rs | 2 | ||||
-rw-r--r-- | tool/src/commands/dump.rs | 37 |
2 files changed, 28 insertions, 11 deletions
diff --git a/tool/src/commands/decrypt.rs b/tool/src/commands/decrypt.rs index cd07e624..0b7e33f7 100644 --- a/tool/src/commands/decrypt.rs +++ b/tool/src/commands/decrypt.rs @@ -77,7 +77,7 @@ impl<'a> Helper<'a> { key_hints: hints, dump_session_key: dump_session_key, dumper: if dump || hex { - Some(PacketDumper::new(false)) + Some(PacketDumper::new(80, false)) } else { None }, diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs index d5601dd8..d5c5725f 100644 --- a/tool/src/commands/dump.rs +++ b/tool/src/commands/dump.rs @@ -19,7 +19,7 @@ pub fn dump(input: &mut io::Read, output: &mut io::Write, mpis: bool, hex: bool, let mut ppr = openpgp::parse::PacketParserBuilder::from_reader(input)? .map(hex).finalize()?; - let mut dumper = PacketDumper::new(mpis); + let mut dumper = PacketDumper::new(80, mpis); while let PacketParserResult::Some(mut pp) = ppr { let additional_fields = match pp.packet { @@ -94,11 +94,6 @@ pub fn dump(input: &mut io::Read, output: &mut io::Write, mpis: bool, hex: bool, dumper.flush(output) } -pub struct PacketDumper { - mpis: bool, - root: Option<Node>, -} - struct Node { header: Header, packet: Packet, @@ -128,9 +123,16 @@ impl Node { } } +pub struct PacketDumper { + width: usize, + mpis: bool, + root: Option<Node>, +} + impl PacketDumper { - pub fn new(mpis: bool) -> Self { + pub fn new(width: usize, mpis: bool) -> Self { PacketDumper { + width: width, mpis: mpis, root: None, } @@ -411,13 +413,28 @@ impl PacketDumper { } if let Some(map) = map { - writeln!(output)?; - let mut hd = HexDumper::new(output, ""); + let hd_indent = ::std::cmp::max( + 0, + ::std::cmp::min( + self.width as isize + - 63 // Length of address, hex digits, and whitespace. + - map.iter().map(|f| f.name.len()).max() + .expect("we always have one entry") as isize, + i.len() as isize), + ) as usize; + + writeln!(output, "{}", i)?; + let mut hd = HexDumper::new( + output, + format!("{} ", + &i.chars().take(hd_indent).collect::<String>())); + for field in map.iter() { hd.write(field.data, field.name)?; } + let output = hd.into_inner(); - writeln!(output)?; + writeln!(output, "{}", i)?; } else { writeln!(output, "{}", i)?; } |