summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-05-28 14:04:34 +0200
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-05-28 14:04:34 +0200
commit91273a63c1ab6de1a514827bbca355dde38e4975 (patch)
tree2884f0502ef4b2e8ade6f2db7718974becaaa567
parent88d38fb52b3d915383a171da92deb2f10c562a68 (diff)
Added doc comments, readme file and travis config file
-rw-r--r--.travis.yml2
-rw-r--r--README.md46
-rw-r--r--example/basic.rs19
-rw-r--r--src/lib.rs13
4 files changed, 80 insertions, 0 deletions
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..51528a5
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,2 @@
+language: rust
+rust: 1.0.0
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0d59bd9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,46 @@
+![License](http://img.shields.io/badge/license-BSD-lightgrey.svg)
+[![Build Status](https://travis-ci.org/phsym/tabprint.svg)](https://travis-ci.org/phsymtabprint)
+
+# tabprint
+
+*Copyright &copy; 2015 Pierre-Henri Symoneaux*
+
+> THIS SOFTWARE IS DISTRIBUTED WITHOUT ANY WARRANTY <br>
+> Check LICENSE.txt file for more information. <br>
+
+A formatted and aligned table print written in rust
+
+# How to build
+
+As usual with Cargo project, simply run
+
+> cargo build
+
+And to build html documentation, run
+
+> cargo doc
+
+# How to use
+More often, you will include the library as a dependency to your project. In order to do this, add the following lines to your **Cargo.toml** file :
+
+```toml
+[dependencies.tabprint]
+git = "https://github.com/phsym/tabprint.git"
+
+```
+
+Then you can start using it the following way :
+
+```rust
+#[macro_use] extern crate tabprint;
+use tabprint::Table
+
+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();
+}
+```
+
+Additional examples are provided in documentation and in [examples](./examples/) directory
diff --git a/example/basic.rs b/example/basic.rs
new file mode 100644
index 0000000..b9df205
--- /dev/null
+++ b/example/basic.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/src/lib.rs b/src/lib.rs
index 1ced760..74d61e4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,5 @@
+
+/// A Struct representing a printable table
pub struct Table {
num_cols: usize,
titles: Vec<String>,
@@ -5,6 +7,7 @@ pub struct Table {
}
impl Table {
+ /// Create a new table with the number of columns equals to the length of `titles`
pub fn new(titles: Vec<String>) -> Table {
let n = titles.len();
return Table {
@@ -14,22 +17,28 @@ impl Table {
};
}
+ /// Get the number of column
pub fn get_column_num(&self) -> usize {
return self.num_cols;
}
+ /// Get the number of rows
pub fn get_rows_number(&self) -> usize {
return self.rows.len();
}
+ /// Get a mutable reference to a row
pub fn get_mut_row(&mut self, row: usize) -> &mut Vec<String> {
return &mut self.rows[row];
}
+ /// Get an immutable reference to a row
pub fn get_row(&self, row: usize) -> &Vec<String> {
return &self.rows[row];
}
+ /// Append a row in the table, transferring ownership of this row to the table
+ /// and returning a mutable reference to the row
pub fn add_row(&mut self, row: Vec<String>) -> Result<&mut Vec<String>, &str> {
if row.len() != self.num_cols {
return Err("Row does not have the proper number of column");
@@ -39,11 +48,13 @@ impl Table {
return Ok(self.get_mut_row(l));
}
+ /// Append an empty row in the table. Return a mutable reference to this new row.
pub fn add_empty_row(&mut self) -> Result<&mut Vec<String>, &str> {
let n = self.num_cols;
return Ok(try!(self.add_row(vec!["".to_string(); n])));
}
+ /// Modify a single element in the table
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");
@@ -59,6 +70,7 @@ impl Table {
return Ok(());
}
+ /// Remove a row
pub fn remove_row(&mut self, row: usize) {
self.rows.remove(row);
}
@@ -100,6 +112,7 @@ impl Table {
println!("");
}
+ /// Print the table to `stdout`
pub fn print(&self) {
let mut col_width = vec![0usize; self.num_cols];
for i in 0..self.num_cols {