summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cell.rs27
-rw-r--r--src/format.rs42
-rw-r--r--src/lib.rs90
-rw-r--r--src/row.rs41
4 files changed, 94 insertions, 106 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 1aca166..29aa3e3 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -42,7 +42,7 @@ impl Cell {
/// Create a new `Cell` initialized with content from `string`.
/// By default, content is align to `LEFT`
pub fn new(string: &str) -> Cell {
- return Cell::new_align(string, Alignment::LEFT);
+ Cell::new_align(string, Alignment::LEFT)
}
/// Set text alignment in the cell
@@ -58,7 +58,7 @@ impl Cell {
/// Add a style attribute to the cell. Can be chained
pub fn with_style(mut self, attr: Attr) -> Cell {
self.style(attr);
- return self;
+ self
}
/// Remove all style attributes and reset alignment to default (LEFT)
@@ -157,17 +157,17 @@ impl Cell {
/// Return the height of the cell
pub fn get_height(&self) -> usize {
- return self.content.len();
+ self.content.len()
}
/// Return the width of the cell
pub fn get_width(&self) -> usize {
- return self.width;
+ self.width
}
/// Return a copy of the full string contained in the cell
pub fn get_content(&self) -> String {
- return self.content.join("\n");
+ self.content.join("\n")
}
/// Print a partial cell to `out`. Since the cell may be multi-lined,
@@ -175,11 +175,8 @@ impl Cell {
/// fill the cells with blanks so it fits in the table.
/// If `ìdx` is higher than this cell's height, it will print empty content
pub fn print<T: Write+?Sized>(&self, out: &mut T, idx: usize, col_width: usize, skip_right_fill: bool) -> Result<(), Error> {
- let c = match self.content.get(idx) {
- Some(s) => s.as_ref(),
- None => ""
- };
- return print_align(out, self.align, c, ' ', col_width, skip_right_fill)
+ let c = self.content.get(idx).map(|s| s.as_ref()).unwrap_or("");
+ print_align(out, self.align, c, ' ', col_width, skip_right_fill)
}
/// Apply style then call `print` to print the cell into a terminal
@@ -193,7 +190,7 @@ impl Cell {
}
try!(self.print(out, idx, col_width, skip_right_fill));
try!(out.reset().map_err(term_error_to_io_error));
- return Ok(());
+ Ok(())
}
}
@@ -206,25 +203,25 @@ fn term_error_to_io_error(te: ::term::Error) -> Error {
impl <'a, T: ToString> From<&'a T> for Cell {
fn from(f: &T) -> Cell {
- return Cell::new(&f.to_string());
+ Cell::new(&f.to_string())
}
}
impl ToString for Cell {
fn to_string(&self) -> String {
- return self.get_content();
+ self.get_content()
}
}
impl Default for Cell {
/// Return a cell initialized with a single empty `String`, with LEFT alignment
fn default() -> Cell {
- return Cell {
+ Cell {
content: vec!["".to_string(); 1],
width: 0,
align: Alignment::LEFT,
style: Vec::new()
- };
+ }
}
}
diff --git a/src/format.rs b/src/format.rs
index 6a973aa..ae72e59 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -47,7 +47,7 @@ impl LineSeparator {
/// Create a new line separator instance where `line` is the character used to separate 2 lines
/// and `junc` is the one used for junctions between columns and lines
pub fn new(line: char, junc: char, ljunc: char, rjunc: char) -> LineSeparator {
- return LineSeparator{line: line, junc: junc, ljunc: ljunc, rjunc: rjunc};
+ LineSeparator{line: line, junc: junc, ljunc: ljunc, rjunc: rjunc}
}
/// Print a full line separator to `out`. `col_width` is a slice containing the width of each column
@@ -65,13 +65,13 @@ impl LineSeparator {
if rborder {
try!(out.write_all(&[self.rjunc as u8]));
}
- return out.write_all(NEWLINE);
+ out.write_all(NEWLINE)
}
}
impl Default for LineSeparator {
fn default() -> Self {
- return LineSeparator::new('-', '+', '+', '+');
+ LineSeparator::new('-', '+', '+', '+')
}
}
@@ -102,7 +102,7 @@ impl TableFormat {
/// Create a new empty TableFormat.
pub fn new() -> TableFormat {
- return TableFormat{
+ TableFormat{
csep: None,
lborder: None,
rborder: None,
@@ -112,12 +112,12 @@ impl TableFormat {
bottom_sep: None,
pad_left: 0,
pad_right: 0
- };
+ }
}
/// Return a tuple with left and right padding
pub fn get_padding(&self) -> (usize, usize) {
- return (self.pad_left, self.pad_right);
+ (self.pad_left, self.pad_right)
}
/// Set left and right padding
@@ -168,32 +168,32 @@ impl TableFormat {
/// Print a full line separator to `out`. `col_width` is a slice containing the width of each column
pub fn print_line_separator<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize], pos: LinePosition) -> Result<(), Error> {
- return match *self.get_sep_for_line(pos) {
+ match *self.get_sep_for_line(pos) {
Some(ref l) => l.print(out, col_width, self.csep.is_some(), self.lborder.is_some(), self.rborder.is_some()),
None => Ok(())
- };
+ }
}
pub fn get_column_separator(&self, pos: ColumnPosition) -> Option<char> {
- return match pos {
+ match pos {
ColumnPosition::Left => self.lborder,
ColumnPosition::Intern => self.csep,
ColumnPosition::Right => self.rborder
- };
+ }
}
/// Print a column separator or a table border
pub fn print_column_separator<T: Write+?Sized>(&self, out: &mut T, pos: ColumnPosition) -> Result<(), Error> {
- return match self.get_column_separator(pos) {
+ match self.get_column_separator(pos) {
Some(s) => out.write_all(&[s as u8]),
None => Ok(())
- };
+ }
}
}
impl Default for TableFormat {
fn default() -> Self {
- return TableFormat::new();
+ TableFormat::new()
}
}
@@ -204,44 +204,44 @@ pub struct FormatBuilder {
impl FormatBuilder {
pub fn new() -> FormatBuilder {
- return FormatBuilder {
+ FormatBuilder {
format: Box::new(TableFormat::new())
- };
+ }
}
/// Set left and right padding
pub fn padding(mut self, left: usize, right: usize) -> Self {
self.format.padding(left, right);
- return self;
+ self
}
/// Set the character used for internal column separation
pub fn column_separator(mut self, separator: char) -> Self {
self.format.column_separator(separator);
- return self;
+ self
}
/// Set the character used for table borders
pub fn borders(mut self, border: char) -> Self {
self.format.borders(border);
- return self;
+ self
}
/// Set a line separator format
pub fn separator(mut self, what: LinePosition, separator: LineSeparator) -> Self {
self.format.separator(what, separator);
- return self;
+ self
}
/// Set separator format for multiple kind of line separators
pub fn separators(mut self, what: &[LinePosition], separator: LineSeparator) -> Self {
self.format.separators(what, separator);
- return self;
+ self
}
/// Consume this builder and return the generated `TableFormat`
pub fn build(self) -> TableFormat {
- return *self.format;
+ *self.format
}
}
diff --git a/src/lib.rs b/src/lib.rs
index ebf587e..2b72a4a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -70,17 +70,17 @@ impl <'a> TableSlice<'a> {
cnum = l;
}
}
- return cnum;
+ cnum
}
/// Get the number of rows
pub fn len(&self) -> usize {
- return self.rows.len();
+ self.rows.len()
}
/// Get an immutable reference to a row
pub fn get_row(&self, row: usize) -> Option<&Row> {
- return self.rows.get(row);
+ self.rows.get(row)
}
/// Get the width of the column at position `col_idx`.
@@ -96,7 +96,7 @@ impl <'a> TableSlice<'a> {
width = l;
}
}
- return width;
+ width
}
/// Get the width of all columns, and return a slice
@@ -107,12 +107,12 @@ impl <'a> TableSlice<'a> {
for i in 0..colnum {
col_width[i] = self.get_column_width(i);
}
- return col_width;
+ col_width
}
/// Returns an iterator over the immutable cells of the column specified by `column`
pub fn column_iter(&self, column: usize) -> ColumnIter {
- return ColumnIter(self.rows.iter(), column);
+ ColumnIter(self.rows.iter(), column)
}
/// Returns an iterator over immutable rows
@@ -139,17 +139,17 @@ impl <'a> TableSlice<'a> {
}
}
try!(self.format.print_line_separator(out, &col_width, LinePosition::Bottom));
- return out.flush();
+ out.flush()
}
/// Print the table to `out`
pub fn print<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
- return self.__print(out, Row::print);
+ self.__print(out, Row::print)
}
/// Print the table to terminal `out`, applying styles when needed
pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T) -> Result<(), Error> {
- return self.__print(out, Row::print_term);
+ self.__print(out, Row::print_term)
}
/// Print the table to standard output. Colors won't be displayed unless
@@ -186,23 +186,23 @@ impl <'a> IntoIterator for &'a TableSlice<'a> {
type Item=&'a Row;
type IntoIter=Iter<'a, Row>;
fn into_iter(self) -> Self::IntoIter {
- return self.row_iter();
+ self.row_iter()
}
}
impl Table {
/// Create an empty table
pub fn new() -> Table {
- return Self::init(Vec::new());
+ Self::init(Vec::new())
}
/// Create a table initialized with `rows`
pub fn init(rows: Vec<Row>) -> Table {
- return Table {
+ Table {
rows: rows,
titles: Box::new(None),
format: Box::new(*consts::FORMAT_DEFAULT)
- };
+ }
}
/// Change the table format. Eg : Separators
@@ -212,12 +212,12 @@ impl Table {
/// Compute and return the number of column
pub fn get_column_num(&self) -> usize {
- return self.as_ref().get_column_num();
+ self.as_ref().get_column_num()
}
/// Get the number of rows
pub fn len(&self) -> usize {
- return self.rows.len();
+ self.rows.len()
}
/// Set the optional title lines
@@ -232,12 +232,12 @@ impl Table {
/// Get a mutable reference to a row
pub fn get_mut_row(&mut self, row: usize) -> Option<&mut Row> {
- return self.rows.get_mut(row);
+ self.rows.get_mut(row)
}
/// Get an immutable reference to a row
pub fn get_row(&self, row: usize) -> Option<&Row> {
- return self.rows.get(row);
+ self.rows.get(row)
}
/// Append a row in the table, transferring ownership of this row to the table
@@ -245,12 +245,12 @@ impl Table {
pub fn add_row(&mut self, row: Row) -> &mut Row {
self.rows.push(row);
let l = self.rows.len()-1;
- return &mut self.rows[l];
+ &mut self.rows[l]
}
/// Append an empty row in the table. Return a mutable reference to this new row.
pub fn add_empty_row(&mut self) -> &mut Row {
- return self.add_row(Row::default());
+ self.add_row(Row::default())
}
/// Insert `row` at the position `index`, and return a mutable reference to this row.
@@ -258,9 +258,9 @@ impl Table {
pub fn insert_row(&mut self, index: usize, row: Row) -> &mut Row {
if index < self.rows.len() {
self.rows.insert(index, row);
- return &mut self.rows[index];
+ &mut self.rows[index]
} else {
- return self.add_row(row);
+ self.add_row(row)
}
}
@@ -268,7 +268,7 @@ impl Table {
pub fn set_element(&mut self, element: &str, column: usize, row: usize) -> Result<(), &str> {
let rowline = try!(self.get_mut_row(row).ok_or("Cannot find row"));
// TODO: If a cell already exist, copy it's alignment parameter
- return rowline.set_cell(Cell::new(element), column);
+ rowline.set_cell(Cell::new(element), column)
}
/// Remove the row at position `index`. Silently skip if the row does not exist
@@ -280,12 +280,12 @@ impl Table {
/// Return an iterator over the immutable cells of the column specified by `column`
pub fn column_iter(&self, column: usize) -> ColumnIter {
- return ColumnIter(self.rows.iter(), column);
+ ColumnIter(self.rows.iter(), column)
}
/// Return an iterator over the mutable cells of the column specified by `column`
pub fn column_iter_mut(&mut self, column: usize) -> ColumnIterMut {
- return ColumnIterMut(self.rows.iter_mut(), column);
+ ColumnIterMut(self.rows.iter_mut(), column)
}
/// Returns an iterator over immutable rows
@@ -300,12 +300,12 @@ impl Table {
/// Print the table to `out`
pub fn print<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
- return self.as_ref().print(out);
+ self.as_ref().print(out)
}
/// Print the table to terminal `out`, applying styles when needed
pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T) -> Result<(), Error> {
- return self.as_ref().print_term(out);
+ self.as_ref().print_term(out)
}
/// Print the table to standard output. Colors won't be displayed unless
@@ -335,26 +335,26 @@ impl Table {
impl Index<usize> for Table {
type Output = Row;
fn index(&self, idx: usize) -> &Self::Output {
- return &self.rows[idx];
+ &self.rows[idx]
}
}
impl <'a> Index<usize> for TableSlice<'a> {
type Output = Row;
fn index(&self, idx: usize) -> &Self::Output {
- return &self.rows[idx];
+ &self.rows[idx]
}
}
impl IndexMut<usize> for Table {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
- return &mut self.rows[idx];
+ &mut self.rows[idx]
}
}
impl fmt::Display for Table {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- return self.as_ref().fmt(fmt);
+ self.as_ref().fmt(fmt)
}
}
@@ -364,19 +364,19 @@ impl <'a> fmt::Display for TableSlice<'a> {
if let Err(_) = self.print(&mut writer) {
return Err(fmt::Error)
}
- return fmt.write_str(writer.as_string());
+ fmt.write_str(writer.as_string())
}
}
impl <B: ToString, A: IntoIterator<Item=B>> FromIterator<A> for Table {
fn from_iter<T>(iterator: T) -> Table where T: IntoIterator<Item=A> {
- return Self::init(iterator.into_iter().map(|r| Row::from(r)).collect());
+ Self::init(iterator.into_iter().map(|r| Row::from(r)).collect())
}
}
impl <T, A, B> From<T> for Table where B: ToString, A: IntoIterator<Item=B>, T : IntoIterator<Item=A> {
fn from(it: T) -> Table {
- return Self::from_iter(it);
+ Self::from_iter(it)
}
}
@@ -384,7 +384,7 @@ impl <'a> IntoIterator for &'a Table {
type Item=&'a Row;
type IntoIter=Iter<'a, Row>;
fn into_iter(self) -> Self::IntoIter {
- return self.as_ref().row_iter();
+ self.as_ref().row_iter()
}
}
@@ -392,7 +392,7 @@ impl <'a> IntoIterator for &'a mut Table {
type Item=&'a mut Row;
type IntoIter=IterMut<'a, Row>;
fn into_iter(self) -> Self::IntoIter {
- return self.row_iter_mut();
+ self.row_iter_mut()
}
}
@@ -402,10 +402,7 @@ pub struct ColumnIter<'a>(std::slice::Iter<'a, Row>, usize);
impl <'a> std::iter::Iterator for ColumnIter<'a> {
type Item = &'a Cell;
fn next(&mut self) -> Option<&'a Cell> {
- return match self.0.next() {
- None => None,
- Some(row) => row.get_cell(self.1)
- }
+ self.0.next().and_then(|row| row.get_cell(self.1))
}
}
@@ -415,27 +412,24 @@ pub struct ColumnIterMut<'a>(std::slice::IterMut<'a, Row>, usize);
impl <'a> std::iter::Iterator for ColumnIterMut<'a> {
type Item = &'a mut Cell;
fn next(&mut self) -> Option<&'a mut Cell> {
- return match self.0.next() {
- None => None,
- Some(row) => row.get_mut_cell(self.1)
- }
+ self.0.next().and_then(|row| row.get_mut_cell(self.1))
}
}
impl <'a> AsRef<TableSlice<'a>> for TableSlice<'a> {
fn as_ref(&self) -> &TableSlice<'a> {
- return self;
+ self
}
}
impl <'a> AsRef<TableSlice<'a>> for Table {
fn as_ref(&self) -> &TableSlice<'a> {
- return unsafe {
+ unsafe {
// All this is a bit hacky. Let's try to find something else
let s = &mut *((self as *const Table) as *mut Table);
s.rows.shrink_to_fit();
- return transmute(self);
- };
+ transmute(self)
+ }
}
}
@@ -451,7 +445,7 @@ impl <'a, T, E> Slice<'a, E> for T where T: AsRef<TableSlice<'a>>, [Row]: Index<
type Output = TableSlice<'a>;
fn slice(&'a self, arg: E) -> Self::Output {
let sl = self.as_ref();
- return TableSlice {
+ TableSlice {
format: sl.format,
titles: sl.titles,
rows: sl.rows.index(arg)
diff --git a/src/row.rs b/src/row.rs
index ed74465..3352628 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -19,19 +19,19 @@ pub struct Row {
impl Row {
/// Create a new `Row` backed with `cells` vector
pub fn new(cells: Vec<Cell>) -> Row {
- return Row {
+ Row {
cells: cells
- };
+ }
}
/// Create an row of length `size`, with empty strings stored
pub fn empty() -> Row {
- return Self::new(vec![Cell::default(); 0]);
+ Self::new(vec![Cell::default(); 0])
}
/// Get the number of cells in this row
pub fn len(&self) -> usize {
- return self.cells.len();
+ self.cells.len()
}
/// Get the height of this row
@@ -43,26 +43,23 @@ impl Row {
height = h;
}
}
- return height;
+ height
}
/// Get the minimum width required by the cell in the column `column`.
/// Return 0 if the cell does not exist in this row
pub fn get_cell_width(&self, column: usize) -> usize {
- return match self.cells.get(column) {
- Some(cell) => cell.get_width(),
- None => 0
- }
+ self.cells.get(column).map(|cell| cell.get_width()).unwrap_or(0)
}
/// Get the cell at index `idx`
pub fn get_cell(&self, idx: usize) -> Option<&Cell> {
- return self.cells.get(idx);
+ self.cells.get(idx)
}
/// Get the mutable cell at index `idx`
pub fn get_mut_cell(&mut self, idx: usize) -> Option<&mut Cell> {
- return self.cells.get_mut(idx);
+ self.cells.get_mut(idx)
}
/// Set the `cell` in the row at the given `column`
@@ -71,7 +68,7 @@ impl Row {
return Err("Cannot find cell");
}
self.cells[column] = cell;
- return Ok(());
+ Ok(())
}
/// Append a `cell` at the end of the row
@@ -128,50 +125,50 @@ impl Row {
try!(format.print_column_separator(out, ColumnPosition::Right));
try!(out.write_all(NEWLINE));
}
- return Ok(());
+ Ok(())
}
/// Print the row to `out`, with `separator` as column separator, and `col_width`
/// specifying the width of each columns
pub fn print<T: Write+?Sized>(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) -> Result<(), Error> {
- return self.__print(out, format, col_width, Cell::print);
+ self.__print(out, format, col_width, Cell::print)
}
/// Print the row to terminal `out`, with `separator` as column separator, and `col_width`
/// specifying the width of each columns. Apply style when needed
pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) -> Result<(), Error> {
- return self.__print(out, format, col_width, Cell::print_term);
+ self.__print(out, format, col_width, Cell::print_term)
}
}
impl Default for Row {
fn default() -> Row {
- return Row::empty();
+ Row::empty()
}
}
impl Index<usize> for Row {
type Output = Cell;
fn index(&self, idx: usize) -> &Self::Output {
- return &self.cells[idx];
+ &self.cells[idx]
}
}
impl IndexMut<usize> for Row {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
- return &mut self.cells[idx];
+ &mut self.cells[idx]
}
}
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());
+ Self::new(iterator.into_iter().map(|ref e| Cell::from(e)).collect())
}
}
impl <T, A> From<T> for Row where A: ToString, T : IntoIterator<Item=A> {
fn from(it: T) -> Row {
- return Self::from_iter(it);
+ Self::from_iter(it)
}
}
@@ -179,7 +176,7 @@ impl <'a> IntoIterator for &'a Row {
type Item=&'a Cell;
type IntoIter=Iter<'a, Cell>;
fn into_iter(self) -> Self::IntoIter {
- return self.iter();
+ self.iter()
}
}
@@ -187,7 +184,7 @@ impl <'a> IntoIterator for &'a mut Row {
type Item=&'a mut Cell;
type IntoIter=IterMut<'a, Cell>;
fn into_iter(self) -> Self::IntoIter {
- return self.iter_mut();
+ self.iter_mut()
}
}