diff --git a/src/handler.rs b/src/handler.rs index b991221..502cbb1 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -113,7 +113,7 @@ pub fn handle_key_events( Ok(_) => { let notif = Notification::new( format!( - "**Info**\nChat saved to `{}` file", + "Chat saved to `{}` file", app.config.archive_file_name ), NotificationLevel::Info, @@ -122,10 +122,8 @@ pub fn handle_key_events( sender.send(Event::Notification(notif)).unwrap(); } Err(e) => { - let notif = Notification::new( - format!("**Error**\n{}", e), - NotificationLevel::Error, - ); + let notif = + Notification::new(e.to_string(), NotificationLevel::Error); sender.send(Event::Notification(notif)).unwrap(); } @@ -139,20 +137,14 @@ pub fn handle_key_events( ) { Ok(_) => { let notif = Notification::new( - format!( - "**Info**\nChat saved to `{}` file", - app.config.archive_file_name - ), + format!("Chat saved to `{}` file", app.config.archive_file_name), NotificationLevel::Info, ); sender.send(Event::Notification(notif)).unwrap(); } Err(e) => { - let notif = Notification::new( - format!("**Error**\n{}", e), - NotificationLevel::Error, - ); + let notif = Notification::new(e.to_string(), NotificationLevel::Error); sender.send(Event::Notification(notif)).unwrap(); } diff --git a/src/notification.rs b/src/notification.rs index b4075d8..ec4671d 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,3 +1,11 @@ +use ratatui::{ + layout::{Alignment, Rect}, + style::{Color, Modifier, Style}, + text::{Line, Text}, + widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap}, + Frame, +}; + #[derive(Debug, Clone)] pub struct Notification { pub message: String, @@ -20,4 +28,35 @@ impl Notification { ttl: 8, } } + + pub fn render(&mut self, frame: &mut Frame, block: Rect) { + let (color, title) = match self.level { + NotificationLevel::Info => (Color::Green, "Info"), + NotificationLevel::Warning => (Color::Yellow, "Warning"), + NotificationLevel::Error => (Color::Red, "Error"), + }; + + let text = Text::from(vec![ + Line::styled( + title, + Style::default().fg(color).add_modifier(Modifier::BOLD), + ) + .alignment(Alignment::Center), + Line::raw(self.message.as_str()), + ]); + + let para = Paragraph::new(text) + .wrap(Wrap { trim: false }) + .alignment(Alignment::Center) + .block( + Block::default() + .borders(Borders::ALL) + .style(Style::default()) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)), + ); + + frame.render_widget(Clear, block); + frame.render_widget(para, block); + } } diff --git a/src/ui.rs b/src/ui.rs index 960db4c..5270ea9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,3 @@ -use crate::notification::NotificationLevel; use std; use crate::app::{App, FocusedBlock}; @@ -258,29 +257,9 @@ pub fn render(app: &mut App, frame: &mut Frame) { app.help.render(frame, area); } - for (i, n) in app.notifications.iter().enumerate() { - let border_color = match n.level { - NotificationLevel::Info => Color::Green, - NotificationLevel::Warning => Color::Yellow, - NotificationLevel::Error => Color::Red, - }; - - let block = Paragraph::new(if !n.message.is_empty() { - Text::from(n.message.as_str()) - } else { - Text::from("") - }) - .wrap(Wrap { trim: false }) - .alignment(Alignment::Center) - .block( - Block::default() - .borders(Borders::ALL) - .style(Style::default()) - .border_type(BorderType::Rounded) - .border_style(Style::default().fg(border_color)), - ); + // Show notifications + for (i, notif) in app.notifications.iter_mut().enumerate() { let area = notification_rect(i as u16, frame_size); - frame.render_widget(Clear, area); - frame.render_widget(block, area); + notif.render(frame, area); } }