From b00a41f684cb1c1fe522343e2cdc9063c9ef8058 Mon Sep 17 00:00:00 2001 From: Luke Frisken Date: Sun, 14 Apr 2024 14:50:19 +1000 Subject: [PATCH] Improved error formatting on index page --- .gitignore | 1 + src/index.rs | 2 +- src/templates.rs | 25 +++++++++++++++++++++++++ src/templates/index.html | 6 +++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ae18cc2..b7a8172 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /dist /data .env.toml +.env.toml.* .env # Spreadsheet Lock Files diff --git a/src/index.rs b/src/index.rs index 51006af..8df3f03 100644 --- a/src/index.rs +++ b/src/index.rs @@ -218,7 +218,7 @@ pub async fn handler( .fold((Vec::new(), Vec::new()), |mut acc, result| async move { match result { Ok(ok) => acc.0.push(ok), - Err(error) => acc.1.push(error.to_string()), + Err(error) => acc.1.push(format!("{error:?}")), } acc }) diff --git a/src/templates.rs b/src/templates.rs index 1729a88..f4e37e6 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -433,6 +433,31 @@ pub async fn middleware( }, ) }); + environment.add_function("limit_line_length", |string: &str, length: usize| { + let mut new_string = String::with_capacity(string.len()); + for line in string.lines() { + let mut line = line; + if line.chars().count() <= length { + new_string.push_str(line); + new_string.push('\n'); + continue; + } + while line.chars().count() > length { + let split_position = line + .char_indices() + .enumerate() + .find_map(|(i, (p, _c))| if i >= length { Some(p) } else { None }) + .expect("Expected to find split position"); + let (before, after) = line.split_at(split_position); + new_string.push_str(before); + new_string.push('\n'); + line = after; + } + new_string.push_str(line); + new_string.push('\n'); + } + new_string + }); let uri = request.uri(); let query_value: Value = uri .query() diff --git a/src/templates/index.html b/src/templates/index.html index 5a09159..36e56c8 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -94,7 +94,11 @@

{{ fl("forecast-archive-heading") }}

Errors Reading Forecast Files

{% for error in errors %}

Error {{ loop.index }}

-
{{ ansi_to_html(error) }}
+ {% autoescape false %} +
{{ ansi_to_html(error) }}
+ {% endautoescape %} {% endfor %} {% endif %}