changeset 7:da25ed4d610c

Build render loop
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 14 Feb 2016 12:40:41 +0100
parents ec5eb5bac52c
children df0b56b80653
files src/framework.rs
diffstat 1 files changed, 50 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/framework.rs	Sun Feb 14 12:40:28 2016 +0100
+++ b/src/framework.rs	Sun Feb 14 12:40:41 2016 +0100
@@ -1,6 +1,8 @@
 #![allow(dead_code)]
 
 use std::collections::BTreeMap;
+use std::thread::sleep;
+use std::time::Duration;
 
 extern crate chrono;
 use self::chrono::Local;
@@ -16,16 +18,45 @@
     Red,
     Green,
     Blue,
+    Black,
     Orange,
     Purple,
 }
 
+impl Color {
+    fn to_string(&self) -> String {
+        match self.clone() {
+            Color::Arbitrary(c) => c,
+            Color::Standard => String::from("#ffffff"),
+            Color::White => String::from("#ffffff"),
+            Color::Red => String::from("#ff0000"),
+            Color::Green => String::from("#00ff00"),
+            Color::Blue => String::from("#0000ff"),
+            Color::Black => String::from("#000000"),
+            Color::Orange => String::from("#e8a317"),
+            Color::Purple => String::from("#8d0552"),
+        }
+    }
+}
+
 /// An output produced by a metric to be displayed in the bar.
 pub struct RenderResult {
+    name: String,
     pub text: String,
     pub color: Color,
 }
 
+impl RenderResult {
+    fn to_json(&self) -> String {
+        let result = format!(
+            "{{\"name\": \"{name}\",\"color\":\"{color}\",\"markup\":\"none\",\"full_text\":\"{text}\"}}",
+            name=self.name,
+            color=self.color.to_string(),
+            text=self.text);
+        result
+    }
+}
+
 /// State that is stored in a MetricState between rendering cycles.
 #[derive(Clone)]
 pub enum State {
@@ -88,4 +119,23 @@
     pub fn name(&self) -> &String {
         &self.name
     }
+    pub fn render(&mut self) -> RenderResult {
+        let mut result = self.m.render(&mut self.st);
+        result.name = self.name.clone();
+        result
+    }
 }
+
+pub fn render_loop(mut metrics: Vec<ActiveMetric>, interval: i32) {
+    let ival_duration = Duration::new(interval as u64, 0);
+    let intro = "{\"version\":1}\n[\n";
+    println!("{}", intro);
+
+    loop {
+        let render_result = metrics.iter_mut().map(|m| m.render()).fold(String::from(""), |mut out, p| { out.push_str(&p.to_json()); out.push_str(","); out });
+
+        println!("[{}],", render_result);
+
+        sleep(ival_duration);
+    }
+}