Add multiple lines and color support for popups

This commit is contained in:
2Shirt 2024-11-03 18:06:16 -08:00
parent c62c6c751c
commit c8faa283c8
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 38 additions and 11 deletions

View file

@ -16,7 +16,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::Display; use strum::Display;
use crate::{app::Mode, system::disk::Disk}; use crate::{app::Mode, components::popup::Type, system::disk::Disk};
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
pub enum Action { pub enum Action {
@ -28,7 +28,7 @@ pub enum Action {
Quit, Quit,
ClearScreen, ClearScreen,
DismissPopup, DismissPopup,
DisplayPopup(String), DisplayPopup(Type, String),
Error(String), Error(String),
Help, Help,
KeyUp, KeyUp,

View file

@ -32,7 +32,12 @@ use tracing::{debug, info};
use crate::{ use crate::{
action::Action, action::Action,
components::{ components::{
footer::Footer, fps::FpsCounter, left::Left, popup::Popup, right::Right, title::Title, footer::Footer,
fps::FpsCounter,
left::Left,
popup::{Popup, Type},
right::Right,
title::Title,
Component, Component,
}, },
config::Config, config::Config,
@ -253,9 +258,10 @@ impl App {
self.action_tx.send(Action::UpdateDiskList(disks.clone()))?; self.action_tx.send(Action::UpdateDiskList(disks.clone()))?;
} }
Mode::Done => { Mode::Done => {
self.action_tx.send(Action::DisplayPopup(String::from( self.action_tx.send(Action::DisplayPopup(
"COMPLETE\n\nThank you for using this tool!", Type::Success,
)))?; String::from("COMPLETE\n\n\nThank you for using this tool!"),
))?;
} }
_ => {} _ => {}
} }

View file

@ -15,15 +15,26 @@
// //
use color_eyre::Result; use color_eyre::Result;
use ratatui::{prelude::*, widgets::*}; use ratatui::{prelude::*, widgets::*};
use serde::{Deserialize, Serialize};
use strum::Display;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use super::Component; use super::Component;
use crate::{action::Action, app::Mode, config::Config}; use crate::{action::Action, app::Mode, config::Config};
#[derive(Default, Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
pub enum Type {
#[default]
Info,
Error,
Success,
}
#[derive(Default)] #[derive(Default)]
pub struct Popup { pub struct Popup {
command_tx: Option<UnboundedSender<Action>>, command_tx: Option<UnboundedSender<Action>>,
config: Config, config: Config,
popup_type: Type,
popup_text: String, popup_text: String,
} }
@ -56,7 +67,10 @@ impl Component for Popup {
// add any logic here that should run on every render // add any logic here that should run on every render
} }
Action::DismissPopup => self.popup_text.clear(), Action::DismissPopup => self.popup_text.clear(),
Action::DisplayPopup(new_text) => self.popup_text = String::from(new_text), Action::DisplayPopup(new_type, new_text) => {
self.popup_type = new_type;
self.popup_text = String::from(new_text);
}
Action::SetMode(Mode::ScanDisks) => self.popup_text = String::from("Scanning Disks..."), Action::SetMode(Mode::ScanDisks) => self.popup_text = String::from("Scanning Disks..."),
_ => {} _ => {}
} }
@ -69,10 +83,17 @@ impl Component for Popup {
return Ok(()); return Ok(());
} }
let popup_block = Block::default() // Set style/color
.borders(Borders::ALL) let mut style = Style::default().bold();
.style(Style::default().cyan().bold()); match self.popup_type {
let popup = Paragraph::new(vec![Line::from(Span::raw(&self.popup_text))]) Type::Info => style = style.cyan(),
Type::Error => style = style.red(),
Type::Success => style = style.green(),
}
// Build block
let popup_block = Block::default().borders(Borders::ALL).style(style);
let popup = Paragraph::new(String::from(&self.popup_text))
.block(popup_block) .block(popup_block)
.centered() .centered()
.wrap(Wrap { trim: false }); .wrap(Wrap { trim: false });