summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-03-01 13:17:45 -0800
committerGitHub <noreply@github.com>2020-03-01 16:17:44 -0500
commitc3661e9f0731c8e6d2952de23eaeaaa1dc6ca4da (patch)
treef8aef863ff6abdfb884dac0d213ec35f12d643a4
parentba0991ad2ba89b7a788a866419759d858b125663 (diff)
Make internel error frames dimmer (#4201)
-rw-r--r--cli/colors.rs15
-rw-r--r--cli/fmt_errors.rs68
2 files changed, 68 insertions, 15 deletions
diff --git a/cli/colors.rs b/cli/colors.rs
index 17b8b7aba..056a6e0d0 100644
--- a/cli/colors.rs
+++ b/cli/colors.rs
@@ -92,3 +92,18 @@ pub fn bold(s: String) -> impl fmt::Display {
style_spec.set_bold(true);
style(&s, style_spec)
}
+
+pub fn gray(s: String) -> impl fmt::Display {
+ let mut style_spec = ColorSpec::new();
+ style_spec.set_fg(Some(Ansi256(8)));
+ style(&s, style_spec)
+}
+
+pub fn italic_bold_gray(s: String) -> impl fmt::Display {
+ let mut style_spec = ColorSpec::new();
+ style_spec
+ .set_fg(Some(Ansi256(8)))
+ .set_bold(true)
+ .set_italic(true);
+ style(&s, style_spec)
+}
diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs
index 9979eeb29..2bf0e859b 100644
--- a/cli/fmt_errors.rs
+++ b/cli/fmt_errors.rs
@@ -19,11 +19,22 @@ pub trait DisplayFormatter {
fn format_source_name(&self) -> String;
}
-fn format_source_name(script_name: String, line: i64, column: i64) -> String {
- let script_name_c = colors::cyan(script_name);
- let line_c = colors::yellow((1 + line).to_string());
- let column_c = colors::yellow((1 + column).to_string());
- format!("{}:{}:{}", script_name_c, line_c, column_c,)
+fn format_source_name(
+ script_name: String,
+ line: i64,
+ column: i64,
+ is_internal: bool,
+) -> String {
+ let line = line + 1;
+ let column = column + 1;
+ if is_internal {
+ format!("{}:{}:{}", script_name, line, column)
+ } else {
+ let script_name_c = colors::cyan(script_name);
+ let line_c = colors::yellow(line.to_string());
+ let column_c = colors::yellow(column.to_string());
+ format!("{}:{}:{}", script_name_c, line_c, column_c)
+ }
}
/// Formats optional source, line and column into a single string.
@@ -38,7 +49,12 @@ pub fn format_maybe_source_name(
assert!(line.is_some());
assert!(column.is_some());
- format_source_name(script_name.unwrap(), line.unwrap(), column.unwrap())
+ format_source_name(
+ script_name.unwrap(),
+ line.unwrap(),
+ column.unwrap(),
+ false,
+ )
}
/// Take an optional source line and associated information to format it into
@@ -108,18 +124,39 @@ pub fn format_error_message(msg: String) -> String {
format!("{} {}", preamble, msg)
}
-fn format_stack_frame(frame: &StackFrame) -> String {
+fn format_stack_frame(frame: &StackFrame, is_internal_frame: bool) -> String {
// Note when we print to string, we change from 0-indexed to 1-indexed.
- let function_name = colors::italic_bold(frame.function_name.clone());
- let source_loc =
- format_source_name(frame.script_name.clone(), frame.line, frame.column);
-
+ let function_name = if is_internal_frame {
+ colors::italic_bold_gray(frame.function_name.clone()).to_string()
+ } else {
+ colors::italic_bold(frame.function_name.clone()).to_string()
+ };
+ let mut source_loc = format_source_name(
+ frame.script_name.clone(),
+ frame.line,
+ frame.column,
+ is_internal_frame,
+ );
+
+ // Each chunk of styled text is auto-resetted on end,
+ // which make nesting not working.
+ // Explicitly specify color for each section.
+ let mut at_prefix = " at".to_owned();
+ if is_internal_frame {
+ at_prefix = colors::gray(at_prefix).to_string();
+ }
+ if !frame.function_name.is_empty() || frame.is_eval {
+ source_loc = format!("({})", source_loc); // wrap then style
+ }
+ if is_internal_frame {
+ source_loc = colors::gray(source_loc).to_string();
+ }
if !frame.function_name.is_empty() {
- format!(" at {} ({})", function_name, source_loc)
+ format!("{} {} {}", at_prefix, function_name, source_loc)
} else if frame.is_eval {
- format!(" at eval ({})", source_loc)
+ format!("{} eval {}", at_prefix, source_loc)
} else {
- format!(" at {}", source_loc)
+ format!("{} {}", at_prefix, source_loc)
}
}
@@ -213,7 +250,8 @@ impl fmt::Display for JSError {
)?;
for frame in &self.0.frames {
- write!(f, "\n{}", format_stack_frame(&frame))?;
+ let is_internal_frame = frame.script_name.starts_with("$deno$");
+ write!(f, "\n{}", format_stack_frame(&frame, is_internal_frame))?;
}
Ok(())
}