pub struct Table { /* private fields */ }Expand description
An owned printable table
Implementations§
Source§impl Table
impl Table
Sourcepub fn new() -> Table
pub fn new() -> Table
Create an empty table
Examples found in repository?
28fn main() {
29 let mut table = Table::new();
30 table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
31 table.add_row(row!["foobar", "bar", "foo"]);
32 table.add_row(Row::new(vec![
33 Cell::new("foobar2"),
34 Cell::new("bar2"),
35 Cell::new("foo2"),
36 ]));
37 table.printstd();
38 println!("Modified : ");
39 table.set_element("new_foo", 2, 1).unwrap();
40 table.printstd();
41
42 // The same table can be built the following way :
43 let _table = table!(
44 ["ABC", "DEFG", "HIJKLMN"],
45 ["foobar", "bar", "foo"],
46 ["foobar2", "bar2", "foo2"]
47 );
48
49 // Or directly print it like this
50 let _table = ptable!(
51 ["ABC", "DEFG", "HIJKLMN"],
52 ["foobar", "bar", "foo"],
53 ["foobar2", "bar2", "foo2"]
54 );
55}More examples
14fn main() {
15 let _ = table!();
16 let mut table = Table::new();
17 // Add style to a cell
18 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
19 // Add style to a full row
20 table.add_row(row![FY => "styled", "bar", "foo"]);
21 table.add_row(Row::new(vec![
22 Cell::new("foobar2"),
23 // Create a cell with a red foreground color
24 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
25 // Create a cell with red foreground color, yellow background color, with bold characters
26 Cell::new("foo2").style_spec("FrByb"),
27 // Using the cell! macro
28 cell!(Fr->"red"),
29 ]));
30
31 table.printstd();
32
33 // Print a table with some styles on it :
34 // FrBybl means : Foregound red, Background yellow, bold, left align
35 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
36
37 // You can also apply style to full rows :
38 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
39 // Set a title line, with all text centered in the cell
40 table.set_titles(row![c => "Title 1", "Title 2"]);
41 table.printstd();
42}Sourcepub fn set_format(&mut self, format: TableFormat)
pub fn set_format(&mut self, format: TableFormat)
Change the table format. Eg : Separators
Examples found in repository?
7fn main() {
8 let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9 table.set_titles(row!["Title 1", "Title 2"]);
10
11 // Print
12 // +-------------+------------+
13 // | Title 1 | Title 2 |
14 // +-------------+------------+
15 // | Value 1 | Value 2 |
16 // | Value three | Value four |
17 // +-------------+------------+
18 println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19 table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20 table.printstd();
21 println!();
22
23 // Print
24 // -------------------------
25 // Title 1 Title 2
26 // =========================
27 // Value 1 Value 2
28 // -------------------------
29 // Value three Value four
30 // -------------------------
31 println!("FORMAT_NO_COLSEP :");
32 table.set_format(*format::consts::FORMAT_NO_COLSEP);
33 table.printstd();
34 println!();
35
36 // Print
37 // +-------------------------+
38 // | Title 1 Title 2 |
39 // +=========================+
40 // | Value 1 Value 2 |
41 // | Value three Value four |
42 // +-------------------------+
43 println!("FORMAT_BORDERS_ONLY :");
44 table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45 table.printstd();
46 println!();
47
48 // Custom format can be implemented using `prettytable::format::FormatBuilder`
49 // Example to print
50 // +-------------+------------+
51 // | Title 1 | Title 2 |
52 // | Value 1 | Value 2 |
53 // | Value three | Value four |
54 // +-------------+------------+
55 println!("Custom :");
56 table.set_format(
57 format::FormatBuilder::new()
58 .column_separator('|')
59 .borders('|')
60 .separators(
61 &[format::LinePosition::Top, format::LinePosition::Bottom],
62 format::LineSeparator::new('-', '+', '+', '+'),
63 )
64 .padding(1, 1)
65 .build(),
66 );
67 table.printstd();
68
69 // Customized format with unicode
70 // Example to print
71 // ┌─────────────┬────────────┐
72 // │ Title 1 │ Title 2 │
73 // ├─────────────┼────────────┤
74 // │ Value 1 │ Value 2 │
75 // ├─────────────┼────────────┤
76 // │ Value three │ Value four │
77 // └─────────────┴────────────┘
78 println!("With unicode:");
79 table.set_format(
80 format::FormatBuilder::new()
81 .column_separator('│')
82 .borders('│')
83 .separators(
84 &[format::LinePosition::Top],
85 format::LineSeparator::new('─', '┬', '┌', '┐'),
86 )
87 .separators(
88 &[format::LinePosition::Intern],
89 format::LineSeparator::new('─', '┼', '├', '┤'),
90 )
91 .separators(
92 &[format::LinePosition::Bottom],
93 format::LineSeparator::new('─', '┴', '└', '┘'),
94 )
95 .padding(1, 1)
96 .build(),
97 );
98 table.printstd();
99
100 // Customized format with unicode and different padding
101 // Example to print
102 // ┌───────────────┬──────────────┐
103 // │ Title 1 │ Title 2 │
104 // ├───────────────┼──────────────┤
105 // │ Value 1 │ Value 2 │
106 // ├───────────────┼──────────────┤
107 // │ Value three │ Value four │
108 // └───────────────┴──────────────┘
109 // Change individual format settings
110 println!("With unicode and padding:");
111 table.get_format().padding(2, 2);
112 table.printstd();
113}Sourcepub fn get_format(&mut self) -> &mut TableFormat
pub fn get_format(&mut self) -> &mut TableFormat
Get a mutable reference to the internal format
Examples found in repository?
7fn main() {
8 let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9 table.set_titles(row!["Title 1", "Title 2"]);
10
11 // Print
12 // +-------------+------------+
13 // | Title 1 | Title 2 |
14 // +-------------+------------+
15 // | Value 1 | Value 2 |
16 // | Value three | Value four |
17 // +-------------+------------+
18 println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19 table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20 table.printstd();
21 println!();
22
23 // Print
24 // -------------------------
25 // Title 1 Title 2
26 // =========================
27 // Value 1 Value 2
28 // -------------------------
29 // Value three Value four
30 // -------------------------
31 println!("FORMAT_NO_COLSEP :");
32 table.set_format(*format::consts::FORMAT_NO_COLSEP);
33 table.printstd();
34 println!();
35
36 // Print
37 // +-------------------------+
38 // | Title 1 Title 2 |
39 // +=========================+
40 // | Value 1 Value 2 |
41 // | Value three Value four |
42 // +-------------------------+
43 println!("FORMAT_BORDERS_ONLY :");
44 table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45 table.printstd();
46 println!();
47
48 // Custom format can be implemented using `prettytable::format::FormatBuilder`
49 // Example to print
50 // +-------------+------------+
51 // | Title 1 | Title 2 |
52 // | Value 1 | Value 2 |
53 // | Value three | Value four |
54 // +-------------+------------+
55 println!("Custom :");
56 table.set_format(
57 format::FormatBuilder::new()
58 .column_separator('|')
59 .borders('|')
60 .separators(
61 &[format::LinePosition::Top, format::LinePosition::Bottom],
62 format::LineSeparator::new('-', '+', '+', '+'),
63 )
64 .padding(1, 1)
65 .build(),
66 );
67 table.printstd();
68
69 // Customized format with unicode
70 // Example to print
71 // ┌─────────────┬────────────┐
72 // │ Title 1 │ Title 2 │
73 // ├─────────────┼────────────┤
74 // │ Value 1 │ Value 2 │
75 // ├─────────────┼────────────┤
76 // │ Value three │ Value four │
77 // └─────────────┴────────────┘
78 println!("With unicode:");
79 table.set_format(
80 format::FormatBuilder::new()
81 .column_separator('│')
82 .borders('│')
83 .separators(
84 &[format::LinePosition::Top],
85 format::LineSeparator::new('─', '┬', '┌', '┐'),
86 )
87 .separators(
88 &[format::LinePosition::Intern],
89 format::LineSeparator::new('─', '┼', '├', '┤'),
90 )
91 .separators(
92 &[format::LinePosition::Bottom],
93 format::LineSeparator::new('─', '┴', '└', '┘'),
94 )
95 .padding(1, 1)
96 .build(),
97 );
98 table.printstd();
99
100 // Customized format with unicode and different padding
101 // Example to print
102 // ┌───────────────┬──────────────┐
103 // │ Title 1 │ Title 2 │
104 // ├───────────────┼──────────────┤
105 // │ Value 1 │ Value 2 │
106 // ├───────────────┼──────────────┤
107 // │ Value three │ Value four │
108 // └───────────────┴──────────────┘
109 // Change individual format settings
110 println!("With unicode and padding:");
111 table.get_format().padding(2, 2);
112 table.printstd();
113}Sourcepub fn set_titles(&mut self, titles: Row)
pub fn set_titles(&mut self, titles: Row)
Set the optional title lines
Examples found in repository?
7fn main() {
8 let mut table = table![[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]];
9 table.set_titles(row!["t1", "t2", "t3"]);
10
11 let slice = table.slice(..);
12 let slice = slice.slice(2..);
13 let slice = slice.slice(..3);
14
15 /*
16 Will print
17 +----+----+----+
18 | t1 | t2 | t3 |
19 +====+====+====+
20 | 2 | 2 | 2 |
21 +----+----+----+
22 | 3 | 3 | 3 |
23 +----+----+----+
24 | 4 | 4 | 4 |
25 +----+----+----+
26 */
27 slice.printstd();
28
29 // This is equivalent to
30 let slice = table.slice(2..5);
31 slice.printstd();
32}More examples
8fn main() {
9 /*
10 The following code will output
11
12 +---------------+---------------+--------------+
13 | A table with horizontal span |
14 +===============+===============+==============+
15 | This is a cell with span of 2 | span of 1 |
16 +---------------+---------------+--------------+
17 | span of 1 | span of 1 | span of 1 |
18 +---------------+---------------+--------------+
19 | This cell with a span of 3 is centered |
20 +---------------+---------------+--------------+
21 */
22
23 let mut table: prettytable::Table = table![
24 [H2 -> "This is a cell with span of 2", "span of 1"],
25 ["span of 1", "span of 1", "span of 1"],
26 [H03c -> "This cell with a span of 3 is centered"]
27 ];
28 table.set_titles(Row::new(vec![Cell::new_align(
29 "A table with horizontal span",
30 Alignment::CENTER,
31 )
32 .with_hspan(3)]));
33 table.printstd();
34}14fn main() {
15 let _ = table!();
16 let mut table = Table::new();
17 // Add style to a cell
18 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
19 // Add style to a full row
20 table.add_row(row![FY => "styled", "bar", "foo"]);
21 table.add_row(Row::new(vec![
22 Cell::new("foobar2"),
23 // Create a cell with a red foreground color
24 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
25 // Create a cell with red foreground color, yellow background color, with bold characters
26 Cell::new("foo2").style_spec("FrByb"),
27 // Using the cell! macro
28 cell!(Fr->"red"),
29 ]));
30
31 table.printstd();
32
33 // Print a table with some styles on it :
34 // FrBybl means : Foregound red, Background yellow, bold, left align
35 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
36
37 // You can also apply style to full rows :
38 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
39 // Set a title line, with all text centered in the cell
40 table.set_titles(row![c => "Title 1", "Title 2"]);
41 table.printstd();
42}7fn main() {
8 let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9 table.set_titles(row!["Title 1", "Title 2"]);
10
11 // Print
12 // +-------------+------------+
13 // | Title 1 | Title 2 |
14 // +-------------+------------+
15 // | Value 1 | Value 2 |
16 // | Value three | Value four |
17 // +-------------+------------+
18 println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19 table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20 table.printstd();
21 println!();
22
23 // Print
24 // -------------------------
25 // Title 1 Title 2
26 // =========================
27 // Value 1 Value 2
28 // -------------------------
29 // Value three Value four
30 // -------------------------
31 println!("FORMAT_NO_COLSEP :");
32 table.set_format(*format::consts::FORMAT_NO_COLSEP);
33 table.printstd();
34 println!();
35
36 // Print
37 // +-------------------------+
38 // | Title 1 Title 2 |
39 // +=========================+
40 // | Value 1 Value 2 |
41 // | Value three Value four |
42 // +-------------------------+
43 println!("FORMAT_BORDERS_ONLY :");
44 table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45 table.printstd();
46 println!();
47
48 // Custom format can be implemented using `prettytable::format::FormatBuilder`
49 // Example to print
50 // +-------------+------------+
51 // | Title 1 | Title 2 |
52 // | Value 1 | Value 2 |
53 // | Value three | Value four |
54 // +-------------+------------+
55 println!("Custom :");
56 table.set_format(
57 format::FormatBuilder::new()
58 .column_separator('|')
59 .borders('|')
60 .separators(
61 &[format::LinePosition::Top, format::LinePosition::Bottom],
62 format::LineSeparator::new('-', '+', '+', '+'),
63 )
64 .padding(1, 1)
65 .build(),
66 );
67 table.printstd();
68
69 // Customized format with unicode
70 // Example to print
71 // ┌─────────────┬────────────┐
72 // │ Title 1 │ Title 2 │
73 // ├─────────────┼────────────┤
74 // │ Value 1 │ Value 2 │
75 // ├─────────────┼────────────┤
76 // │ Value three │ Value four │
77 // └─────────────┴────────────┘
78 println!("With unicode:");
79 table.set_format(
80 format::FormatBuilder::new()
81 .column_separator('│')
82 .borders('│')
83 .separators(
84 &[format::LinePosition::Top],
85 format::LineSeparator::new('─', '┬', '┌', '┐'),
86 )
87 .separators(
88 &[format::LinePosition::Intern],
89 format::LineSeparator::new('─', '┼', '├', '┤'),
90 )
91 .separators(
92 &[format::LinePosition::Bottom],
93 format::LineSeparator::new('─', '┴', '└', '┘'),
94 )
95 .padding(1, 1)
96 .build(),
97 );
98 table.printstd();
99
100 // Customized format with unicode and different padding
101 // Example to print
102 // ┌───────────────┬──────────────┐
103 // │ Title 1 │ Title 2 │
104 // ├───────────────┼──────────────┤
105 // │ Value 1 │ Value 2 │
106 // ├───────────────┼──────────────┤
107 // │ Value three │ Value four │
108 // └───────────────┴──────────────┘
109 // Change individual format settings
110 println!("With unicode and padding:");
111 table.get_format().padding(2, 2);
112 table.printstd();
113}Sourcepub fn unset_titles(&mut self)
pub fn unset_titles(&mut self)
Unset the title line
Sourcepub fn get_mut_row(&mut self, row: usize) -> Option<&mut Row>
pub fn get_mut_row(&mut self, row: usize) -> Option<&mut Row>
Get a mutable reference to a row
Examples found in repository?
15fn main() {
16 let mut table = table![
17 [EMPTY, EMPTY, EMPTY],
18 [EMPTY, EMPTY, EMPTY],
19 [EMPTY, EMPTY, EMPTY]
20 ];
21 let mut height = table.print_tty(false).unwrap();
22 let stdin = io::stdin();
23 let mut stdout = io::stdout();
24 let mut current = CROSS;
25 let mut terminal = term::stdout().unwrap();
26 loop {
27 let mut line = String::new();
28 print!("{} plays > ", current);
29 height += 1;
30 stdout.flush().unwrap();
31 stdin.read_line(&mut line).expect("Cannot read input");
32 let i = match usize::from_str(line.trim()) {
33 Ok(i) => i,
34 _ => {
35 println!("Bad input");
36 height += 1;
37 continue;
38 },
39 };
40 if !(1..=9).contains(&i) {
41 println!("Bad input, should be between 1 and 9");
42 height += 1;
43 continue;
44 }
45 let x = (i - 1) % 3;
46 let y = (i - 1) / 3;
47 {
48 let row = table.get_mut_row(y).unwrap();
49 if row.get_cell(x).unwrap().to_string() != EMPTY {
50 println!("There's already someone there");
51 height += 1;
52 continue;
53 }
54 row.set_cell(cell!(current), x).unwrap();
55 }
56 for _ in 0..height {
57 terminal.cursor_up().unwrap();
58 terminal.delete_line().unwrap();
59 }
60 height = table.print_tty(false).unwrap();
61 if check(&table) {
62 return;
63 }
64 if current == CROSS {
65 current = ROUND;
66 } else {
67 current = CROSS;
68 }
69 }
70}Sourcepub fn add_row(&mut self, row: Row) -> &mut Row
pub fn add_row(&mut self, row: Row) -> &mut Row
Append a row in the table, transferring ownership of this row to the table and returning a mutable reference to the row
Examples found in repository?
28fn main() {
29 let mut table = Table::new();
30 table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
31 table.add_row(row!["foobar", "bar", "foo"]);
32 table.add_row(Row::new(vec![
33 Cell::new("foobar2"),
34 Cell::new("bar2"),
35 Cell::new("foo2"),
36 ]));
37 table.printstd();
38 println!("Modified : ");
39 table.set_element("new_foo", 2, 1).unwrap();
40 table.printstd();
41
42 // The same table can be built the following way :
43 let _table = table!(
44 ["ABC", "DEFG", "HIJKLMN"],
45 ["foobar", "bar", "foo"],
46 ["foobar2", "bar2", "foo2"]
47 );
48
49 // Or directly print it like this
50 let _table = ptable!(
51 ["ABC", "DEFG", "HIJKLMN"],
52 ["foobar", "bar", "foo"],
53 ["foobar2", "bar2", "foo2"]
54 );
55}More examples
14fn main() {
15 let _ = table!();
16 let mut table = Table::new();
17 // Add style to a cell
18 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
19 // Add style to a full row
20 table.add_row(row![FY => "styled", "bar", "foo"]);
21 table.add_row(Row::new(vec![
22 Cell::new("foobar2"),
23 // Create a cell with a red foreground color
24 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
25 // Create a cell with red foreground color, yellow background color, with bold characters
26 Cell::new("foo2").style_spec("FrByb"),
27 // Using the cell! macro
28 cell!(Fr->"red"),
29 ]));
30
31 table.printstd();
32
33 // Print a table with some styles on it :
34 // FrBybl means : Foregound red, Background yellow, bold, left align
35 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
36
37 // You can also apply style to full rows :
38 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
39 // Set a title line, with all text centered in the cell
40 table.set_titles(row![c => "Title 1", "Title 2"]);
41 table.printstd();
42}Sourcepub fn add_empty_row(&mut self) -> &mut Row
pub fn add_empty_row(&mut self) -> &mut Row
Append an empty row in the table. Return a mutable reference to this new row.
Sourcepub fn insert_row(&mut self, index: usize, row: Row) -> &mut Row
pub fn insert_row(&mut self, index: usize, row: Row) -> &mut Row
Insert row at the position index, and return a mutable reference to this row.
If index is higher than current numbers of rows, row is appended at the end of the table
Sourcepub fn set_element(
&mut self,
element: &str,
column: usize,
row: usize,
) -> Result<(), &str>
pub fn set_element( &mut self, element: &str, column: usize, row: usize, ) -> Result<(), &str>
Modify a single element in the table
Examples found in repository?
28fn main() {
29 let mut table = Table::new();
30 table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
31 table.add_row(row!["foobar", "bar", "foo"]);
32 table.add_row(Row::new(vec![
33 Cell::new("foobar2"),
34 Cell::new("bar2"),
35 Cell::new("foo2"),
36 ]));
37 table.printstd();
38 println!("Modified : ");
39 table.set_element("new_foo", 2, 1).unwrap();
40 table.printstd();
41
42 // The same table can be built the following way :
43 let _table = table!(
44 ["ABC", "DEFG", "HIJKLMN"],
45 ["foobar", "bar", "foo"],
46 ["foobar2", "bar2", "foo2"]
47 );
48
49 // Or directly print it like this
50 let _table = ptable!(
51 ["ABC", "DEFG", "HIJKLMN"],
52 ["foobar", "bar", "foo"],
53 ["foobar2", "bar2", "foo2"]
54 );
55}Sourcepub fn remove_row(&mut self, index: usize)
pub fn remove_row(&mut self, index: usize)
Remove the row at position index. Silently skip if the row does not exist
Sourcepub fn column_iter(&self, column: usize) -> ColumnIter<'_> ⓘ
pub fn column_iter(&self, column: usize) -> ColumnIter<'_> ⓘ
Return an iterator over the immutable cells of the column specified by column
Sourcepub fn column_iter_mut(&mut self, column: usize) -> ColumnIterMut<'_> ⓘ
pub fn column_iter_mut(&mut self, column: usize) -> ColumnIterMut<'_> ⓘ
Return an iterator over the mutable cells of the column specified by column
Sourcepub fn row_iter_mut(&mut self) -> IterMut<'_, Row>
pub fn row_iter_mut(&mut self) -> IterMut<'_, Row>
Returns an iterator over mutable rows
Sourcepub fn print<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error>
pub fn print<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error>
Print the table to out and returns the number
of lines printed, or an error
Sourcepub fn print_term<T: Terminal + ?Sized>(
&self,
out: &mut T,
) -> Result<usize, Error>
pub fn print_term<T: Terminal + ?Sized>( &self, out: &mut T, ) -> Result<usize, Error>
Print the table to terminal out, applying styles when needed and returns the number
of lines printed, or an error
Sourcepub fn print_tty(&self, force_colorize: bool) -> Result<usize, Error>
pub fn print_tty(&self, force_colorize: bool) -> Result<usize, Error>
Print the table to standard output. Colors won’t be displayed unless
stdout is a tty terminal, or force_colorize is set to true.
In ANSI terminals, colors are displayed using ANSI escape characters. When for example the
output is redirected to a file, or piped to another program, the output is considered
as not beeing tty, and ANSI escape characters won’t be displayed unless force colorize
is set to true.
§Returns
A Result holding the number of lines printed, or an io::Error if any failure happens
Examples found in repository?
15fn main() {
16 let mut table = table![
17 [EMPTY, EMPTY, EMPTY],
18 [EMPTY, EMPTY, EMPTY],
19 [EMPTY, EMPTY, EMPTY]
20 ];
21 let mut height = table.print_tty(false).unwrap();
22 let stdin = io::stdin();
23 let mut stdout = io::stdout();
24 let mut current = CROSS;
25 let mut terminal = term::stdout().unwrap();
26 loop {
27 let mut line = String::new();
28 print!("{} plays > ", current);
29 height += 1;
30 stdout.flush().unwrap();
31 stdin.read_line(&mut line).expect("Cannot read input");
32 let i = match usize::from_str(line.trim()) {
33 Ok(i) => i,
34 _ => {
35 println!("Bad input");
36 height += 1;
37 continue;
38 },
39 };
40 if !(1..=9).contains(&i) {
41 println!("Bad input, should be between 1 and 9");
42 height += 1;
43 continue;
44 }
45 let x = (i - 1) % 3;
46 let y = (i - 1) / 3;
47 {
48 let row = table.get_mut_row(y).unwrap();
49 if row.get_cell(x).unwrap().to_string() != EMPTY {
50 println!("There's already someone there");
51 height += 1;
52 continue;
53 }
54 row.set_cell(cell!(current), x).unwrap();
55 }
56 for _ in 0..height {
57 terminal.cursor_up().unwrap();
58 terminal.delete_line().unwrap();
59 }
60 height = table.print_tty(false).unwrap();
61 if check(&table) {
62 return;
63 }
64 if current == CROSS {
65 current = ROUND;
66 } else {
67 current = CROSS;
68 }
69 }
70}Sourcepub fn printstd(&self)
pub fn printstd(&self)
Print the table to standard output. Colors won’t be displayed unless
stdout is a tty terminal. This means that if stdout is redirected to a file, or piped
to another program, no color will be displayed.
To force colors rendering, use print_tty() method.
Any failure to print is ignored. For better control, use print_tty().
Calling printstd() is equivalent to calling print_tty(false) and ignoring the result.
Examples found in repository?
20fn main() {
21 let table1 = table!(
22 ["ABC", "DEFG", "HIJKLMN"],
23 ["foobar", "bar", "foo"],
24 ["foobar2", "bar2", "foo2"]
25 );
26 let table2 = table!(
27 ["Title 1", "Title 2"],
28 ["This is\na multiline\ncell", "foo"],
29 ["Yo dawg ;) You can even\nprint tables\ninto tables", table1]
30 );
31 table2.printstd();
32}More examples
28fn main() {
29 let mut table = Table::new();
30 table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
31 table.add_row(row!["foobar", "bar", "foo"]);
32 table.add_row(Row::new(vec![
33 Cell::new("foobar2"),
34 Cell::new("bar2"),
35 Cell::new("foo2"),
36 ]));
37 table.printstd();
38 println!("Modified : ");
39 table.set_element("new_foo", 2, 1).unwrap();
40 table.printstd();
41
42 // The same table can be built the following way :
43 let _table = table!(
44 ["ABC", "DEFG", "HIJKLMN"],
45 ["foobar", "bar", "foo"],
46 ["foobar2", "bar2", "foo2"]
47 );
48
49 // Or directly print it like this
50 let _table = ptable!(
51 ["ABC", "DEFG", "HIJKLMN"],
52 ["foobar", "bar", "foo"],
53 ["foobar2", "bar2", "foo2"]
54 );
55}8fn main() {
9 /*
10 The following code will output
11
12 +---------------+---------------+--------------+
13 | A table with horizontal span |
14 +===============+===============+==============+
15 | This is a cell with span of 2 | span of 1 |
16 +---------------+---------------+--------------+
17 | span of 1 | span of 1 | span of 1 |
18 +---------------+---------------+--------------+
19 | This cell with a span of 3 is centered |
20 +---------------+---------------+--------------+
21 */
22
23 let mut table: prettytable::Table = table![
24 [H2 -> "This is a cell with span of 2", "span of 1"],
25 ["span of 1", "span of 1", "span of 1"],
26 [H03c -> "This cell with a span of 3 is centered"]
27 ];
28 table.set_titles(Row::new(vec![Cell::new_align(
29 "A table with horizontal span",
30 Alignment::CENTER,
31 )
32 .with_hspan(3)]));
33 table.printstd();
34}14fn main() {
15 let _ = table!();
16 let mut table = Table::new();
17 // Add style to a cell
18 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
19 // Add style to a full row
20 table.add_row(row![FY => "styled", "bar", "foo"]);
21 table.add_row(Row::new(vec![
22 Cell::new("foobar2"),
23 // Create a cell with a red foreground color
24 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
25 // Create a cell with red foreground color, yellow background color, with bold characters
26 Cell::new("foo2").style_spec("FrByb"),
27 // Using the cell! macro
28 cell!(Fr->"red"),
29 ]));
30
31 table.printstd();
32
33 // Print a table with some styles on it :
34 // FrBybl means : Foregound red, Background yellow, bold, left align
35 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
36
37 // You can also apply style to full rows :
38 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
39 // Set a title line, with all text centered in the cell
40 table.set_titles(row![c => "Title 1", "Title 2"]);
41 table.printstd();
42}7fn main() {
8 let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9 table.set_titles(row!["Title 1", "Title 2"]);
10
11 // Print
12 // +-------------+------------+
13 // | Title 1 | Title 2 |
14 // +-------------+------------+
15 // | Value 1 | Value 2 |
16 // | Value three | Value four |
17 // +-------------+------------+
18 println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19 table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20 table.printstd();
21 println!();
22
23 // Print
24 // -------------------------
25 // Title 1 Title 2
26 // =========================
27 // Value 1 Value 2
28 // -------------------------
29 // Value three Value four
30 // -------------------------
31 println!("FORMAT_NO_COLSEP :");
32 table.set_format(*format::consts::FORMAT_NO_COLSEP);
33 table.printstd();
34 println!();
35
36 // Print
37 // +-------------------------+
38 // | Title 1 Title 2 |
39 // +=========================+
40 // | Value 1 Value 2 |
41 // | Value three Value four |
42 // +-------------------------+
43 println!("FORMAT_BORDERS_ONLY :");
44 table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45 table.printstd();
46 println!();
47
48 // Custom format can be implemented using `prettytable::format::FormatBuilder`
49 // Example to print
50 // +-------------+------------+
51 // | Title 1 | Title 2 |
52 // | Value 1 | Value 2 |
53 // | Value three | Value four |
54 // +-------------+------------+
55 println!("Custom :");
56 table.set_format(
57 format::FormatBuilder::new()
58 .column_separator('|')
59 .borders('|')
60 .separators(
61 &[format::LinePosition::Top, format::LinePosition::Bottom],
62 format::LineSeparator::new('-', '+', '+', '+'),
63 )
64 .padding(1, 1)
65 .build(),
66 );
67 table.printstd();
68
69 // Customized format with unicode
70 // Example to print
71 // ┌─────────────┬────────────┐
72 // │ Title 1 │ Title 2 │
73 // ├─────────────┼────────────┤
74 // │ Value 1 │ Value 2 │
75 // ├─────────────┼────────────┤
76 // │ Value three │ Value four │
77 // └─────────────┴────────────┘
78 println!("With unicode:");
79 table.set_format(
80 format::FormatBuilder::new()
81 .column_separator('│')
82 .borders('│')
83 .separators(
84 &[format::LinePosition::Top],
85 format::LineSeparator::new('─', '┬', '┌', '┐'),
86 )
87 .separators(
88 &[format::LinePosition::Intern],
89 format::LineSeparator::new('─', '┼', '├', '┤'),
90 )
91 .separators(
92 &[format::LinePosition::Bottom],
93 format::LineSeparator::new('─', '┴', '└', '┘'),
94 )
95 .padding(1, 1)
96 .build(),
97 );
98 table.printstd();
99
100 // Customized format with unicode and different padding
101 // Example to print
102 // ┌───────────────┬──────────────┐
103 // │ Title 1 │ Title 2 │
104 // ├───────────────┼──────────────┤
105 // │ Value 1 │ Value 2 │
106 // ├───────────────┼──────────────┤
107 // │ Value three │ Value four │
108 // └───────────────┴──────────────┘
109 // Change individual format settings
110 println!("With unicode and padding:");
111 table.get_format().padding(2, 2);
112 table.printstd();
113}Trait Implementations§
Source§impl AsTableSlice for Table
impl AsTableSlice for Table
Source§fn as_slice(&self) -> TableSlice<'_>
fn as_slice(&self) -> TableSlice<'_>
Source§impl<A: Into<Row>> Extend<A> for Table
impl<A: Into<Row>> Extend<A> for Table
Source§fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)