changeset 36:b0f65be9464d

Add plain-text renderer.
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 18 Feb 2016 16:22:00 +0100
parents cbfe39a75d77
children c65c28c13591
files src/framework.rs src/helper.rs src/main.rs src/render.rs
diffstat 4 files changed, 54 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/framework.rs	Thu Feb 18 16:21:29 2016 +0100
+++ b/src/framework.rs	Thu Feb 18 16:22:00 2016 +0100
@@ -58,6 +58,9 @@
                              text = self.text);
         result
     }
+    pub fn get(&self) -> (String, Color) {
+        (self.text.clone(), self.color.clone())
+    }
 }
 
 /// State that is passed to and returned from every render cycle.
--- a/src/helper.rs	Thu Feb 18 16:21:29 2016 +0100
+++ b/src/helper.rs	Thu Feb 18 16:22:00 2016 +0100
@@ -37,6 +37,7 @@
     }
 }
 
+/// Splits the result of read_procfs_file() into lines.
 pub fn get_procfs_file_lines(path: String) -> Option<Vec<String>> {
     match read_procfs_file(path) {
         None => None,
@@ -44,10 +45,13 @@
     }
 }
 
+/// Splits a string at commas (',') and returns the list of the elements separated.
 pub fn commaseparated_to_vec(s: String) -> Vec<String> {
     s.split(",").map(String::from).collect()
 }
 
+/// Return a list of all matches of a regex on a string in the wanted type.
+/// This is difficult to explain -- look at src/metrics/load.rs for a simple use case.
 pub fn extract_from_str<T: FromStr + Clone, C: FromIterator<T>>(s: &String,
                                                                 re: &Regex,
                                                                 default: T)
--- a/src/main.rs	Thu Feb 18 16:21:29 2016 +0100
+++ b/src/main.rs	Thu Feb 18 16:22:00 2016 +0100
@@ -37,7 +37,7 @@
                        "SECONDS");
         options.optopt("",
                        "renderer",
-                       "Which renderer to use. Currently available: i3status",
+                       "Which renderer to use. Currently available: i3status,plain",
                        "i3status");
         options.optflag("h", "help", "Print a help text");
 
@@ -174,6 +174,7 @@
     use render;
 
     registry.register_renderer("i3status", render::make_i3status());
+    registry.register_renderer("plain", render::make_plaintextrenderer());
 }
 
 fn main() {
--- a/src/render.rs	Thu Feb 18 16:21:29 2016 +0100
+++ b/src/render.rs	Thu Feb 18 16:22:00 2016 +0100
@@ -40,6 +40,51 @@
     Box::new(I3statRenderer::new())
 }
 
+struct PlainTextRenderer {
+    metrics: Vec<ActiveMetric>,
+}
+
+impl PlainTextRenderer {
+    fn new() -> PlainTextRenderer {
+        PlainTextRenderer { metrics: Vec::new() }
+    }
+    fn color_to_ansi(c: Color) -> String {
+        String::from(match c {
+            Color::Arbitrary(_) => "\x1B[0m",
+            Color::Default => "\x1B[0m",
+            Color::White => "\x1B[37m",
+            Color::Red => "\x1b[31m",
+            Color::Green => "\x1b[32m",
+            Color::Blue => "\x1b[34m",
+            Color::Black => "\x1b[30m",
+            Color::Orange => "\x1b[31;1m",
+            Color::Purple => "\x1b[35m",
+        })
+    }
+}
+
+impl Renderer for PlainTextRenderer {
+    fn init(&mut self, metrics: Vec<ActiveMetric>) -> String {
+        self.metrics = metrics;
+        String::new()
+    }
+    fn render(&mut self) -> String {
+
+        self.metrics.iter_mut().map(|m| m.render()).fold(String::from(""), |mut out, rendres| {
+            let (txt, col) = rendres.get();
+            out.push_str(&PlainTextRenderer::color_to_ansi(col));
+            out.push_str(&txt);
+            out.push_str(&PlainTextRenderer::color_to_ansi(Color::Default));
+            out.push_str(" | ");
+            out
+        })
+    }
+}
+
+pub fn make_plaintextrenderer() -> Box<Renderer> {
+    Box::new(PlainTextRenderer::new())
+}
+
 pub fn render_loop(mut r: Box<Renderer>, metrics: Vec<ActiveMetric>, interval: i32) {
     use std::thread::sleep;
     use std::time::Duration;