From 6b888bbfe45568825a5981c9ca3e7a65e3e3921e Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 11 Nov 2016 17:15:16 -0800 Subject: [PATCH] Fix off by one error in backtick error message (#52) I was using the width of the index of the line, not the displayed line number, which is the index + 1, which could cause the error message to be misaligned. Fixed it, and added a test that checks for this. --- src/integration.rs | 15 +++++++++++++++ src/lib.rs | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/integration.rs b/src/integration.rs index b189b91..283872a 100644 --- a/src/integration.rs +++ b/src/integration.rs @@ -371,6 +371,21 @@ fn backtick_code_interpolation() { ); } +#[test] +fn backtick_code_long() { + integration_test( + &[], + "\n\n\n\n\n\nb = a\na = `echo hello`\nbar:\n echo '{{`exit 200`}}'", + 200, + "", + "backtick failed with exit code 200 + | +10 | echo '{{`exit 200`}}' + | ^^^^^^^^^^ +", + ); +} + #[test] fn shebang_backtick_failure() { integration_test( diff --git a/src/lib.rs b/src/lib.rs index 00b4ea1..f218d20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1068,9 +1068,10 @@ impl<'a> Display for RunError<'a> { try!(write!(f, "backtick failed with exit code {}\n", code)); match token.text.lines().nth(token.line) { Some(line) => { - let line_number_width = token.line.to_string().len(); + let displayed_line = token.line + 1; + let line_number_width = displayed_line.to_string().len(); try!(write!(f, "{0:1$} |\n", "", line_number_width)); - try!(write!(f, "{} | {}\n", token.line + 1, line)); + try!(write!(f, "{} | {}\n", displayed_line, line)); try!(write!(f, "{0:1$} |", "", line_number_width)); try!(write!(f, " {0:1$}{2:^<3$}", "", token.column + token.prefix.len(), "", token.lexeme.len()));