-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
162 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! Render a multi-line string as a table. | ||
//! | ||
//! Rows as separated by `\n`, and columns by `\t`. | ||
|
||
use std::io::{self, Write}; | ||
use std::{mem, str}; | ||
use unicode_width::UnicodeWidthStr; | ||
|
||
/// Padding between columns. | ||
const PADDING: usize = 2; | ||
|
||
pub struct TableWriter<T> { | ||
output: T, | ||
contents: Vec<u8>, | ||
} | ||
|
||
impl<T> TableWriter<T> { | ||
pub fn new(output: T) -> Self { | ||
TableWriter { | ||
output, | ||
contents: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Write> Write for TableWriter<T> { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
self.contents.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
let contents = mem::take(&mut self.contents); | ||
let lines: Vec<&str> = match str::from_utf8(&contents) { | ||
Ok(c) => c.trim_end().split('\n').collect(), | ||
|
||
Err(_) => return self.output.write_all(&contents), | ||
}; | ||
|
||
// Compute the width for every column. | ||
let mut widths = Vec::new(); | ||
|
||
for line in &lines { | ||
for (n, column) in line.split('\t').enumerate() { | ||
let width = UnicodeWidthStr::width(column) + PADDING; | ||
match widths.get_mut(n) { | ||
Some(col) if *col < width => *col = width, | ||
None => widths.push(width), | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
// The last column does not need the width value. | ||
if let Some(last) = widths.last_mut() { | ||
*last = 0; | ||
} | ||
|
||
// Print table. | ||
for line in &lines { | ||
for (column, width) in line.split('\t').zip(&widths) { | ||
write!(self.output, "{:1$}", column, width)?; | ||
} | ||
|
||
self.output.write_all(&[b'\n'])?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn render_test() { | ||
let mut buf = vec![]; | ||
let mut table = TableWriter::new(&mut buf); | ||
|
||
write!(&mut table, "aaa\tb\tcc\na\t\tc\na\tbbbb\na\tb\tcccc").unwrap(); | ||
table.flush().unwrap(); | ||
|
||
assert_eq!( | ||
String::from_utf8(buf).unwrap(), | ||
"aaa b cc\n\ | ||
a c\n\ | ||
a bbbb \n\ | ||
a b cccc\n" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Test to use the shell session after removing the builtin. | ||
|
||
load_builtin | ||
|
||
/bin/true 1 | ||
/bin/true 2 | ||
|
||
timehistory -s header=true -s table=true | ||
|
||
ASSERT_OUTPUT \ | ||
"timehistory -f '%n\t%C'" \ | ||
<<-ITEMS | ||
NUMBER COMMAND | ||
1 /bin/true 1 | ||
2 /bin/true 2 | ||
ITEMS |