From 88d38fb52b3d915383a171da92deb2f10c562a68 Mon Sep 17 00:00:00 2001 From: pierresy Date: Thu, 28 May 2015 12:42:56 +0200 Subject: Moved root directory --- .gitignore | 2 + .project | 17 ++++++++ Cargo.toml | 9 ++++ src/lib.rs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 19 +++++++++ tabprint/.gitignore | 2 - tabprint/.project | 17 -------- tabprint/Cargo.toml | 9 ---- tabprint/src/lib.rs | 116 --------------------------------------------------- tabprint/src/main.rs | 19 --------- 10 files changed, 163 insertions(+), 163 deletions(-) create mode 100644 .gitignore create mode 100644 .project create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs delete mode 100644 tabprint/.gitignore delete mode 100644 tabprint/.project delete mode 100644 tabprint/Cargo.toml delete mode 100644 tabprint/src/lib.rs delete mode 100644 tabprint/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9e2199 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target/ +/Cargo.lock diff --git a/.project b/.project new file mode 100644 index 0000000..85a2e6a --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + tabprint + + + + + + com.github.rustdt.ide.core.Builder + + + + + + com.github.rustdt.ide.core.nature + + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..47a1256 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] + +name = "tabprint" +version = "0.0.1" +authors = [ "Pierre-Henri Symoneaux" ] + +[[bin]] + +name = "tabprint" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1ced760 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,116 @@ +pub struct Table { + num_cols: usize, + titles: Vec, + rows: Vec> +} + +impl Table { + pub fn new(titles: Vec) -> Table { + let n = titles.len(); + return Table { + num_cols: n, + titles: titles, + rows: Vec::new() + }; + } + + pub fn get_column_num(&self) -> usize { + return self.num_cols; + } + + pub fn get_rows_number(&self) -> usize { + return self.rows.len(); + } + + pub fn get_mut_row(&mut self, row: usize) -> &mut Vec { + return &mut self.rows[row]; + } + + pub fn get_row(&self, row: usize) -> &Vec { + return &self.rows[row]; + } + + pub fn add_row(&mut self, row: Vec) -> Result<&mut Vec, &str> { + if row.len() != self.num_cols { + return Err("Row does not have the proper number of column"); + } + self.rows.push(row); + let l = self.rows.len()-1; + return Ok(self.get_mut_row(l)); + } + + pub fn add_empty_row(&mut self) -> Result<&mut Vec, &str> { + let n = self.num_cols; + return Ok(try!(self.add_row(vec!["".to_string(); n]))); + } + + pub fn set_element(&mut self, element: String, column: usize, row: usize) -> Result<(), &str> { + if column >= self.num_cols { + return Err("Column index is higher than expected"); + } + let rowline: &mut Vec; + if row > self.rows.len() { + rowline = try!(self.add_empty_row()); + } + else { + rowline = self.get_mut_row(row); + } + rowline[column] = element; + return Ok(()); + } + + pub fn remove_row(&mut self, row: usize) { + self.rows.remove(row); + } + + fn get_col_width(&self, col_idx: usize) -> Result { + if col_idx >= self.num_cols { + return Err("Column index is too high"); + } + let mut width = self.titles[col_idx].len(); + for r in &self.rows { + let l = r[col_idx].len(); + if l > width { + width = l; + } + } + return Ok(width); + } + + fn print_line_separator(&self, col_width: &[usize]) { + print!("+"); + for i in 0..self.num_cols { + for _ in 0..(col_width[i] + 2) { + print!("-"); + } + print!("+"); + } + println!(""); + } + + fn print_line(&self, line: &[String], col_width: &[usize]) { + print!("|"); + for i in 0..self.num_cols { + print!(" {} ", line[i]); + for _ in 0..(col_width[i] - line[i].len()) { + print!(" "); + } + print!("|"); + } + println!(""); + } + + pub fn print(&self) { + let mut col_width = vec![0usize; self.num_cols]; + for i in 0..self.num_cols { + col_width[i] = self.get_col_width(i).unwrap(); + } + self.print_line_separator(&col_width); + self.print_line(&self.titles, &col_width); + self.print_line_separator(&col_width); + for r in &self.rows { + self.print_line(r, &col_width); + self.print_line_separator(&col_width); + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b9df205 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,19 @@ +extern crate tabprint; +use tabprint::Table; + +/* + Following main function will print : + +---------+------+---------+ + | ABC | DEFG | HIJKLMN | + +---------+------+---------+ + | foobar | bar | foo | + +---------+------+---------+ + | foobar2 | bar2 | foo2 | + +---------+------+---------+ +*/ +fn main() { + let mut table = Table::new(vec!["ABC".to_string(), "DEFG".to_string(), "HIJKLMN".to_string()]); + table.add_row(vec!["foobar".to_string(), "bar".to_string(), "foo".to_string()]).unwrap(); + table.add_row(vec!["foobar2".to_string(), "bar2".to_string(), "foo2".to_string()]).unwrap(); + table.print(); +} diff --git a/tabprint/.gitignore b/tabprint/.gitignore deleted file mode 100644 index e9e2199..0000000 --- a/tabprint/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target/ -/Cargo.lock diff --git a/tabprint/.project b/tabprint/.project deleted file mode 100644 index 85a2e6a..0000000 --- a/tabprint/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - tabprint - - - - - - com.github.rustdt.ide.core.Builder - - - - - - com.github.rustdt.ide.core.nature - - diff --git a/tabprint/Cargo.toml b/tabprint/Cargo.toml deleted file mode 100644 index 47a1256..0000000 --- a/tabprint/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] - -name = "tabprint" -version = "0.0.1" -authors = [ "Pierre-Henri Symoneaux" ] - -[[bin]] - -name = "tabprint" \ No newline at end of file diff --git a/tabprint/src/lib.rs b/tabprint/src/lib.rs deleted file mode 100644 index 1ced760..0000000 --- a/tabprint/src/lib.rs +++ /dev/null @@ -1,116 +0,0 @@ -pub struct Table { - num_cols: usize, - titles: Vec, - rows: Vec> -} - -impl Table { - pub fn new(titles: Vec) -> Table { - let n = titles.len(); - return Table { - num_cols: n, - titles: titles, - rows: Vec::new() - }; - } - - pub fn get_column_num(&self) -> usize { - return self.num_cols; - } - - pub fn get_rows_number(&self) -> usize { - return self.rows.len(); - } - - pub fn get_mut_row(&mut self, row: usize) -> &mut Vec { - return &mut self.rows[row]; - } - - pub fn get_row(&self, row: usize) -> &Vec { - return &self.rows[row]; - } - - pub fn add_row(&mut self, row: Vec) -> Result<&mut Vec, &str> { - if row.len() != self.num_cols { - return Err("Row does not have the proper number of column"); - } - self.rows.push(row); - let l = self.rows.len()-1; - return Ok(self.get_mut_row(l)); - } - - pub fn add_empty_row(&mut self) -> Result<&mut Vec, &str> { - let n = self.num_cols; - return Ok(try!(self.add_row(vec!["".to_string(); n]))); - } - - pub fn set_element(&mut self, element: String, column: usize, row: usize) -> Result<(), &str> { - if column >= self.num_cols { - return Err("Column index is higher than expected"); - } - let rowline: &mut Vec; - if row > self.rows.len() { - rowline = try!(self.add_empty_row()); - } - else { - rowline = self.get_mut_row(row); - } - rowline[column] = element; - return Ok(()); - } - - pub fn remove_row(&mut self, row: usize) { - self.rows.remove(row); - } - - fn get_col_width(&self, col_idx: usize) -> Result { - if col_idx >= self.num_cols { - return Err("Column index is too high"); - } - let mut width = self.titles[col_idx].len(); - for r in &self.rows { - let l = r[col_idx].len(); - if l > width { - width = l; - } - } - return Ok(width); - } - - fn print_line_separator(&self, col_width: &[usize]) { - print!("+"); - for i in 0..self.num_cols { - for _ in 0..(col_width[i] + 2) { - print!("-"); - } - print!("+"); - } - println!(""); - } - - fn print_line(&self, line: &[String], col_width: &[usize]) { - print!("|"); - for i in 0..self.num_cols { - print!(" {} ", line[i]); - for _ in 0..(col_width[i] - line[i].len()) { - print!(" "); - } - print!("|"); - } - println!(""); - } - - pub fn print(&self) { - let mut col_width = vec![0usize; self.num_cols]; - for i in 0..self.num_cols { - col_width[i] = self.get_col_width(i).unwrap(); - } - self.print_line_separator(&col_width); - self.print_line(&self.titles, &col_width); - self.print_line_separator(&col_width); - for r in &self.rows { - self.print_line(r, &col_width); - self.print_line_separator(&col_width); - } - } -} \ No newline at end of file diff --git a/tabprint/src/main.rs b/tabprint/src/main.rs deleted file mode 100644 index b9df205..0000000 --- a/tabprint/src/main.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate tabprint; -use tabprint::Table; - -/* - Following main function will print : - +---------+------+---------+ - | ABC | DEFG | HIJKLMN | - +---------+------+---------+ - | foobar | bar | foo | - +---------+------+---------+ - | foobar2 | bar2 | foo2 | - +---------+------+---------+ -*/ -fn main() { - let mut table = Table::new(vec!["ABC".to_string(), "DEFG".to_string(), "HIJKLMN".to_string()]); - table.add_row(vec!["foobar".to_string(), "bar".to_string(), "foo".to_string()]).unwrap(); - table.add_row(vec!["foobar2".to_string(), "bar2".to_string(), "foo2".to_string()]).unwrap(); - table.print(); -} -- cgit v1.2.3