diff --git a/imagrc.toml b/imagrc.toml index ccc1f551..93184460 100644 --- a/imagrc.toml +++ b/imagrc.toml @@ -1,6 +1,49 @@ # This is a example configuration file for the imag suite. # It is written in TOML +# +# imag supports templates when specifying formats. The templates support several +# functionalities, from colorizing to underlining and such things. +# +# Here goes a list of supported formatting helpers: +# +# These functions can be applied for colorizing the output: +# {{black }} +# {{blue }} +# {{cyan }} +# {{green }} +# {{purple }} +# {{red }} +# {{white }} +# {{yellow }} +# +# The following functions are allowed for formatting text: +# {{lpad }} - to "left pad" by spaces +# {{rpad }} - to "right pad" by spaces +# {{abbrev }} - to "abbreviate" the output to chars +# {{underline }} - for underlining +# {{bold }} - for making input bold +# {{blink }} - for making input blinking +# {{strikethrough }} - for making input struck through +# +# Strings can only be printed. Arrays can be indexed with the `lookup` function. +# +# +# The underlying templating engine also supports these: +# +# {{#raw}} ... {{/raw}} escape handlebars expression within the block +# {{#if ...}} ... {{else}} ... {{/if}} if-else block +# {{#unless ...}} ... {{else}} .. {{/unless}} if-not-else block +# {{#each ...}} ... {{/each}} iterates over an array or object. +# Handlebar-rust doesn't support mustach iteration syntax so use this instead. +# {{#with ...}} ... {{/with}} change current context. Similar to {{#each}}, used for replace corresponding mustach syntax. +# {{lookup ... ...}} get value from array by @index or @key +# {{> ...}} include template with name +# {{log ...}} log value with rust logger, default level: INFO. Currently you cannot change the level. +# +# Warning: These are _not_ tested and should be used with care. +# + # The alias section # # In this section one can define aliases for imag subcommands. @@ -19,6 +62,7 @@ store = [ "s", "st" ] level = "debug" destinations = [ "-" ] +# # Valid variables for logging: # * "level" # * "module_path" @@ -27,15 +71,6 @@ destinations = [ "-" ] # * "target" # * "message" # -# Valid functions to be applied: -# * "black" -# * "blue" -# * "cyan" -# * "green" -# * "purple" -# * "red" -# * "white" -# * "yellow" [imag.logging.format] trace = "[imag][{{red level}}][{{module_path}}]: {{message}}" diff --git a/lib/etc/libimaginteraction/Cargo.toml b/lib/etc/libimaginteraction/Cargo.toml index 6e1e9844..0789629b 100644 --- a/lib/etc/libimaginteraction/Cargo.toml +++ b/lib/etc/libimaginteraction/Cargo.toml @@ -23,6 +23,7 @@ regex = "0.2" toml = "0.4" error-chain = "0.11" handlebars = "0.29.0" +serde_json = "1" libimagstore = { version = "0.5.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.5.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/etc/libimaginteraction/src/format.rs b/lib/etc/libimaginteraction/src/format.rs index 4bb6d66d..409c072e 100644 --- a/lib/etc/libimaginteraction/src/format.rs +++ b/lib/etc/libimaginteraction/src/format.rs @@ -18,6 +18,7 @@ // use handlebars::{Handlebars, HelperDef, JsonRender, RenderError, RenderContext, Helper}; +use serde_json::value::Value; use ansi_term::Colour; use ansi_term::Style; @@ -152,6 +153,51 @@ impl HelperDef for StrikethroughHelper { } } +fn param_to_number(idx: usize, h: &Helper) -> Result { + match try!(h.param(idx).ok_or(RenderError::new("Too few arguments"))).value() { + &Value::Number(ref num) => num.as_u64().ok_or_else(|| RenderError::new("Number cannot be parsed")), + _ => Err(RenderError::new("Type error: First argument should be a number")), + } +} + +#[derive(Clone, Copy)] +pub struct LeftPadHelper; + +impl HelperDef for LeftPadHelper { + fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { + let count = param_to_number(0, h)? as usize; + let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))); + let text = format!("{:>width$}", text.value().render(), width = count); + try!(write!(rc.writer(), "{}", text)); + Ok(()) + } +} + +#[derive(Clone, Copy)] +pub struct RightPadHelper; + +impl HelperDef for RightPadHelper { + fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { + let count = param_to_number(0, h)? as usize; + let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))); + let text = format!("{:width$}", text.value().render(), width = count); + try!(write!(rc.writer(), "{}", text)); + Ok(()) + } +} + +#[derive(Clone, Copy)] +pub struct AbbrevHelper; + +impl HelperDef for AbbrevHelper { + fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { + let count = param_to_number(0, h)? as usize; + let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))).value().render(); + try!(write!(rc.writer(), "{}", text.chars().take(count).collect::())); + Ok(()) + } +} + pub fn register_all_color_helpers(handlebars: &mut Handlebars) { handlebars.register_helper("black" , Box::new(ColorizeBlackHelper)); handlebars.register_helper("blue" , Box::new(ColorizeBlueHelper)); @@ -168,5 +214,8 @@ pub fn register_all_format_helpers(handlebars: &mut Handlebars) { handlebars.register_helper("bold" , Box::new(BoldHelper)); handlebars.register_helper("blink" , Box::new(BlinkHelper)); handlebars.register_helper("strikethrough" , Box::new(StrikethroughHelper)); + handlebars.register_helper("lpad" , Box::new(LeftPadHelper)); + handlebars.register_helper("rpad" , Box::new(RightPadHelper)); + handlebars.register_helper("abbrev" , Box::new(AbbrevHelper)); } diff --git a/lib/etc/libimaginteraction/src/lib.rs b/lib/etc/libimaginteraction/src/lib.rs index 700e4ee6..de8ee0c0 100644 --- a/lib/etc/libimaginteraction/src/lib.rs +++ b/lib/etc/libimaginteraction/src/lib.rs @@ -42,6 +42,7 @@ extern crate regex; extern crate clap; extern crate toml; extern crate handlebars; +extern crate serde_json; #[macro_use] extern crate error_chain; extern crate libimagstore;