aboutsummaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/components')
-rw-r--r--src/components/global_keys.rs131
-rw-r--r--src/components/mod.rs3
-rw-r--r--src/components/status.rs86
-rw-r--r--src/components/url_manager.rs34
4 files changed, 0 insertions, 254 deletions
diff --git a/src/components/global_keys.rs b/src/components/global_keys.rs
deleted file mode 100644
index dd903b1..0000000
--- a/src/components/global_keys.rs
+++ /dev/null
@@ -1,131 +0,0 @@
-use crossterm::event::{KeyEvent, KeyEventKind};
-use ratatui::prelude::{
- Alignment, Color, Constraint, Direction, Frame, Layout, Line, Margin, Rect,
- Span, Style, Stylize,
-};
-use ratatui::widgets::block::{Block, BorderType, Title};
-use ratatui::widgets::{
- Borders, Clear, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState,
- Wrap,
-};
-
-use crate::app_action::AppAction;
-use crate::component::Component;
-use crate::keys::key_commands::{serialize_key_event, KeyCommand};
-
-#[derive(Default)]
-pub struct GlobalKeys {
- pub key_commands: Vec<KeyCommand>,
-
- pub should_show: bool,
- pub scroll: usize,
- pub scroll_state: ScrollbarState,
-}
-
-impl Component for GlobalKeys {
- fn init(&mut self) -> eyre::Result<()> {
- self.scroll_state =
- ScrollbarState::new(self.key_commands.len()).position(self.scroll);
-
- Ok(())
- }
-
- fn handle_action(&mut self, action: AppAction) {
- match action {
- AppAction::ScrollUp => {
- if self.scroll > 0 {
- self.scroll -= 1;
- }
- }
- AppAction::ScrollDown => {
- if self.scroll < self.key_commands.len() - 1 {
- self.scroll += 1;
- }
- }
- AppAction::ScrollTop => {
- self.scroll = 0;
- }
- AppAction::ScrollBottom => {
- self.scroll = self.key_commands.len() - 1;
- }
- AppAction::ShowHelpMenu => {
- self.should_show = !self.should_show;
- self.scroll = 0;
- }
-
- _ => {}
- }
- self.scroll_state = self.scroll_state.position(self.scroll);
- }
-
- fn handle_key_event(&mut self, key: KeyEvent) -> Option<AppAction> {
- if key.kind == KeyEventKind::Press {
- let key_event = serialize_key_event(key);
- for key_command in &mut self.key_commands {
- if key_command.key_code == key_event {
- return Some(key_command.action.clone());
- }
- }
- }
-
- None
- }
-
- fn render(&mut self, frame: &mut Frame, rect: Rect) -> eyre::Result<()> {
- let vertical_center = Layout::default()
- .direction(Direction::Vertical)
- .constraints([
- Constraint::Percentage(50 / 2),
- Constraint::Percentage(50),
- Constraint::Percentage(50 / 2),
- ])
- .split(rect);
- let center = Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Percentage(50 / 2),
- Constraint::Percentage(50),
- Constraint::Percentage(50 / 2),
- ])
- .split(vertical_center[1])[1];
-
- let block = Block::default()
- .title(
- Title::from("Keyboard shortcuts").alignment(Alignment::Center),
- )
- .borders(Borders::ALL)
- .border_type(BorderType::Thick)
- .style(Style::default().bg(Color::DarkGray));
-
- let mut lines: Vec<Line> = vec![];
- for key_command in &mut self.key_commands {
- let command = Span::from(key_command.key_code.clone());
- let description =
- Span::from(key_command.description.clone()).italic();
- let spacer = Span::from(" ");
-
- let line = Line::from(vec![command, spacer, description]);
- lines.push(line);
- }
-
- let commands = Paragraph::new(lines)
- .block(block)
- .wrap(Wrap { trim: true })
- .scroll((u16::try_from(self.scroll)?, 0));
-
- if self.should_show {
- frame.render_widget(Clear, center);
- frame.render_widget(commands, center);
- frame.render_stateful_widget(
- Scrollbar::new(ScrollbarOrientation::VerticalRight),
- center.inner(&Margin {
- vertical: 1,
- horizontal: 0,
- }),
- &mut self.scroll_state,
- );
- }
-
- Ok(())
- }
-}
diff --git a/src/components/mod.rs b/src/components/mod.rs
deleted file mode 100644
index 07d34ab..0000000
--- a/src/components/mod.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub mod global_keys;
-pub mod status;
-pub mod url_manager;
diff --git a/src/components/status.rs b/src/components/status.rs
deleted file mode 100644
index aa2b384..0000000
--- a/src/components/status.rs
+++ /dev/null
@@ -1,86 +0,0 @@
-use ratatui::prelude::*;
-use ratatui::widgets::*;
-
-use crate::app_action::AppAction;
-use crate::app_event::AppEvent;
-use crate::component::Component;
-use crate::keys::key_commands::serialize_key_event;
-
-#[derive(Default, Clone)]
-pub struct StatusBar {
- pub message: String,
- pub current_key: String,
- pub error: bool,
- pub url_to_open: Option<url::Url>,
-}
-
-impl Component for StatusBar {
- fn handle_key_event(
- &mut self,
- key: crossterm::event::KeyEvent,
- ) -> Option<AppAction> {
- let key_str = serialize_key_event(key);
- self.current_key = key_str;
-
- None
- }
-
- fn handle_action(&mut self, action: crate::app_action::AppAction) {
- match action.clone() {
- AppAction::StatusBarSetMessage(message) => {
- self.error = false;
- self.message = message;
- }
- AppAction::StatusBarSetError(message) => {
- self.error = true;
- self.message = message;
- }
- AppAction::OpenUrl => {
- self.url_to_open =
- Some(url::Url::parse("molerat://example.com").unwrap());
- }
- _ => {}
- }
- }
-
- fn update(&mut self) -> Option<AppEvent> {
- if let Some(url) = &self.url_to_open {
- let event = AppEvent::OpenUrl(url.clone());
- self.url_to_open = None;
- return Some(event);
- }
-
- None
- }
-
- fn render(
- &mut self,
- frame: &mut ratatui::prelude::Frame,
- rect: ratatui::prelude::Rect,
- ) -> eyre::Result<()> {
- let block =
- Block::default().style(Style::default().bg(if self.error {
- Color::Red
- } else {
- Color::DarkGray
- }));
-
- let layout = Layout::default()
- .direction(Direction::Horizontal)
- .constraints(vec![
- Constraint::Percentage(50),
- Constraint::Percentage(50),
- ])
- .split(rect);
-
- let message = Paragraph::new(self.message.clone()).block(block.clone());
- let current_key = Paragraph::new(self.current_key.clone())
- .block(block)
- .alignment(Alignment::Right);
-
- frame.render_widget(message, layout[0]);
- frame.render_widget(current_key, layout[1]);
-
- Ok(())
- }
-}
diff --git a/src/components/url_manager.rs b/src/components/url_manager.rs
deleted file mode 100644
index 6067923..0000000
--- a/src/components/url_manager.rs
+++ /dev/null
@@ -1,34 +0,0 @@
-use url::Url;
-
-use crate::app_action::AppAction;
-use crate::app_event::AppEvent;
-use crate::component::Component;
-
-#[derive(Default)]
-pub struct UrlManager {
- url: Option<Url>,
-}
-
-impl Component for UrlManager {
- fn handle_event(&mut self, event: AppEvent) -> Option<AppAction> {
- match event {
- AppEvent::OpenUrl(url) => {
- self.url = Some(url.clone());
- return Some(AppAction::StatusBarSetMessage(format!(
- "Opening {}",
- url.as_str()
- )));
- }
- _ => {}
- }
- None
- }
-
- fn render(
- &mut self,
- _frame: &mut ratatui::prelude::Frame,
- _rect: ratatui::prelude::Rect,
- ) -> eyre::Result<()> {
- Ok(())
- }
-}