2021-07-18 15:50:39 +00:00
|
|
|
use super::{Component, DrawableComponent, EventState};
|
2021-07-29 10:47:36 +00:00
|
|
|
use crate::components::command::CommandInfo;
|
2021-07-31 16:03:39 +00:00
|
|
|
use crate::config::KeyConfig;
|
2021-07-09 10:32:55 +00:00
|
|
|
use crate::event::Key;
|
|
|
|
use anyhow::Result;
|
|
|
|
use tui::{
|
|
|
|
backend::Backend,
|
|
|
|
layout::{Alignment, Rect},
|
|
|
|
style::{Color, Style},
|
|
|
|
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
|
|
|
Frame,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct ErrorComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
pub error: String,
|
|
|
|
visible: bool,
|
|
|
|
key_config: KeyConfig,
|
2021-07-09 10:32:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
impl ErrorComponent {
|
|
|
|
pub fn new(key_config: KeyConfig) -> Self {
|
|
|
|
Self {
|
|
|
|
error: String::new(),
|
|
|
|
visible: false,
|
|
|
|
key_config,
|
|
|
|
}
|
2021-07-09 10:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrorComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
pub fn set(&mut self, error: String) -> anyhow::Result<()> {
|
|
|
|
self.error = error;
|
|
|
|
self.show()
|
2021-07-09 10:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawableComponent for ErrorComponent {
|
2021-09-17 06:16:31 +00:00
|
|
|
fn draw<B: Backend>(&self, f: &mut Frame<B>, _area: Rect, _focused: bool) -> Result<()> {
|
2021-07-31 16:03:39 +00:00
|
|
|
if self.visible {
|
2021-07-09 10:32:55 +00:00
|
|
|
let width = 65;
|
|
|
|
let height = 10;
|
2021-07-31 16:03:39 +00:00
|
|
|
let error = Paragraph::new(self.error.to_string())
|
2021-07-09 10:32:55 +00:00
|
|
|
.block(Block::default().title("Error").borders(Borders::ALL))
|
|
|
|
.style(Style::default().fg(Color::Red))
|
|
|
|
.alignment(Alignment::Left)
|
|
|
|
.wrap(Wrap { trim: true });
|
|
|
|
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.render_widget(Clear, area);
|
|
|
|
f.render_widget(error, area);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for ErrorComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
2021-07-29 10:47:36 +00:00
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
|
|
|
if self.visible {
|
|
|
|
if key == self.key_config.exit_popup {
|
|
|
|
self.error = String::new();
|
|
|
|
self.hide();
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
}
|
|
|
|
return Ok(EventState::NotConsumed);
|
|
|
|
}
|
2021-07-18 15:50:39 +00:00
|
|
|
Ok(EventState::NotConsumed)
|
2021-07-09 10:32:55 +00:00
|
|
|
}
|
2021-07-31 16:03:39 +00:00
|
|
|
|
|
|
|
fn hide(&mut self) {
|
|
|
|
self.visible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show(&mut self) -> Result<()> {
|
|
|
|
self.visible = true;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-09 10:32:55 +00:00
|
|
|
}
|