Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a message in error modal to show close button #67

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/components/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::config::KeyConfig;
use crate::event::Key;
use anyhow::Result;
use ratatui::{
layout::{Alignment, Rect},
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph, Wrap},
Frame,
};
Expand All @@ -17,16 +18,16 @@ pub struct ErrorComponent {
}

impl ErrorComponent {
const WIDTH: u16 = 65;
const HEIGHT: u16 = 10;
pub fn new(key_config: KeyConfig) -> Self {
Self {
error: String::new(),
visible: false,
key_config,
}
}
}

impl ErrorComponent {
pub fn set(&mut self, error: String) -> anyhow::Result<()> {
self.error = error;
self.show()
Expand All @@ -36,21 +37,41 @@ impl ErrorComponent {
impl DrawableComponent for ErrorComponent {
fn draw(&self, f: &mut Frame, _area: Rect, _focused: bool) -> Result<()> {
if self.visible {
let width = 65;
let height = 10;
let error = Paragraph::new(self.error.to_string())
.block(Block::default().title("Error").borders(Borders::ALL))
.style(Style::default().fg(Color::Red))
.alignment(Alignment::Left)
.wrap(Wrap { trim: true });
let error = Block::default()
.title("Error")
.borders(Borders::ALL)
.style(Style::default().fg(Color::Red));

let area = Rect::new(
(f.size().width.saturating_sub(width)) / 2,
(f.size().height.saturating_sub(height)) / 2,
width.min(f.size().width),
height.min(f.size().height),
(f.size().width.saturating_sub(Self::WIDTH)) / 2,
(f.size().height.saturating_sub(Self::HEIGHT)) / 2,
Self::WIDTH.min(f.size().width),
Self::HEIGHT.min(f.size().height),
);
let chunks = Layout::default()
.vertical_margin(1)
.horizontal_margin(1)
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(1)].as_ref())
.split(area);

f.render_widget(Clear, area);
f.render_widget(error, area);
f.render_widget(
Paragraph::new(self.error.to_string()).wrap(Wrap { trim: true }),
chunks[0],
);
f.render_widget(
Paragraph::new(Line::from(vec![Span::styled(
format!(
"Press [{}] to close this modal.",
self.key_config.exit_popup
),
Style::default(),
)]))
.alignment(Alignment::Right),
chunks[1],
);
}
Ok(())
}
Expand Down
13 changes: 7 additions & 6 deletions src/components/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ pub struct HelpComponent {
impl DrawableComponent for HelpComponent {
fn draw(&self, f: &mut Frame, _area: Rect, _focused: bool) -> Result<()> {
if self.visible {
const SIZE: (u16, u16) = (65, 24);
let scroll_threshold = SIZE.1 / 3;
let scroll_threshold = Self::HEIGHT / 3;
let scroll = self.selection.saturating_sub(scroll_threshold);

let area = Rect::new(
(f.size().width.saturating_sub(SIZE.0)) / 2,
(f.size().height.saturating_sub(SIZE.1)) / 2,
SIZE.0.min(f.size().width),
SIZE.1.min(f.size().height),
(f.size().width.saturating_sub(Self::WIDTH)) / 2,
(f.size().height.saturating_sub(Self::HEIGHT)) / 2,
Self::WIDTH.min(f.size().width),
Self::HEIGHT.min(f.size().height),
);

f.render_widget(Clear, area);
Expand Down Expand Up @@ -105,6 +104,8 @@ impl Component for HelpComponent {
}

impl HelpComponent {
const WIDTH: u16 = 65;
const HEIGHT: u16 = 24;
pub const fn new(key_config: KeyConfig) -> Self {
Self {
cmds: vec![],
Expand Down
Loading