summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-10 21:28:23 +0200
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-10 22:23:32 +0200
commit27979dd617bfb9af83dc3801eb4372f3a38812a3 (patch)
treee456a7d6453e640e2c4b3b2ad4bb7eace1a3a71b
parent3fe5c39fe453594900aec4048134f051e0528872 (diff)
Capability to update row (append, insert, remove cells)
-rw-r--r--TODO.md6
-rw-r--r--src/cell.rs2
-rw-r--r--src/lib.rs14
-rw-r--r--src/main.rs2
-rw-r--r--src/row.rs34
5 files changed, 42 insertions, 16 deletions
diff --git a/TODO.md b/TODO.md
index 7ab9bbf..0e8ad49 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,14 +2,12 @@
## Features :
* Select a range of rows to print
-* Capability to store object implementing the "std::string::ToString" trait
-* Capability to not use line separators
+* Capability to not use/print line separators
* Manage formatting with "term" library
* Limit cell width and split content if needed
* Limit table width and auto adjust cell width as needed
* Create a table format struct, and provide some default format
-* Capability to update row (append, insert, remove cells)
-* Add capability to print a special line separator after first line (for titles for example), or just empty lines, so 2 spearators would be printed
+* Add capability to print a special line separator after first line (for titles for example)
## Improvements :
diff --git a/src/cell.rs b/src/cell.rs
index da1ef63..b3caacf 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -15,7 +15,7 @@ pub struct Cell {
impl Cell {
/// Create a new `Cell` initialized with content from `string`
pub fn new(string: &String) -> Cell {
- let content: Vec<String> = string.lines_any().map(|x| x.to_string()).collect();
+ let content: Vec<String> = string.lines_any().map(|ref x| x.to_string()).collect();
let mut width = 0;
for cont in &content {
let l = cont.len();
diff --git a/src/lib.rs b/src/lib.rs
index 4a6f4ad..d7a7806 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -85,11 +85,11 @@ impl Table {
/// Append an empty row in the table. Return a mutable reference to this new row.
pub fn add_empty_row(&mut self) -> &mut Row {
- let n = self.get_column_num();
- return self.add_row(Row::empty(n));
+ return self.add_row(Row::default());
}
- /// Insert `row` at the position `index`, and return a mutable reference to this 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
pub fn insert_row(&mut self, index: usize, row: Row) -> &mut Row {
if index < self.rows.len() {
self.rows.insert(index, row);
@@ -105,10 +105,10 @@ impl Table {
return rowline.set_cell(Cell::new(element), column);
}
- /// Remove a row. Silently skip if row with index `row` does not exist
- pub fn remove_row(&mut self, row: usize) {
- if row < self.rows.len() {
- self.rows.remove(row);
+ /// Remove the row at position `index`. Silently skip if the row does not exist
+ pub fn remove_row(&mut self, index: usize) {
+ if index < self.rows.len() {
+ self.rows.remove(index);
}
}
diff --git a/src/main.rs b/src/main.rs
index f36d829..2c968f7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,7 +15,7 @@ fn main() {
);
table.printstd();
println!("Modified : ");
- table.separators('*', '*', '*');
+ table.separators('|', '=', '+');
table.set_element(&"new_foo".to_string(), 2, 1).unwrap();
table.printstd();
diff --git a/src/row.rs b/src/row.rs
index 2ccca0b..cbf1e91 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -20,8 +20,8 @@ impl Row {
}
/// Create an row of length `size`, with empty strings stored
- pub fn empty(size: usize) -> Row {
- return Self::new(vec![Cell::default(); size]);
+ pub fn empty() -> Row {
+ return Self::new(vec![Cell::default(); 0]);
}
/// Get the number of cells in this row
@@ -58,12 +58,34 @@ impl Row {
/// Set the `cell` in the row at the given `column`
pub fn set_cell(&mut self, cell: Cell, column: usize) -> Result<(), &str> {
if column >= self.len() {
- return Err("Column index is higher than expected");
+ return Err("Cannot find cell");
}
self.cells[column] = cell;
return Ok(());
}
+ /// Append a `cell` at the end of the row
+ pub fn add_cell(&mut self, cell: Cell) {
+ self.cells.push(cell);
+ }
+
+ /// Insert `cell` at position `index`. If `index` is higher than the row lenght,
+ /// the cell will be appended at the end
+ pub fn insert_cell(&mut self, index: usize, cell: Cell) {
+ if index < self.cells.len() {
+ self.cells.insert(index, cell);
+ } else {
+ self.add_cell(cell);
+ }
+ }
+
+ /// Remove the cell at position `index`. Silently skip if this cell does not exist
+ pub fn remove_cell(&mut self, index: usize) {
+ if index < self.cells.len() {
+ self.cells.remove(index);
+ }
+ }
+
/// Print the row to `out`, with `separator` as column separator, and `col_width`
/// specifying the width of each columns
pub fn print<T: Write>(&self, out: &mut T, separator: char, col_width: &[usize]) -> Result<(), Error> {
@@ -82,6 +104,12 @@ impl Row {
}
}
+impl Default for Row {
+ fn default() -> Row {
+ return Row::empty();
+ }
+}
+
impl <A: ToString> FromIterator<A> for Row {
fn from_iter<T>(iterator: T) -> Row where T: IntoIterator<Item=A> {
return Self::new(iterator.into_iter().map(|ref e| Cell::from(e)).collect());