view src/metrics/time.rs.bk @ 13:441318144593

Add clock metric
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 14 Feb 2016 14:49:16 +0100
parents
children
line wrap: on
line source

//! 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)
}