remove some more duplicated code (format_output)

This commit is contained in:
Brian Carroll 2023-09-09 14:45:14 +01:00
parent 4367e357e6
commit fc7b831285
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0

View file

@ -6,11 +6,10 @@ use std::borrow::Cow;
use bumpalo::Bump;
use roc_load::MonomorphizedModule;
use roc_mono::ir::OptLevel;
use roc_repl_eval::gen::{Problems, ReplOutput};
use roc_repl_ui::colors::{END_COL, GREEN, PINK};
use roc_repl_eval::gen::Problems;
use roc_repl_ui::repl_state::{ReplAction, ReplState};
use roc_repl_ui::{is_incomplete, CONT_PROMPT, PROMPT, SHORT_INSTRUCTIONS, TIPS, WELCOME_MESSAGE};
use roc_reporting::report::DEFAULT_PALETTE;
use roc_repl_ui::{is_incomplete, format_output, CONT_PROMPT, PROMPT, SHORT_INSTRUCTIONS, TIPS, WELCOME_MESSAGE};
use roc_reporting::report::{DEFAULT_PALETTE, ANSI_STYLE_CODES};
use roc_target::TargetInfo;
use rustyline::highlight::{Highlighter, PromptInfo};
use rustyline::validate::{self, ValidationContext, ValidationResult, Validator};
@ -104,7 +103,7 @@ pub fn evaluate<'a>(
dimensions: Option<(usize, usize)>,
) -> String {
let opt_output = opt_mono.and_then(|mono| eval_llvm(mono, target, OptLevel::Normal));
format_output(opt_output, problems, opt_var_name, dimensions)
format_output(ANSI_STYLE_CODES, opt_output, problems, opt_var_name, dimensions)
}
#[derive(Default)]
@ -150,86 +149,3 @@ impl Validator for ReplHelper {
self.validator.validate_while_typing()
}
}
fn format_output(
opt_output: Option<ReplOutput>,
problems: Problems,
opt_var_name: Option<String>,
dimensions: Option<(usize, usize)>,
) -> String {
let mut buf = String::new();
for message in problems.errors.iter().chain(problems.warnings.iter()) {
if !buf.is_empty() {
buf.push_str("\n\n");
}
buf.push('\n');
buf.push_str(message);
buf.push('\n');
}
if let Some(ReplOutput { expr, expr_type }) = opt_output {
// If expr was empty, it was a type annotation or ability declaration;
// don't print anything!
//
// Also, for now we also don't print anything if there was a compile-time error.
// In the future, it would be great to run anyway and print useful output here!
if !expr.is_empty() && problems.errors.is_empty() {
const EXPR_TYPE_SEPARATOR: &str = " : "; // e.g. in "5 : Num *"
// Print the expr and its type
{
buf.push('\n');
buf.push_str(&expr);
buf.push_str(PINK); // Color for the type separator
buf.push_str(EXPR_TYPE_SEPARATOR);
buf.push_str(END_COL);
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_PREFIX: &str = " # "; // e.g. in " # val1"
const VAR_NAME_COLUMN_MAX: usize = 32; // Right-align the var_name at this column
let term_width = match dimensions {
Some((width, _)) => width.min(VAR_NAME_COLUMN_MAX),
None => VAR_NAME_COLUMN_MAX,
};
let expr_with_type = format!("{expr}{EXPR_TYPE_SEPARATOR}{expr_type}");
// Count graphemes because we care about what's *rendered* in the terminal
let last_line_len = expr_with_type
.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 {
buf.push(' ');
}
buf.push_str(GREEN);
buf.push_str(VAR_NAME_PREFIX);
buf.push_str(&var_name);
buf.push_str(END_COL);
buf.push('\n');
}
}
}
buf
}