pub struct Row { /* private fields */ }Expand description
Represent a table row made of cells
Implementations§
Source§impl Row
impl Row
Sourcepub fn new(cells: Vec<Cell>) -> Row
pub fn new(cells: Vec<Cell>) -> Row
Create a new Row backed with cells vector
Examples found in repository?
examples/basic.rs (lines 32-36)
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
examples/span.rs (lines 28-32)
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}examples/style.rs (lines 21-29)
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 get_cell(&self, idx: usize) -> Option<&Cell>
pub fn get_cell(&self, idx: usize) -> Option<&Cell>
Get the cell at index idx
Examples found in repository?
examples/tictactoe.rs (line 49)
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}
71
72fn get(table: &Table, x: usize, y: usize) -> String {
73 match table.get_row(y) {
74 Some(r) => match r.get_cell(x) {
75 Some(c) => c.to_string(),
76 _ => EMPTY.to_string(),
77 },
78 _ => EMPTY.to_string(),
79 }
80}Sourcepub fn get_mut_cell(&mut self, idx: usize) -> Option<&mut Cell>
pub fn get_mut_cell(&mut self, idx: usize) -> Option<&mut Cell>
Get the mutable cell at index idx
Sourcepub fn set_cell(&mut self, cell: Cell, idx: usize) -> Result<(), &str>
pub fn set_cell(&mut self, cell: Cell, idx: usize) -> Result<(), &str>
Set the cell in the row at the given idx index
Examples found in repository?
examples/tictactoe.rs (line 54)
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 insert_cell(&mut self, index: usize, cell: Cell)
pub fn insert_cell(&mut self, index: usize, cell: Cell)
Insert cell at position index. If index is higher than the row length,
the cell will be appended at the end
Sourcepub fn remove_cell(&mut self, index: usize)
pub fn remove_cell(&mut self, index: usize)
Remove the cell at position index. Silently skip if this cell does not exist
Trait Implementations§
Source§impl<S: ToString> Extend<S> for Row
impl<S: ToString> Extend<S> for Row
Source§fn extend<T: IntoIterator<Item = S>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = S>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<A: ToString> FromIterator<A> for Row
impl<A: ToString> FromIterator<A> for Row
Source§impl FromIterator<Row> for Table
impl FromIterator<Row> for Table
Source§impl<'a> IntoIterator for &'a Row
impl<'a> IntoIterator for &'a Row
Source§impl<'a> IntoIterator for &'a mut Row
impl<'a> IntoIterator for &'a mut Row
impl Eq for Row
impl StructuralPartialEq for Row
Auto Trait Implementations§
impl Freeze for Row
impl RefUnwindSafe for Row
impl Send for Row
impl Sync for Row
impl Unpin for Row
impl UnwindSafe for Row
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more