Try a different way of writing val1

This commit is contained in:
Richard Feldman 2023-09-17 00:45:14 -04:00
parent ab2ec925a3
commit ca33e4f64c
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
2 changed files with 15 additions and 25 deletions

View file

@ -159,41 +159,30 @@ pub fn format_output(
if let Some(var_name) = opt_var_name { if let Some(var_name) = opt_var_name {
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
const VAR_NAME_PREFIX: &str = " # "; // e.g. in " # val1" const VAR_NAME_COLUMN_MIN: usize = 16; // Always draw the line under the answer at least this wide
const VAR_NAME_COLUMN_MAX: usize = 32; // Right-align the var_name at this column
let term_width = match dimensions { let term_width = match dimensions {
Some((width, _)) => width.min(VAR_NAME_COLUMN_MAX), Some((width, _)) => width.max(VAR_NAME_COLUMN_MIN),
None => VAR_NAME_COLUMN_MAX, None => VAR_NAME_COLUMN_MIN,
}; };
let expr_with_type = format!("{expr}{EXPR_TYPE_SEPARATOR}{expr_type}");
// Count graphemes because we care about what's *rendered* in the terminal // Count graphemes because we care about what's *rendered* in the terminal
let last_line_len = expr_with_type let var_name_len = var_name.graphemes(true).count();
.split('\n')
.last()
.unwrap_or_default()
.graphemes(true)
.count();
let var_name_len =
var_name.graphemes(true).count() + VAR_NAME_PREFIX.graphemes(true).count();
let spaces_needed = if last_line_len + var_name_len > term_width {
buf.push('\n');
term_width - var_name_len
} else {
term_width - last_line_len - var_name_len
};
for _ in 0..spaces_needed { // Subtract 2 to make room for a space on either side of var_name
buf.push(' '); 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_str(style_codes.green); buf.push(' ');
buf.push_str(VAR_NAME_PREFIX);
buf.push_str(&var_name); buf.push_str(&var_name);
buf.push(' ');
buf.push_str(style_codes.reset); buf.push_str(style_codes.reset);
buf.push('\n');
} }
} }
} }

View file

@ -16,6 +16,7 @@ use roc_target::TargetInfo;
/// The prefix we use for the automatic variable names we assign to each expr, /// The prefix we use for the automatic variable names we assign to each expr,
/// e.g. if the prefix is "val" then the first expr you enter will be named "val1" /// e.g. if the prefix is "val" then the first expr you enter will be named "val1"
pub const AUTO_VAR_PREFIX: &str = "val"; pub const AUTO_VAR_PREFIX: &str = "val";
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct PastDef { struct PastDef {
ident: String, ident: String,