Very start of render color terminal

This commit is contained in:
Chad Stearns 2020-03-16 02:27:38 -04:00
parent f8dd2fb9a1
commit 9cdd7988fc
2 changed files with 60 additions and 23 deletions

View file

@ -21,24 +21,18 @@ pub enum Color {
Red, Red,
} }
pub const DEFAULT_PALETTE: Palette = Palette {
pub const default_palette: Palette = Palette {
primary: Color::White, primary: Color::White,
error: Color::Red, error: Color::Red,
}; };
impl Color { impl Color {
pub fn render(self, str: &str) -> String { pub fn render(self, str: &str) -> String {
use Color::*; use Color::*;
match self { match self {
Red => { Red => red(str),
red(str) White => white(str),
}
White => {
white(str)
}
} }
} }
} }
@ -110,8 +104,6 @@ fn newline() -> ReportText {
} }
fn red(str: &str) -> String { fn red(str: &str) -> String {
use ReportText::*;
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\u{001b}[31m"); buf.push_str("\u{001b}[31m");
@ -122,19 +114,16 @@ fn red(str: &str) -> String {
} }
fn white(str: &str) -> String { fn white(str: &str) -> String {
use ReportText::*;
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\u{001b}[31m"); buf.push_str("\u{001b}[31m");
buf.push_str(str); buf.push_str(str);
buf.push_str(reset); buf.push_str(RESET);
buf buf
} }
const RESET: &str = "\u{001b}[0m";
const reset: &str = "\u{001b}[0m";
impl ReportText { impl ReportText {
/// Render to CI console output, where no colors are available. /// Render to CI console output, where no colors are available.
@ -203,13 +192,21 @@ impl ReportText {
/// Render to a color terminal using ANSI escape sequences /// Render to a color terminal using ANSI escape sequences
pub fn render_color_terminal( pub fn render_color_terminal(
&self, &self,
_buf: &mut String, buf: &mut String,
_subs: &mut Subs, _subs: &mut Subs,
_home: ModuleId, _home: ModuleId,
_src_lines: &[&str], _src_lines: &[&str],
_interns: &Interns, _interns: &Interns,
palette: Palette,
) { ) {
// TODO do the same stuff as render_ci, but with colors via ANSI terminal escape codes! use ReportText::*;
// Examples of how to do this are in the source code of https://github.com/rtfeldman/console-print
match self {
Plain(string) => {
buf.push_str(&palette.primary.render(string));
}
_ => panic!("TODO implement more ReportTexts in render color terminal"),
}
} }
} }

View file

@ -11,7 +11,7 @@ mod helpers;
mod test_report { mod test_report {
use crate::helpers::test_home; use crate::helpers::test_home;
use roc_module::symbol::{Interns, ModuleId}; use roc_module::symbol::{Interns, ModuleId};
use roc_reporting::report::{can_problem, plain_text, Report, ReportText}; use roc_reporting::report::{can_problem, DEFAULT_PALETTE, plain_text, Report, ReportText};
use roc_types::pretty_print::name_all_type_vars; use roc_types::pretty_print::name_all_type_vars;
use roc_types::subs::Subs; use roc_types::subs::Subs;
use roc_types::types; use roc_types::types;
@ -82,6 +82,38 @@ mod test_report {
assert_eq!(buf, expected_rendering); assert_eq!(buf, expected_rendering);
} }
fn report_renders_in_color_from_src(src: &str, report: Report, expected_rendering: &str) {
let (_type_problems, _can_problems, mut subs, home, interns) = infer_expr_help(src);
let mut buf: String = String::new();
let src_lines: Vec<&str> = src.split('\n').collect();
report.text.render_color_terminal(
&mut buf,
&mut subs,
home,
&src_lines,
&interns,
DEFAULT_PALETTE,
);
assert_eq!(buf, expected_rendering);
}
fn report_renders_in_color(report: Report, expected_rendering: &str) {
report_renders_in_color_from_src(
indoc!(
r#"
x = 1
y = 2
x
"#
),
report,
expected_rendering,
)
}
fn report_renders_as(report: Report, expected_rendering: &str) { fn report_renders_as(report: Report, expected_rendering: &str) {
report_renders_as_from_src( report_renders_as_from_src(
indoc!( indoc!(
@ -202,15 +234,23 @@ mod test_report {
buf, buf,
indoc!( indoc!(
r#" r#"
y is not used anywhere in your code. y is not used anywhere in your code.
1 | y = 2 1 y = 2
If you didn't intend on using y then remove it so future readers of your code don't wonder why it is there."# If you didn't intend on using y then remove it so future readers of your code don't wonder why it is there."#
) )
); );
} }
#[test]
fn report_in_color() {
report_renders_in_color(
to_simple_report(plain_text("y")),
"\u{001b}[31my\u{001b}[0m",
);
}
#[test] #[test]
fn report_region() { fn report_region() {
report_renders_as_from_src( report_renders_as_from_src(