Skip to content

Instantly share code, notes, and snippets.

@BartMassey
Created September 7, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BartMassey/6023817d3fcb91a1547354f37098b679 to your computer and use it in GitHub Desktop.
Save BartMassey/6023817d3fcb91a1547354f37098b679 to your computer and use it in GitHub Desktop.
use std::fmt::{self, Display, Formatter};
use std::io::{self, Write};
use tabwriter::TabWriter;
struct Table([[usize; 2]; 2]);
impl Display for Table {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let write_table = || -> io::Result<Vec<u8>> {
let table = Vec::new();
let mut tf = TabWriter::new(table).padding(1);
for r in self.0.iter() {
let mut ci = r.iter();
if let Some(c0) = ci.next() {
write!(tf, "{}", c0)?;
for c in ci {
write!(tf, "\t{}", c)?;
}
}
writeln!(tf)?;
}
tf.flush().unwrap();
Ok(tf.into_inner().unwrap())
};
let table = write_table().or(Err(fmt::Error))?;
write!(f, "{}", std::str::from_utf8(&table).unwrap())
}
}
fn main() {
let table = Table([[0, 1], [2, 3]]);
print!("{}", table);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment