Skip to content

Commit

Permalink
feat: Add colors to error messages in book (#2253)
Browse files Browse the repository at this point in the history
Co-authored-by: Aljaž Mur Eržen <[email protected]>
  • Loading branch information
max-sixty and aljazerzen authored May 16, 2023
1 parent 0f04b2e commit 65b7fef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/book/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ name = "mdbook-prql"
test = false

[dependencies]
ansi-to-html = "0.1.2"
anyhow = "1.0.57"
globset = "0.4.8"
itertools = "0.10.3"
Expand Down
53 changes: 22 additions & 31 deletions web/book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,22 @@ fn replace_examples(text: &str) -> Result<String> {
};

let prql = text.to_string();
let options = prql_compiler::Options::default().no_signature();
let options = prql_compiler::Options::default()
.no_signature()
.with_color(true);
let result = compile(&prql, &options);

if lang_tags.contains(&LangTag::NoTest) {
cmark_acc.push(Event::Html(table_of_prql_only(&prql).into()));
} else if lang_tags.contains(&LangTag::Error) {
cmark_acc.push(Event::Html(
table_of_error(
&prql,
result
.expect_err(
&format!(
"Query was labeled to raise an error, but succeeded.\n {prql}\n\n"
)
.to_string(),
)
.to_string()
.as_str(),
)
.into(),
))
let error_message = match result {
Ok(prql) => {
panic!("Query was labeled to raise an error, but succeeded.\n{prql}\n\n")
}
Err(e) => ansi_to_html::convert_escaped(e.to_string().as_str()).unwrap(),
};

cmark_acc.push(Event::Html(table_of_error(&prql, &error_message).into()))
} else {
// Either a bare `prql` or with `no-fmt`
cmark_acc.push(Event::Html(
Expand Down Expand Up @@ -256,7 +251,7 @@ fn table_of_prql_only(prql: &str) -> String {
}

// Exactly the same as `table_of_comparison`, but with a different title for the second column.
fn table_of_error(prql: &str, error: &str) -> String {
fn table_of_error(prql: &str, message: &str) -> String {
format!(
r#"
<div class="comparison">
Expand All @@ -273,16 +268,14 @@ fn table_of_error(prql: &str, error: &str) -> String {
<div>
<h4>Error</h4>
```
{error}
```
<pre><code class="hljs language-undefined">{message}</code></pre>
</div>
</div>
"#,
prql = prql.trim(),
error = error,
message = message,
)
.trim_start()
.to_string()
Expand Down Expand Up @@ -356,16 +349,14 @@ this is an error
<div>
<h4>Error</h4>
```
Error:
╭─[:1:1]
1 │ this is an error
│ ──┬─
│ ╰─── Unknown name this
───╯
```
<pre><code class="hljs language-undefined"><span style='color:#a00'>Error:</span>
<span style='color:#949494'>╭─[</span>:1:1<span style='color:#949494'>]</span>
<span style='color:#949494'>│</span>
<span style='color:#949494'>1 │</span> this<span style='color:#b2b2b2'> is an error</span>
<span style='color:#585858'> │</span> ──┬─
<span style='color:#585858'> │</span> ╰─── Unknown name this
<span style='color:#949494'>───╯</span>
</code></pre>
</div>
Expand Down

0 comments on commit 65b7fef

Please sign in to comment.