Merge pull request #1141 from matthiasbeyer/libimaginteraction/more-helpers
libimaginteraction: Add more helpers for handlebars
This commit is contained in:
commit
bdf1848f3f
4 changed files with 95 additions and 9 deletions
53
imagrc.toml
53
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 <input>}}
|
||||
# {{blue <input>}}
|
||||
# {{cyan <input>}}
|
||||
# {{green <input>}}
|
||||
# {{purple <input>}}
|
||||
# {{red <input>}}
|
||||
# {{white <input>}}
|
||||
# {{yellow <input>}}
|
||||
#
|
||||
# The following functions are allowed for formatting text:
|
||||
# {{lpad <count> <input>}} - to "left pad" by <count> spaces
|
||||
# {{rpad <count> <input>}} - to "right pad" by <count> spaces
|
||||
# {{abbrev <count> <input>}} - to "abbreviate" the output to <count> chars
|
||||
# {{underline <input>}} - for underlining
|
||||
# {{bold <input>}} - for making input bold
|
||||
# {{blink <input>}} - for making input blinking
|
||||
# {{strikethrough <input>}} - 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}}"
|
||||
|
|
|
@ -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" }
|
||||
|
|
|
@ -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<u64, RenderError> {
|
||||
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::<String>()));
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue