Try printing #val1 at end of previous line

This commit is contained in:
Richard Feldman 2023-09-17 00:56:59 -04:00
parent ca33e4f64c
commit f4937e72cc
No known key found for this signature in database
GPG Key ID: F1F21AA5B1D9E43B
2 changed files with 15 additions and 33 deletions

View File

@ -45,7 +45,9 @@ pub fn main() -> i32 {
loop {
match editor.readline(PROMPT) {
Ok(line) => {
editor.add_history_entry(line.trim());
let line = line.trim();
editor.add_history_entry(line);
let dimensions = editor.dimensions();
let repl_state = &mut editor
@ -65,7 +67,9 @@ pub fn main() -> i32 {
// If there was no output, don't print a blank line!
// (This happens for something like a type annotation.)
if !output.is_empty() {
println!("{output}");
// Overwrite the previous line so we can do things like
// print " #val1" after what the user just entered.
println!("\x1B[A{PROMPT}{line}{output}");
}
}
ReplAction::Exit => {

View File

@ -145,45 +145,23 @@ pub fn format_output(
if !expr.is_empty() && problems.errors.is_empty() {
const EXPR_TYPE_SEPARATOR: &str = " : "; // e.g. in "5 : Num *"
// Print var_name on the line before the output
if let Some(var_name) = opt_var_name {
buf.push_str(style_codes.green);
buf.push_str(" # ");
buf.push_str(&var_name);
buf.push_str(style_codes.reset);
}
// Print the expr and its type
{
buf.push('\n');
buf.push_str("\n\n");
buf.push_str(&expr);
buf.push_str(style_codes.magenta); // Color for the type separator
buf.push_str(EXPR_TYPE_SEPARATOR);
buf.push_str(style_codes.reset);
buf.push_str(&expr_type);
}
// Print var_name right-aligned on the last line of output.
if let Some(var_name) = opt_var_name {
use unicode_segmentation::UnicodeSegmentation;
const VAR_NAME_COLUMN_MIN: usize = 16; // Always draw the line under the answer at least this wide
let term_width = match dimensions {
Some((width, _)) => width.max(VAR_NAME_COLUMN_MIN),
None => VAR_NAME_COLUMN_MIN,
};
// Count graphemes because we care about what's *rendered* in the terminal
let var_name_len = var_name.graphemes(true).count();
// Subtract 2 to make room for a space on either side of var_name
let line_width = term_width.saturating_sub(var_name_len).saturating_sub(2);
buf.push('\n');
buf.push_str(style_codes.white);
for _ in 0..line_width {
buf.push('─');
}
buf.push(' ');
buf.push_str(&var_name);
buf.push(' ');
buf.push_str(style_codes.reset);
}
}
}