Struct prettytable::Row
source · 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 25-29)
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
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.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"]
);
// Or directly print it like this
let _table = ptable!(
["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]
);
}
More examples
examples/span.rs (lines 23-27)
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
fn main() {
/*
The following code will output
+---------------+---------------+--------------+
| A table with horizontal span |
+===============+===============+==============+
| This is a cell with span of 2 | span of 1 |
+---------------+---------------+--------------+
| span of 1 | span of 1 | span of 1 |
+---------------+---------------+--------------+
| This cell with a span of 3 is centered |
+---------------+---------------+--------------+
*/
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)]));
table.printstd();
}
examples/style.rs (lines 13-21)
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
fn main() {
let _ = table!();
let mut table = Table::new();
// Add style to a cell
table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
// 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"),
]));
table.printstd();
// Print a table with some styles on it :
// FrBybl means : Foregound red, Background yellow, bold, left align
ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
// You can also apply style to full rows :
let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
// Set a title line, with all text centered in the cell
table.set_titles(row![c => "Title 1", "Title 2"]);
table.printstd();
}
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 45)
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
fn main() {
let mut table = table![
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
];
let mut height = table.print_tty(false).unwrap();
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut current = CROSS;
let mut terminal = term::stdout().unwrap();
loop {
let mut line = String::new();
print!("{} plays > ", current);
height += 1;
stdout.flush().unwrap();
stdin.read_line(&mut line).expect("Cannot read input");
let i = match usize::from_str(line.trim()) {
Ok(i) => i,
_ => {
println!("Bad input");
height += 1;
continue;
}
};
if i < 1 || i > 9 {
println!("Bad input, should be between 1 and 9");
height += 1;
continue;
}
let x = (i - 1) % 3;
let y = (i - 1) / 3;
{
let row = table.get_mut_row(y).unwrap();
if row.get_cell(x).unwrap().to_string() != EMPTY {
println!("There's already someone there");
height += 1;
continue;
}
row.set_cell(cell!(current), x).unwrap();
}
for _ in 0..height {
terminal.cursor_up().unwrap();
terminal.delete_line().unwrap();
}
height = table.print_tty(false).unwrap();
if check(&table) {
return;
}
if current == CROSS {
current = ROUND;
} else {
current = CROSS;
}
}
}
fn get(table: &Table, x: usize, y: usize) -> String {
match table.get_row(y) {
Some(r) => match r.get_cell(x) {
Some(c) => c.to_string(),
_ => EMPTY.to_string(),
},
_ => EMPTY.to_string(),
}
}
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 50)
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
fn main() {
let mut table = table![
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
];
let mut height = table.print_tty(false).unwrap();
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut current = CROSS;
let mut terminal = term::stdout().unwrap();
loop {
let mut line = String::new();
print!("{} plays > ", current);
height += 1;
stdout.flush().unwrap();
stdin.read_line(&mut line).expect("Cannot read input");
let i = match usize::from_str(line.trim()) {
Ok(i) => i,
_ => {
println!("Bad input");
height += 1;
continue;
}
};
if i < 1 || i > 9 {
println!("Bad input, should be between 1 and 9");
height += 1;
continue;
}
let x = (i - 1) % 3;
let y = (i - 1) / 3;
{
let row = table.get_mut_row(y).unwrap();
if row.get_cell(x).unwrap().to_string() != EMPTY {
println!("There's already someone there");
height += 1;
continue;
}
row.set_cell(cell!(current), x).unwrap();
}
for _ in 0..height {
terminal.cursor_up().unwrap();
terminal.delete_line().unwrap();
}
height = table.print_tty(false).unwrap();
if check(&table) {
return;
}
if current == CROSS {
current = ROUND;
} else {
current = CROSS;
}
}
}
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