Skip to content

Commit

Permalink
Improve error display
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Dec 2, 2024
1 parent 2c2be56 commit a0b5a7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased] - ReleaseDate

### Changes

- Wrap long error messages in response pane

## [2.3.0] - 2024-11-11

### Added
Expand Down
45 changes: 24 additions & 21 deletions crates/tui/src/view/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use chrono::{DateTime, Duration, Local, Utc};
use itertools::{Itertools, Position};
use ratatui::{
text::{Line, Span, Text},
widgets::{Block, Borders},
widgets::{Block, Borders, Paragraph, Wrap},
};
use reqwest::{header::HeaderValue, StatusCode};
use slumber_core::{
Expand Down Expand Up @@ -197,36 +197,39 @@ impl Generate for &HeaderValue {

impl Generate for &anyhow::Error {
/// 'static because string is generated
type Output<'this> = Text<'static> where Self: 'this;
type Output<'this> = Paragraph<'static> where Self: 'this;

fn generate<'this>(self) -> Self::Output<'this>
where
Self: 'this,
{
let chain = self.chain();
chain
.with_position()
.enumerate()
.map::<Line, _>(|(i, (position, error))| {
let icon = match position {
Position::First | Position::Only => "",
Position::Middle => "└┬",
Position::Last => "└─",
let mut lines: Vec<Line> = Vec::new();
for (i, (position, error)) in chain.with_position().enumerate() {
let icon = match position {
Position::First | Position::Only => "",
Position::Middle => "└┬",
Position::Last => "└─",
};
for (position, line) in error.to_string().lines().with_position() {
let line = if let Position::First | Position::Only = position {
format!(
"{indent:width$}{icon}{line}",
indent = "",
width = i.saturating_sub(1)
)
} else {
line.to_owned()
};
format!(
"{indent:width$}{icon}{error}",
indent = "",
width = i.saturating_sub(1)
)
.into()
})
.collect_vec()
.into()
lines.push(line.into());
}
}
Paragraph::new(lines).wrap(Wrap::default())
}
}

impl Generate for &RequestBuildError {
type Output<'this> = Text<'static> where Self: 'this;
type Output<'this> = Paragraph<'static> where Self: 'this;

fn generate<'this>(self) -> Self::Output<'this>
where
Expand All @@ -238,7 +241,7 @@ impl Generate for &RequestBuildError {
}

impl Generate for &RequestError {
type Output<'this> = Text<'static> where Self: 'this;
type Output<'this> = Paragraph<'static> where Self: 'this;

fn generate<'this>(self) -> Self::Output<'this>
where
Expand Down
7 changes: 2 additions & 5 deletions crates/tui/src/view/component/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use derive_more::Display;
use ratatui::{
prelude::Constraint,
text::{Line, Text},
widgets::{Paragraph, Wrap},
widgets::Paragraph,
Frame,
};
use slumber_core::template::{Prompt, Select};
Expand All @@ -46,10 +46,7 @@ impl EventHandler for ErrorModal {}

impl Draw for ErrorModal {
fn draw(&self, frame: &mut Frame, _: (), metadata: DrawMetadata) {
frame.render_widget(
Paragraph::new(self.0.generate()).wrap(Wrap::default()),
metadata.area(),
);
frame.render_widget(self.0.generate(), metadata.area());
}
}

Expand Down

0 comments on commit a0b5a7e

Please sign in to comment.