summaryrefslogtreecommitdiffstats
path: root/src/format.rs
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-17 21:16:10 +0200
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-17 23:10:02 +0200
commit1ef61e0be3384f9ac630df985179fad13dd1170e (patch)
tree7e30ba8e658566f3208c9bde91d9f3ce07fd6c0b /src/format.rs
parent27979dd617bfb9af83dc3801eb4372f3a38812a3 (diff)
Create a table format struct, and provide some default format, add
capability to add optional title line
Diffstat (limited to 'src/format.rs')
-rw-r--r--src/format.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/format.rs b/src/format.rs
new file mode 100644
index 0000000..c9ac29a
--- /dev/null
+++ b/src/format.rs
@@ -0,0 +1,99 @@
+//! Define table formatting utilities
+
+use std::io::{Write, Error};
+
+use super::utils::LINEFEED;
+
+/// Contains the character used for printing a line separator
+#[derive(Clone, Debug)]
+pub struct LineSeparator {
+ line: [u8; 1],
+ cross: [u8; 1],
+}
+
+impl LineSeparator {
+
+ /// Create a new line separator instance where `line` is the character used to separate 2 lines
+ /// and `cross` is the one used when column separaors and line separators cross
+ pub fn new(line: char, cross: char) -> LineSeparator {
+ return LineSeparator{line: [line as u8], cross: [cross as u8]};
+ }
+
+ /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column
+ pub fn print<T: Write>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ try!(out.write_all(&self.cross));
+ for width in col_width {
+ for _ in 0..(width + 2) {
+ try!(out.write_all(&self.line));
+ }
+ try!(out.write_all(&self.cross));
+ }
+ return out.write_all(LINEFEED);
+ }
+}
+
+/// Contains the table formatting rules
+#[derive(Clone, Debug)]
+pub struct TableFormat {
+ col_sep: [u8; 1],
+ line_sep: Option<LineSeparator>,
+ title_sep: Option<LineSeparator>
+}
+
+impl TableFormat {
+
+ /// Create a new TableFormat.
+ ///
+ /// `col_sep` is the character used for separating columns.
+ /// `line_sep` is an optional `LineSeparator` defining how to separate lines.
+ /// `title_sep` is an optional `LineSeparator` defining the format of the separator after the title line (if set).
+ /// If `title_sep` is set to `None`, then `line_sep` will be used, f it's set.
+ pub fn new(col_sep: char, line_sep: Option<LineSeparator>, title_sep: Option<LineSeparator>) -> TableFormat {
+ return TableFormat{col_sep: [col_sep as u8], line_sep: line_sep, title_sep: title_sep};
+ }
+
+ /// 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>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ if let Some(ref l) = self.line_sep {
+ return l.print(out, col_width);
+ }
+ return Ok(());
+ }
+
+ /// Print a full title separator to `out`. `col_width` is a slice containing the width of each column
+ pub fn print_title_separator<T: Write>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ if let Some(ref l) = self.title_sep {
+ return l.print(out, col_width);
+ }
+ return self.print_line_separator(out, col_width);
+ }
+
+ /// Print a column separator to `out`
+ pub fn print_column_separator<T: Write>(&self, out: &mut T) -> Result<(), Error> {
+ return out.write_all(&self.col_sep);
+ }
+}
+
+/// A line separator mad of `-` and `+`
+pub const MINUS_PLUS_SEP: LineSeparator = LineSeparator{line: ['-' as u8], cross: ['+' as u8]};
+/// A line separator mad of `=` and `+`
+pub const EQU_PLUS_SEP: LineSeparator = LineSeparator{line: ['=' as u8], cross: ['+' as u8]};
+
+/// Default table format, printing a table like this :
+///
+/// ```text
+/// +----+----+
+/// | T1 | T2 |
+/// +====+====+
+/// | | |
+/// +----+----+
+/// | | |
+/// +----+----+
+/// ```
+pub const FORMAT_DEFAULT: TableFormat = TableFormat{col_sep: ['|' as u8], line_sep: Some(MINUS_PLUS_SEP), title_sep: Some(EQU_PLUS_SEP)};
+
+/// Similar to `FORMAT_DEFAULT` but without special separator after title line
+pub const FORMAT_NO_LINESEP: TableFormat = TableFormat{col_sep: ['|' as u8], line_sep: None, title_sep: Some(MINUS_PLUS_SEP)};
+
+/// Format for printing a table without any separators (only alignment)
+pub const FORMAT_BLANK: TableFormat = TableFormat{col_sep: [' ' as u8], line_sep: None, title_sep: None}; \ No newline at end of file