Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fmt #149

Merged
merged 1 commit into from Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions examples/basic.rs
@@ -1,4 +1,4 @@
use prettytable::{Table, Row, Cell, row, table, ptable};
use prettytable::{ptable, row, table, Cell, Row, Table};

/*
Following main function will print :
Expand All @@ -22,19 +22,27 @@ fn main() {
let mut table = Table::new();
table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
table.add_row(row!["foobar", "bar", "foo"]);
table.add_row(Row::new(vec![Cell::new("foobar2"), Cell::new("bar2"), Cell::new("foo2")]));
table.add_row(Row::new(vec![
Cell::new("foobar2"),
Cell::new("bar2"),
Cell::new("foo2"),
]));
table.printstd();
println!("Modified : ");
table.set_element("new_foo", 2, 1).unwrap();
table.printstd();

// The same table can be built the following way :
let _table = table!(["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]);
let _table = table!(
["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]
);

// Or directly print it like this
let _table = ptable!(["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]);
let _table = ptable!(
["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]
);
}
14 changes: 9 additions & 5 deletions examples/csv.rs
Expand Up @@ -16,15 +16,19 @@
fn main() {
use prettytable::Table;

let table = Table::from_csv_string("ABC,DEFG,HIJKLMN\n\
let table = Table::from_csv_string(
"ABC,DEFG,HIJKLMN\n\
foobar,bar,foo\n\
foobar2,bar2,foo2")
.unwrap();
foobar2,bar2,foo2",
)
.unwrap();
table.printstd();

println!("");
println!("{}",
String::from_utf8(table.to_csv(Vec::new()).unwrap().into_inner().unwrap()).unwrap());
println!(
"{}",
String::from_utf8(table.to_csv(Vec::new()).unwrap().into_inner().unwrap()).unwrap()
);
}

#[cfg(not(feature = "csv"))]
Expand Down
51 changes: 31 additions & 20 deletions examples/formatting.rs
@@ -1,4 +1,4 @@
use prettytable::{format, table, row};
use prettytable::{format, row, table};

fn main() {
let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
Expand Down Expand Up @@ -49,14 +49,17 @@ fn main() {
// | Value three | Value four |
// +-------------+------------+
println!("Custom :");
table.set_format(format::FormatBuilder::new()
.column_separator('|')
.borders('|')
.separators(&[format::LinePosition::Top,
format::LinePosition::Bottom],
format::LineSeparator::new('-', '+', '+', '+'))
.padding(1, 1)
.build());
table.set_format(
format::FormatBuilder::new()
.column_separator('|')
.borders('|')
.separators(
&[format::LinePosition::Top, format::LinePosition::Bottom],
format::LineSeparator::new('-', '+', '+', '+'),
)
.padding(1, 1)
.build(),
);
table.printstd();

// Customized format with unicode
Expand All @@ -69,17 +72,25 @@ fn main() {
// │ Value three │ Value four │
// └─────────────┴────────────┘
println!("With unicode:");
table.set_format(format::FormatBuilder::new()
.column_separator('│')
.borders('│')
.separators(&[format::LinePosition::Top],
format::LineSeparator::new('─', '┬', '┌', '┐'))
.separators(&[format::LinePosition::Intern],
format::LineSeparator::new('─', '┼', '├', '┤'))
.separators(&[format::LinePosition::Bottom],
format::LineSeparator::new('─', '┴', '└', '┘'))
.padding(1, 1)
.build());
table.set_format(
format::FormatBuilder::new()
.column_separator('│')
.borders('│')
.separators(
&[format::LinePosition::Top],
format::LineSeparator::new('─', '┬', '┌', '┐'),
)
.separators(
&[format::LinePosition::Intern],
format::LineSeparator::new('─', '┼', '├', '┤'),
)
.separators(
&[format::LinePosition::Bottom],
format::LineSeparator::new('─', '┴', '└', '┘'),
)
.padding(1, 1)
.build(),
);
table.printstd();

// Customized format with unicode and different padding
Expand Down
16 changes: 10 additions & 6 deletions examples/multiline.rs
Expand Up @@ -18,11 +18,15 @@ use prettytable::table;
+-------------------------+------------------------------+
*/
fn main() {
let table1 = table!(["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]);
let table2 = table!(["Title 1", "Title 2"],
["This is\na multiline\ncell", "foo"],
["Yo dawg ;) You can even\nprint tables\ninto tables", table1]);
let table1 = table!(
["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]
);
let table2 = table!(
["Title 1", "Title 2"],
["This is\na multiline\ncell", "foo"],
["Yo dawg ;) You can even\nprint tables\ninto tables", table1]
);
table2.printstd();
}
11 changes: 9 additions & 2 deletions examples/slices.rs
@@ -1,7 +1,14 @@
use prettytable::{Slice, table, row};
use prettytable::{row, table, Slice};

fn main() {
let mut table = table![[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]];
let mut table = table![
[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]
];
table.set_titles(row!["t1", "t2", "t3"]);

let slice = table.slice(..);
Expand Down
22 changes: 11 additions & 11 deletions examples/span.rs
@@ -1,8 +1,6 @@
use prettytable::{Row, Cell, format::Alignment, table};

use prettytable::{format::Alignment, table, Cell, Row};

fn main() {

/*
The following code will output

Expand All @@ -18,12 +16,14 @@ fn main() {
*/

let mut table: prettytable::Table = table![
[H2 -> "This is a cell with span of 2", "span of 1"],
["span of 1", "span of 1", "span of 1"],
[H03c -> "This cell with a span of 3 is centered"]
];
table.set_titles(Row::new(vec![
Cell::new_align("A table with horizontal span", Alignment::CENTER).with_hspan(3)
]));
[H2 -> "This is a cell with span of 2", "span of 1"],
["span of 1", "span of 1", "span of 1"],
[H03c -> "This cell with a span of 3 is centered"]
];
table.set_titles(Row::new(vec![Cell::new_align(
"A table with horizontal span",
Alignment::CENTER,
)
.with_hspan(3)]));
table.printstd();
}
}
22 changes: 11 additions & 11 deletions examples/style.rs
@@ -1,6 +1,6 @@
use prettytable::{Table, Row, Cell};
use prettytable::{Attr, color};
use prettytable::{table, row, cell, ptable};
use prettytable::{cell, ptable, row, table};
use prettytable::{color, Attr};
use prettytable::{Cell, Row, Table};

#[allow(dead_code)]
fn main() {
Expand All @@ -11,14 +11,14 @@ fn main() {
// Add style to a full row
table.add_row(row![FY => "styled", "bar", "foo"]);
table.add_row(Row::new(vec![
Cell::new("foobar2"),
// Create a cell with a red foreground color
Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
// Create a cell with red foreground color, yellow background color, with bold characters
Cell::new("foo2").style_spec("FrByb"),
// Using the cell! macro
cell!(Fr->"red")])
);
Cell::new("foobar2"),
// Create a cell with a red foreground color
Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
// Create a cell with red foreground color, yellow background color, with bold characters
Cell::new("foo2").style_spec("FrByb"),
// Using the cell! macro
cell!(Fr->"red"),
]));

table.printstd();

Expand Down
2 changes: 1 addition & 1 deletion examples/tictactoe.rs
@@ -1,4 +1,4 @@
use prettytable::{Table, table, cell};
use prettytable::{cell, table, Table};

use std::io;
use std::io::Write;
Expand Down
38 changes: 21 additions & 17 deletions src/cell.rs
Expand Up @@ -4,8 +4,8 @@ use super::format::Alignment;
use super::utils::{display_width, print_align, HtmlEscape};
use super::{color, Attr, Terminal};
use std::io::{Error, Write};
use std::string::ToString;
use std::str::FromStr;
use std::string::ToString;

/// Represent a table cell containing a string.
///
Expand Down Expand Up @@ -177,19 +177,19 @@ impl Cell {

/// Return the height of the cell
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
pub (crate) fn get_height(&self) -> usize {
pub(crate) fn get_height(&self) -> usize {
self.content.len()
}

/// Return the width of the cell
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
pub (crate) fn get_width(&self) -> usize {
pub(crate) fn get_width(&self) -> usize {
self.width
}

/// Set horizontal span for this cell (must be > 0)
pub fn set_hspan(&mut self, hspan: usize) {
self.hspan = if hspan == 0 {1} else {hspan};
self.hspan = if hspan == 0 { 1 } else { hspan };
}

/// Get horizontal span of this cell (> 0)
Expand All @@ -207,7 +207,7 @@ impl Cell {
/// fill the cells with blanks so it fits in the table.
/// If `ìdx` is higher than this cell's height, it will print empty content
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
pub (crate) fn print<T: Write + ?Sized>(
pub(crate) fn print<T: Write + ?Sized>(
&self,
out: &mut T,
idx: usize,
Expand All @@ -220,7 +220,7 @@ impl Cell {

/// Apply style then call `print` to print the cell into a terminal
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
pub (crate) fn print_term<T: Terminal + ?Sized>(
pub(crate) fn print_term<T: Terminal + ?Sized>(
&self,
out: &mut T,
idx: usize,
Expand Down Expand Up @@ -393,8 +393,8 @@ macro_rules! cell {
mod tests {
use super::Cell;
use crate::format::Alignment;
use term::{color, Attr};
use crate::utils::StringWriter;
use term::{color, Attr};

#[test]
fn get_content() {
Expand Down Expand Up @@ -438,7 +438,10 @@ mod tests {

let mut out = StringWriter::new();
let _ = ascii_cell.print_html(&mut out);
assert_eq!(out.as_string(), r#"<td style="text-align: left;">hello</td>"#);
assert_eq!(
out.as_string(),
r#"<td style="text-align: left;">hello</td>"#
);
}

#[test]
Expand All @@ -447,7 +450,10 @@ mod tests {

let mut out = StringWriter::new();
let _ = ascii_cell.print_html(&mut out);
assert_eq!(out.as_string(), r#"<td style="text-align: left;">&lt;abc&quot;&gt;&amp;&#39;</td>"#);
assert_eq!(
out.as_string(),
r#"<td style="text-align: left;">&lt;abc&quot;&gt;&amp;&#39;</td>"#
);
}

#[test]
Expand Down Expand Up @@ -482,18 +488,16 @@ mod tests {
assert!(cell.style.contains(&Attr::Italic(true)));
assert!(cell.style.contains(&Attr::Bold));
assert!(cell.style.contains(&Attr::ForegroundColor(color::RED)));
assert!(
cell.style
.contains(&Attr::BackgroundColor(color::BRIGHT_BLUE))
);
assert!(cell
.style
.contains(&Attr::BackgroundColor(color::BRIGHT_BLUE)));
assert_eq!(cell.align, Alignment::CENTER);

cell = cell.style_spec("FDBwr");
assert_eq!(cell.style.len(), 2);
assert!(
cell.style
.contains(&Attr::ForegroundColor(color::BRIGHT_BLACK))
);
assert!(cell
.style
.contains(&Attr::ForegroundColor(color::BRIGHT_BLACK)));
assert!(cell.style.contains(&Attr::BackgroundColor(color::WHITE)));
assert_eq!(cell.align, Alignment::RIGHT);

Expand Down