From: Simon Tatham Date: Sat, 27 Jan 2024 11:39:39 +0000 (+0000) Subject: Nicer handling of 'not found' in bottom-line prompts. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=04525bf547ad49442fd9321d1232db84b0e64d7a;p=mastodonochrome.git Nicer handling of 'not found' in bottom-line prompts. This was another remaining error-handling todo, now tada. --- diff --git a/TODO.md b/TODO.md index 1838cd4..2230e45 100644 --- a/TODO.md +++ b/TODO.md @@ -32,13 +32,6 @@ Error Log entries could usefully include various extra detail: context, like 'I was trying to fetch this status because this other one was replying to it'. -In the `get_user_to_examine` and `get_post_id_to_read` overlay -activities, a simple 'not found' error shouldn't throw the user into -the Error Log. That's overkill. We should instead toggle to an overlay -activity showing a friendlier bottom-line error message, and abort the -attempt. (Then the user can try again via line recall if it was just a -typo – at least, once we implement line recall.) - We don't have good handling for I/O errors while saving the user's LDB. That's not a _client_ error, but we could make an extra enum branch in `ClientError` anyway, so that the TuiLogicalState could put diff --git a/src/activity_stack.rs b/src/activity_stack.rs index ea9dadc..01e5d4c 100644 --- a/src/activity_stack.rs +++ b/src/activity_stack.rs @@ -46,6 +46,7 @@ pub enum OverlayActivity { GetPostIdToRead, GetHashtagToRead, GetSearchExpression(SearchDirection), + BottomLineError(String), } #[derive(PartialEq, Eq, Debug, Clone, Hash)] diff --git a/src/editor.rs b/src/editor.rs index 7c2834c..748f789 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,7 +1,9 @@ use std::cmp::{max, min}; use unicode_width::UnicodeWidthChar; -use super::activity_stack::{NonUtilityActivity, UtilityActivity}; +use super::activity_stack::{ + NonUtilityActivity, OverlayActivity, UtilityActivity, +}; use super::client::{Client, ClientError}; use super::coloured_string::*; use super::file::SearchDirection; @@ -739,6 +741,42 @@ impl ActivityState for BottomLineEditorOverlay { } } +struct BottomLineErrorOverlay { + line: CentredInfoLine, +} + +impl BottomLineErrorOverlay { + fn new(msg: &str) -> Self { + let msg = ColouredString::uniform("Error: ", 'r') + + ColouredString::plain(msg) + + ColouredString::general(" [RET]", " kkk "); + BottomLineErrorOverlay { + line: CentredInfoLine::new(msg), + } + } +} + +impl ActivityState for BottomLineErrorOverlay { + fn draw( + &self, + w: usize, + _h: usize, + ) -> (Vec, CursorPosition) { + (self.line.render(w), CursorPosition::End) + } + + fn handle_keypress( + &mut self, + key: OurKey, + _client: &mut Client, + ) -> LogicalAction { + match key { + Return => LogicalAction::PopOverlaySilent, + _ => LogicalAction::Nothing, + } + } +} + pub trait EditableMenuLineData { // If SECRET, then the implementor of this trait promises that // display() will show the text as ***** when it's not being @@ -949,10 +987,17 @@ pub fn get_user_to_examine() -> Box { UtilityActivity::ExamineUser(account.id).into(), ), - // FIXME: it would be nice to discriminate errors - // better here, and do something a bit less - // terrifying for plain "account not found", like - // allowing the user to re-edit + Err(ClientError::UrlFetchHTTPRich( + _, + reqwest::StatusCode::NOT_FOUND, + _, + )) => LogicalAction::Goto( + OverlayActivity::BottomLineError( + format!("User '{}' not found", s) + ) + .into(), + ), + Err(err) => LogicalAction::Error(err), } } @@ -973,10 +1018,17 @@ pub fn get_post_id_to_read() -> Box { UtilityActivity::InfoStatus(st.id).into(), ), - // FIXME: it would be nice to discriminate errors - // better here, and do something a bit less - // terrifying for plain "status not found", like - // allowing the user to re-edit + Err(ClientError::UrlFetchHTTPRich( + _, + reqwest::StatusCode::NOT_FOUND, + _, + )) => LogicalAction::Goto( + OverlayActivity::BottomLineError( + format!("Post id {} not found", s) + ) + .into(), + ), + Err(err) => LogicalAction::Error(err), } } @@ -1014,6 +1066,10 @@ pub fn get_search_expression(dir: SearchDirection) -> Box { )) } +pub fn bottom_line_error(msg: &str) -> Box { + Box::new(BottomLineErrorOverlay::new(msg)) +} + #[derive(Debug, PartialEq, Eq, Clone)] struct ComposeBufferRegion { start: usize, diff --git a/src/tui.rs b/src/tui.rs index e300394..dfeab48 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1043,6 +1043,9 @@ impl TuiLogicalState { Activity::Overlay(OverlayActivity::GetSearchExpression(dir)) => { Ok(get_search_expression(dir)) } + Activity::Overlay(OverlayActivity::BottomLineError(msg)) => { + Ok(bottom_line_error(&msg)) + } Activity::Util(UtilityActivity::ExamineUser(ref name)) => { examine_user(client, name) }