summaryrefslogtreecommitdiffstats
path: root/src/cell.rs
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-08 11:03:51 +0100
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-08 11:03:51 +0100
commit8d56043062880ca1b70e2d600deb6cbda393a3c2 (patch)
treeb6037525efc57b6ec694631a0841e4e1d6a81803 /src/cell.rs
parentc551d1f70c4994ae75a0750d3bebb55eb56bb7e2 (diff)
#2 : Styles can be set in macros for simplicity
Diffstat (limited to 'src/cell.rs')
-rw-r--r--src/cell.rs49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 04f8722..c41e800 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -3,7 +3,7 @@
use std::io::{Write, Error};
use std::string::ToString;
use unicode_width::UnicodeWidthStr;
-use term::{Attr, Terminal};
+use term::{Attr, Terminal, color};
use super::format::Align;
/// Represent a table cell containing a string.
@@ -60,6 +60,53 @@ impl Cell {
return self;
}
+ pub fn style_spec(mut self, spec: &str) -> Cell {
+ let mut foreground = false;
+ let mut background = false;
+ for c in spec.chars() {
+ if foreground || background {
+ let color = match c {
+ 'r' => color::RED,
+ 'R' => color::BRIGHT_RED,
+ 'b' => color::BLUE,
+ 'B' => color::BRIGHT_BLUE,
+ 'g' => color::GREEN,
+ 'G' => color::BRIGHT_GREEN,
+ 'y' => color::YELLOW,
+ 'Y' => color::BRIGHT_YELLOW,
+ 'c' => color::CYAN,
+ 'C' => color::BRIGHT_CYAN,
+ 'm' => color::MAGENTA,
+ 'M' => color::BRIGHT_MAGENTA,
+ 'w' => color::WHITE,
+ 'W' => color::BRIGHT_WHITE,
+ 'd' => color::BLACK,
+ 'D' => color::BRIGHT_BLACK,
+ _ => panic!("Unsupported color specifier {}", c)
+ };
+ if foreground { self.style(Attr::ForegroundColor(color)); }
+ else if background { self.style(Attr::BackgroundColor(color)); }
+ foreground = false;
+ background = false;
+ }
+ else {
+ match c {
+ 'F' => foreground = true,
+ 'B' => background = true,
+ 'b' => self.style(Attr::Bold),
+ 'i' => self.style(Attr::Italic(true)),
+ 'u' => self.style(Attr::Underline(true)),
+ 'c' => self.align(Align::CENTER),
+ 'l' => self.align(Align::LEFT),
+ 'r' => self.align(Align::RIGHT),
+ 'd' => {/*Default : do nothing*/}
+ _ => panic!("Unsupported style specifier {}", c)
+ }
+ }
+ }
+ return self;
+ }
+
/// Return the height of the cell
pub fn get_height(&self) -> usize {
return self.content.len();