From 9cdd7988fc80f8eb0765ede227f5f11aa7412229 Mon Sep 17 00:00:00 2001 From: Chad Stearns Date: Mon, 16 Mar 2020 02:27:38 -0400 Subject: [PATCH] Very start of render color terminal --- compiler/reporting/src/report.rs | 35 ++++++++-------- compiler/reporting/tests/test_reporting.rs | 48 ++++++++++++++++++++-- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/compiler/reporting/src/report.rs b/compiler/reporting/src/report.rs index b9ce7b7667..484a5a6b3e 100644 --- a/compiler/reporting/src/report.rs +++ b/compiler/reporting/src/report.rs @@ -21,24 +21,18 @@ pub enum Color { Red, } - -pub const default_palette: Palette = Palette { +pub const DEFAULT_PALETTE: Palette = Palette { primary: Color::White, error: Color::Red, }; - impl Color { pub fn render(self, str: &str) -> String { use Color::*; match self { - Red => { - red(str) - } - White => { - white(str) - } + Red => red(str), + White => white(str), } } } @@ -110,8 +104,6 @@ fn newline() -> ReportText { } fn red(str: &str) -> String { - use ReportText::*; - let mut buf = String::new(); buf.push_str("\u{001b}[31m"); @@ -122,19 +114,16 @@ fn red(str: &str) -> String { } fn white(str: &str) -> String { - use ReportText::*; - let mut buf = String::new(); buf.push_str("\u{001b}[31m"); buf.push_str(str); - buf.push_str(reset); + buf.push_str(RESET); buf } - -const reset: &str = "\u{001b}[0m"; +const RESET: &str = "\u{001b}[0m"; impl ReportText { /// 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 pub fn render_color_terminal( &self, - _buf: &mut String, + buf: &mut String, _subs: &mut Subs, _home: ModuleId, _src_lines: &[&str], _interns: &Interns, + palette: Palette, ) { - // TODO do the same stuff as render_ci, but with colors via ANSI terminal escape codes! - // Examples of how to do this are in the source code of https://github.com/rtfeldman/console-print + use ReportText::*; + + match self { + Plain(string) => { + buf.push_str(&palette.primary.render(string)); + } + + _ => panic!("TODO implement more ReportTexts in render color terminal"), + } } } diff --git a/compiler/reporting/tests/test_reporting.rs b/compiler/reporting/tests/test_reporting.rs index de8311bf1a..fae80d0272 100644 --- a/compiler/reporting/tests/test_reporting.rs +++ b/compiler/reporting/tests/test_reporting.rs @@ -11,7 +11,7 @@ mod helpers; mod test_report { use crate::helpers::test_home; 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::subs::Subs; use roc_types::types; @@ -82,6 +82,38 @@ mod test_report { 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) { report_renders_as_from_src( indoc!( @@ -202,15 +234,23 @@ mod test_report { buf, indoc!( 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] fn report_region() { report_renders_as_from_src(