Skip to content

Commit

Permalink
refactor: implement render for Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Jan 23, 2024
1 parent 57855a2 commit bbea87c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
18 changes: 5 additions & 13 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
39 changes: 39 additions & 0 deletions src/notification.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}
}
27 changes: 3 additions & 24 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::notification::NotificationLevel;
use std;

use crate::app::{App, FocusedBlock};
Expand Down Expand Up @@ -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);
}
}

0 comments on commit bbea87c

Please sign in to comment.