diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-02-18 14:17:20 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-02-18 15:06:13 +0100 |
| commit | ed81049ddc8fcef3ddf6f6f69d95bb52512bf698 (patch) | |
| tree | 5ee2e955b4fa50bc536d215577a0321c9b4c6617 /src | |
| parent | cc964e32c993e9446896f8a75731783108866ce8 (diff) | |
Show repr in monospace 📏
Diffstat (limited to 'src')
| -rw-r--r-- | src/exec/context.rs | 7 | ||||
| -rw-r--r-- | src/exec/mod.rs | 16 | ||||
| -rw-r--r-- | src/library/extend.rs | 22 | ||||
| -rw-r--r-- | src/library/mod.rs | 1 |
4 files changed, 38 insertions, 8 deletions
diff --git a/src/exec/context.rs b/src/exec/context.rs index 821a2616..5ff55c00 100644 --- a/src/exec/context.rs +++ b/src/exec/context.rs @@ -259,6 +259,13 @@ impl<'a> ExecContext<'a> { } } + /// Set the font to monospace. + pub fn apply_monospace(&mut self) { + let families = self.state.font.families_mut(); + families.list.insert(0, "monospace".to_string()); + families.flatten(); + } + /// Apply a forced line break. pub fn apply_linebreak(&mut self) { self.end_par_group(); diff --git a/src/exec/mod.rs b/src/exec/mod.rs index cafc55cd..3dbe8270 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -97,9 +97,7 @@ impl ExecWithMap for NodeHeading { impl Exec for NodeRaw { fn exec(&self, ctx: &mut ExecContext) { let prev = Rc::clone(&ctx.state.font.families); - let families = ctx.state.font.families_mut(); - families.list.insert(0, "monospace".to_string()); - families.flatten(); + ctx.apply_monospace(); let em = ctx.state.font.font_size(); let line_spacing = ctx.state.par.line_spacing.resolve(em); @@ -136,9 +134,19 @@ impl Exec for Value { fn exec(&self, ctx: &mut ExecContext) { match self { Value::None => {} + Value::Int(v) => ctx.push_text(pretty(v)), + Value::Float(v) => ctx.push_text(pretty(v)), Value::Str(s) => ctx.push_text(s), Value::Template(template) => template.exec(ctx), - other => ctx.push_text(pretty(other)), + Value::Error => {} + other => { + // For values which can't be shown "naturally", we print + // the representation in monospace. + let prev = Rc::clone(&ctx.state.font.families); + ctx.apply_monospace(); + ctx.push_text(pretty(other)); + ctx.state.font.families = prev; + } } } } diff --git a/src/library/extend.rs b/src/library/extend.rs index 6396274e..cf69dbd0 100644 --- a/src/library/extend.rs +++ b/src/library/extend.rs @@ -1,4 +1,5 @@ use crate::prelude::*; +use crate::pretty::pretty; /// `type`: Find out the name of a value's type. /// @@ -8,9 +9,22 @@ use crate::prelude::*; /// # Return value /// The name of the value's type as a string. pub fn type_(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { - if let Some(value) = args.require::<Value>(ctx, "value") { - value.type_name().into() - } else { - Value::Error + match args.require::<Value>(ctx, "value") { + Some(value) => value.type_name().into(), + None => Value::Error, + } +} + +/// `repr`: Get the string representation of a value. +/// +/// # Positional arguments +/// - Any value. +/// +/// # Return value +/// The string representation of the value. +pub fn repr(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { + match args.require::<Value>(ctx, "value") { + Some(value) => pretty(&value).into(), + None => Value::Error, } } diff --git a/src/library/mod.rs b/src/library/mod.rs index 48da093b..59198846 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -38,6 +38,7 @@ pub fn new() -> Scope { set!(func: "image", image); set!(func: "page", page); set!(func: "pagebreak", pagebreak); + set!(func: "repr", repr); set!(func: "rgb", rgb); set!(func: "type", type_); set!(func: "v", v); |
