changeset 13:441318144593

Add clock metric
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 14 Feb 2016 14:49:16 +0100
parents 79ca03c87498
children 61cdb00a3a84
files src/main.rs src/metrics/mod.rs src/metrics/mod.rs.bk src/metrics/time.rs src/metrics/time.rs.bk
diffstat 5 files changed, 83 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs	Sun Feb 14 14:48:57 2016 +0100
+++ b/src/main.rs	Sun Feb 14 14:49:16 2016 +0100
@@ -1,5 +1,6 @@
 mod framework;
 mod helper;
+mod metrics;
 
 use std::collections::BTreeMap;
 use std::process;
@@ -43,7 +44,7 @@
     /// desc and example are for the purpose of documenting the command line option that is added.
     fn register_metric(&mut self, name: &str, desc: &str, example: &str, metric: Box<Metric>) {
         if !self.metrics.contains_key(&String::from(name)) {
-            self.opts.optopt("", name, desc, example);
+            self.opts.optflagopt("", name, desc, example);
             self.metrics.insert(String::from(name), metric);
         }
     }
@@ -92,7 +93,8 @@
         // Look for every defined metric if the user wants to have it displayed.
         for (metric_name, metric) in self.metrics.into_iter() {
             if matches.opt_present(&metric_name) {
-                let st = metric.init(matches.opt_str(&metric_name));
+                let mut st = MetricState::new();
+                metric.init(&mut st, matches.opt_str(&metric_name));
                 metrics.push(ActiveMetric::new(String::from(metric_name), metric, st));
             }
         }
@@ -117,7 +119,15 @@
     }
 }
 
-fn register_metrics(registry: &mut AvailableMetrics) {}
+fn register_metrics(registry: &mut AvailableMetrics) {
+    use metrics::time;
+
+    // List of codes: https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html
+    registry.register_metric("clock",
+                             "A timestamp clock. Uses format codes like date(1)",
+                             "%H:%M",
+                             time::clock_metric());
+}
 
 fn main() {
     let args: Vec<String> = env::args().collect();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metrics/mod.rs	Sun Feb 14 14:49:16 2016 +0100
@@ -0,0 +1,1 @@
+pub mod time;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metrics/mod.rs.bk	Sun Feb 14 14:49:16 2016 +0100
@@ -0,0 +1,1 @@
+pub mod time;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metrics/time.rs	Sun Feb 14 14:49:16 2016 +0100
@@ -0,0 +1,34 @@
+//! A simple clock.
+
+use framework::*;
+
+extern crate chrono;
+use self::chrono::Local;
+
+struct TimeMetric;
+
+const DEFAULT_FMT: &'static str = "%a %b %d %H:%M:%S %Y (%Z)";
+
+impl Metric for TimeMetric {
+    fn init(&self, st: &mut MetricState, arg: Option<String>) {
+        st.set(String::from("format"),
+               State::S(arg.unwrap_or(String::from(DEFAULT_FMT))));
+    }
+    fn render(&self, st: &mut MetricState) -> RenderResult {
+        let fmt;
+
+        match st.get(String::from("format")) {
+            State::S(f) => fmt = f,
+            _ => fmt = String::from(DEFAULT_FMT),
+        }
+
+        let t = Local::now();
+        let tstr = format!("{}", t.format(&fmt));
+
+        RenderResult::new(tstr, Color::Default)
+    }
+}
+
+pub fn clock_metric() -> Box<Metric> {
+    Box::new(TimeMetric)
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metrics/time.rs.bk	Sun Feb 14 14:49:16 2016 +0100
@@ -0,0 +1,34 @@
+//! A simple clock.
+
+use framework::*;
+
+extern crate chrono;
+use self::chrono::{Local};
+
+struct TimeMetric;
+
+const DEFAULT_FMT: &'static str = "%a %b %d %H:%M:%S %Y (%Z)";
+
+impl Metric for TimeMetric {
+    fn init(&self, st: &mut MetricState, arg: Option<String>) {
+        st.set(String::from("format"), State::S(arg.unwrap_or(String::from(DEFAULT_FMT))));
+    }
+    fn render(&self, st: &mut MetricState) -> RenderResult {
+        let fmt;
+
+        match st.get(String::from("format")) {
+            State::S(f) => fmt = f,
+            _ => fmt = String::from(DEFAULT_FMT),
+        }
+
+        let t = Local::now();
+        let tstr = format!("{}", t.format(&fmt));
+
+        RenderResult::new(tstr, Color::Default)
+    }
+}
+
+pub fn clock_metric() -> Box<Metric> {
+    Box::new(TimeMetric)
+}
+