From: Simon Tatham Date: Fri, 29 Dec 2023 11:08:52 +0000 (+0000) Subject: Make a Client and put it in the Tui. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=9f44652b97a5053772f46906e331bc8e798660d0;p=mastodonochrome.git Make a Client and put it in the Tui. This involved souping up the error handling so that we can absorb errors from various different sources during Tui construction. --- diff --git a/src/auth.rs b/src/auth.rs index 1e0ea7e..3cfae97 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -7,6 +7,21 @@ pub enum AuthError { Bad(String), } +impl std::fmt::Display for AuthError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> + Result<(), std::fmt::Error> + { + match self { + AuthError::Nonexistent(_) => { + write!(f, "not logged in") + }, + AuthError::Bad(ref msg) => { + write!(f, "unable to read authentication: {}", msg) + }, + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct AuthConfig { pub account_id: String, @@ -21,7 +36,7 @@ pub struct AuthConfig { impl AuthConfig { pub fn load() -> Result { let xdg_dirs = match BaseDirectories::with_prefix("mastodonochrome") { - Err(e) => Err(AuthError::Nonexistent( + Err(e) => Err(AuthError::Bad( format!("unable to get config directory: {}", e))), Ok(d) => Ok(d), }?; diff --git a/src/tui.rs b/src/tui.rs index d1ef512..e317757 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -14,9 +14,10 @@ use std::io::{Stdout, Write, stdout}; use unicode_width::UnicodeWidthStr; use super::activity_stack::*; -//use super::client::Client; +use super::client::Client; use super::coloured_string::{ColouredString, ColouredStringSlice}; use super::menu::*; +use super::auth::AuthError; fn ratatui_style_from_colour(colour: char) -> Style { match colour { @@ -130,7 +131,7 @@ pub struct Tui { subthread_sender: std::sync::mpsc::SyncSender, subthread_receiver: std::sync::mpsc::Receiver, state: TuiLogicalState, - // FIXME: we'll need a Client here + client: Client, } #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] @@ -143,8 +144,43 @@ pub enum OurKey { PgUp, PgDn, Home, End, Ins, Del, } +pub struct TuiError { + message: String, +} + +impl TuiError { + pub fn new(message: &str) -> Self { + TuiError { + message: message.to_owned(), + } + } +} + +impl From for TuiError { + fn from(err: std::io::Error) -> Self { + TuiError { + message: err.to_string(), + } + } +} +impl From for TuiError { + fn from(err: AuthError) -> Self { + TuiError { + message: err.to_string(), + } + } +} + +impl std::fmt::Display for TuiError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> + Result<(), std::fmt::Error> + { + write!(f, "unable to read authentication: {}", self.message) + } +} + impl Tui { - pub fn run() -> std::io::Result<()> { + pub fn run() -> Result<(), TuiError> { stdout().execute(EnterAlternateScreen)?; enable_raw_mode()?; let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; @@ -163,11 +199,14 @@ impl Tui { } }); + let client = Client::new()?; + let mut tui = Tui { terminal: terminal, subthread_sender: sender, subthread_receiver: receiver, state: TuiLogicalState::new(), + client: client, }; let result = tui.main_loop(); @@ -226,7 +265,7 @@ impl Tui { } } - fn main_loop(&mut self) -> std::io::Result<()> { + fn main_loop(&mut self) -> Result<(), TuiError> { 'outer: loop { self.terminal.draw(|frame| { let area = frame.size();