Skip to content

Commit

Permalink
move newline from markup to raw-text
Browse files Browse the repository at this point in the history
this avoids a current _very small_ bug in the ANSI formatter which adds
the indent after all new lines which means the next time the terminal is
asked to printed a line it has the indent. We can "absorb" this indent
by simply moving one of the newlines out of the markup and into a raw
println!() call
  • Loading branch information
tomeichlersmith committed Nov 3, 2023
1 parent 2d76927 commit d2579d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
6 changes: 5 additions & 1 deletion numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ impl Cli {
PrettyPrintMode::Auto => true,
};
let help = help_markup(pretty_print);
print!("{}", ansi_format(&help, false));
print!("{}", ansi_format(&help, true));
// currently, the ansi formatter adds indents
// _after_ each newline and so we need to manually
// add an extra blank line to absorb this indent
println!();
}
_ => {
let result = self.parse_and_evaluate(
Expand Down
89 changes: 41 additions & 48 deletions numbat/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ use crate::{InterpreterSettings, NameResolutionError, Type};

use std::sync::{Arc, Mutex};

fn evaluate_example(
context: &mut Context,
input: &str,
pretty_print: bool,
) -> m::Markup {
let statement_output : Arc<Mutex<Vec<m::Markup>>> = Arc::new(Mutex::new(vec![]));
fn evaluate_example(context: &mut Context, input: &str, pretty_print: bool) -> m::Markup {
let statement_output: Arc<Mutex<Vec<m::Markup>>> = Arc::new(Mutex::new(vec![]));
let statement_output_c = statement_output.clone();
let mut settings = InterpreterSettings {
print_fn: Box::new(move |s: &m::Markup| {
statement_output_c
.lock()
.unwrap()
.push(s.clone());
statement_output_c.lock().unwrap().push(s.clone());
}),
};

Expand All @@ -40,16 +33,15 @@ fn evaluate_example(
match result {
Ok((statements, interpreter_result)) => {
if pretty_print {
full_output +=
statements
.iter()
.fold(m::empty(), |accumulated_mk, statement| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ statement.pretty_print()
+ m::nl()
});
full_output += statements
.iter()
.fold(m::empty(), |accumulated_mk, statement| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ statement.pretty_print()
+ m::nl()
});
}

match interpreter_result {
Expand All @@ -70,16 +62,16 @@ fn evaluate_example(
}
});

full_output +=
statement_output.lock().unwrap().iter().fold(
m::empty(), |accumulated_mk, single_line| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ single_line.clone()
+ m::nl()
})
+ m::nl()
full_output += statement_output.lock().unwrap().iter().fold(
m::empty(),
|accumulated_mk, single_line| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ single_line.clone()
+ m::nl()
},
) + m::nl()
+ m::whitespace(" ")
+ m::operator("=")
+ m::space()
Expand All @@ -89,14 +81,16 @@ fn evaluate_example(
}
InterpreterResult::Continue => {
full_output += statement_output.lock().unwrap().iter().fold(
m::empty(), |accumulated_mk, single_line| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ single_line.clone()
+ m::nl()
});
},
m::empty(),
|accumulated_mk, single_line| {
accumulated_mk
+ m::nl()
+ m::whitespace(" ")
+ single_line.clone()
+ m::nl()
},
);
}
InterpreterResult::Exit(_exit_status) => {
println!("Interpretation Error.");
}
Expand Down Expand Up @@ -124,12 +118,12 @@ fn evaluate_example(

pub fn help_markup(pretty_print: bool) -> m::Markup {
let mut output = m::nl()
+ m::keyword("numbat")
+ m::space()
+ m::text(env!("CARGO_PKG_DESCRIPTION"))
+ m::nl()
+ m::text("You can start by trying one of the examples:")
+ m::nl();
+ m::keyword("numbat")
+ m::space()
+ m::text(env!("CARGO_PKG_DESCRIPTION"))
+ m::nl()
+ m::text("You can start by trying one of the examples:")
+ m::nl();

let examples = vec![
"8 km / (1 h + 25 min)",
Expand All @@ -145,9 +139,8 @@ pub fn help_markup(pretty_print: bool) -> m::Markup {
output += m::nl();
}
output += m::text("Full documentation:")
+ m::space()
+ m::keyword("https://numbat.dev/doc/")
+ m::nl()
+ m::nl();
+ m::space()
+ m::keyword("https://numbat.dev/doc/")
+ m::nl();
output
}

0 comments on commit d2579d4

Please sign in to comment.