1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#![doc(html_root_url = "https://docs.rs/tabular/0.1.2")] //! Builds plain, automatically-aligned tables of monospaced text. //! This is basically what you want if you are implementing `ls`. //! //! # Example //! //! ``` //! use tabular::{Table, Row}; //! use std::path::Path; //! //! fn ls(dir: &Path) -> ::std::io::Result<()> { //! let mut table = Table::new("{:>} {:<}{:<} {:<}"); //! for entry_result in ::std::fs::read_dir(dir)? { //! let entry = entry_result?; //! let metadata = entry.metadata()?; //! //! table.add_row(Row::new() //! .with_cell(metadata.len()) //! .with_cell(if metadata.permissions().readonly() {"r"} else {""}) //! .with_cell(if metadata.is_dir() {"d"} else {""}) //! .with_cell(entry.path().display())); //! } //! //! print!("{}", table); //! //! Ok(()) //! } //! //! ls(Path::new(&"target")).unwrap(); //! ``` //! //! produces something like //! //! ```text //! 1198 target/.rustc_info.json //! 1120 d target/doc //! 192 d target/package //! 1056 d target/debug //! ``` //! //! # Other features //! //! - The [`Table::with_header`] and [`Table::add_header`] methods add //! lines that span all columns. //! //! - The [`row!`] macro builds a row with a fixed number of columns //! using less syntax. //! //! - The [`Table::set_line_end`] method allows changing the line ending //! to include a carriage return (or whatever you want). //! //! # Usage //! //! It's on [crates.io](https://crates.io/crates/tabular), so you can add //! //! ```toml //! [dependencies] //! tabular = "0.1.2" //! ``` //! //! to your `Cargo.toml`. //! //! //! Feature `unicode-width` is enabled be default; it depends on the //! [unicode-width](https://crates.io/crates/unicode-width) crate. You can turn //! it off with: //! //! ```toml //! [dependencies] //! tabular = { version = "0.1.2", default-features = false } //! ``` //! //! Note that without `unicode-width`, alignment will be based on the count of the //! `std::str::Chars` iterator. //! //! This crate supports Rust version 1.18.0 and later. //! //! # See also //! //! You may also want: //! //! - [text-tables](https://crates.io/crates/text-tables) – This is more automatic //! than tabular. You give it an array of arrays, it renders a nice table with borders. //! Tabular doesn't do borders. //! //! - [prettytable](https://crates.io/crates/prettytable-rs) — This has an API more //! similar to tabular’s in terms of building a table, but it does a lot more, including, //! color, borders, and CSV import. //! //! [`row!`]: macro.row.html //! [`Row`]: struct.Row.html //! [`Table`]: struct.Table.html //! [`Table::add_header`]: struct.Table.html#method.add_header //! [`Table::add_row`]: struct.Table.html#method.add_row //! [`Table::new`]: struct.Table.html#method.new //! [`Table::set_line_end`]: struct.Table.html#method.set_line_end //! [`Table::with_row`]: struct.Table.html#method.with_row //! [`Table::with_header`]: struct.Table.html#method.with_header #![warn(missing_docs)] #[cfg(feature = "unicode-width")] extern crate unicode_width; mod column_spec; mod error; mod macros; mod row; mod table; mod width_string; pub use error::{Error, Result}; pub use row::Row; pub use table::Table; #[cfg(test)] mod tests { use super::*; #[test] fn alignment() { let mut table = Table::new("{:>} ({:<}) {:<}"); table .add_row(Row::new().with_cell(1).with_cell("I").with_cell("one")) .add_row(Row::new().with_cell(5).with_cell("V").with_cell("five")) .add_row(Row::new().with_cell(10).with_cell("X").with_cell("ten")) .add_row(Row::new().with_cell(50).with_cell("L").with_cell("fifty")) .add_row(Row::new().with_cell(100).with_cell("C").with_cell("one-hundred")); assert_eq!( format!("\n{}", table), r#" 1 (I) one 5 (V) five 10 (X) ten 50 (L) fifty 100 (C) one-hundred "# ); } #[test] fn heading() { let _row = Row::from_cells(vec!["a", "b", "c"]); // eprintln!("{:?}", _row); let table = Table::new("{:<} {:<} {:>}") .with_row(Row::from_cells(vec!["a", "b", "d"])) .with_heading("This is my table") .with_row(Row::from_cells(vec!["ab", "bc", "cd"])); // eprintln!("\n\n{:?}\n\n", table); assert_eq! ( format!("\n{}", table), r#" a b d This is my table ab bc cd "# ); } #[test] fn centering() { let table = Table::new("{:<} {:^} {:>}") .with_row(Row::from_cells(vec!["a", "b", "c"])) .with_row(Row::from_cells(vec!["a", "bc", "d"])) .with_row(Row::from_cells(vec!["a", "bcd", "e"])) .with_row(Row::from_cells(vec!["a", "bcde", "f"])) .with_row(Row::from_cells(vec!["a", "bcdef", "g"])); assert_eq! ( format!("\n{}", table), r#" a b c a bc d a bcd e a bcde f a bcdef g "# ); } #[test] fn temporary() { } }