Skip to content

Commit

Permalink
restructure the project better
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 13, 2024
1 parent 677ce78 commit 443acf3
Show file tree
Hide file tree
Showing 31 changed files with 152 additions and 168 deletions.
6 changes: 2 additions & 4 deletions rm-main/src/tui/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::sync::Arc;

use crate::tui::ui::components::Component;
use crate::{
transmission::{self, TorrentAction},
tui::Tui,
tui::components::Component,
};

use rm_config::CONFIG;
Expand All @@ -14,8 +13,7 @@ use crossterm::event::{Event, KeyCode, KeyModifiers};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use transmission_rpc::{types::SessionGet, TransClient};

use super::ui::components::tabs::CurrentTab;
use super::ui::MainWindow;
use super::{components::tabs::CurrentTab, main_window::MainWindow, terminal::Tui};

#[derive(Clone)]
pub struct Ctx {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ratatui::Frame;

use rm_shared::action::Action;
use rm_shared::action::UpdateAction;
pub use tabs::TabComponent;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ComponentAction {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use ratatui::{

use rm_shared::action::Action;

use crate::tui::ui::{
centered_rect,
use crate::tui::{
components::{Component, ComponentAction},
main_window::centered_rect,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ use rm_shared::action::Action;

use crate::tui::{
app,
ui::{
centered_rect,
components::{Component, ComponentAction},
},
components::{Component, ComponentAction},
main_window::centered_rect,
};

macro_rules! add_line {
Expand Down
File renamed without changes.
22 changes: 8 additions & 14 deletions rm-main/src/tui/ui/mod.rs → rm-main/src/tui/main_window.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
pub mod components;
pub mod global_popups;
pub mod tabs;

use components::ComponentAction;
use global_popups::ErrorPopup;
use ratatui::prelude::*;
use tabs::torrents::TorrentsTab;

use rm_shared::action::{Action, UpdateAction};

use self::{
components::{tabs::CurrentTab, Component, TabComponent},
global_popups::GlobalPopupManager,
tabs::search::SearchTab,
};
use crate::tui::components::tabs::CurrentTab;

use super::app;
use super::{
app,
components::{tabs::TabComponent, Component, ComponentAction},
global_popups::{ErrorPopup, GlobalPopupManager},
tabs::{search::SearchTab, torrents::TorrentsTab},
};

pub struct MainWindow {
pub tabs: TabComponent,
Expand Down Expand Up @@ -97,7 +91,7 @@ impl Component for MainWindow {
}
}

fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
pub fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
let popup_layout = Layout::vertical([
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Expand Down
114 changes: 5 additions & 109 deletions rm-main/src/tui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,6 @@
pub mod app;
mod ui;

use std::{io, time::Duration};

use anyhow::Result;
use crossterm::{
cursor,
event::{Event, KeyEventKind},
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
};
use futures::{FutureExt, StreamExt};
use ratatui::{backend::CrosstermBackend as Backend, Terminal};
use tokio::{
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
task::JoinHandle,
};
use tokio_util::sync::CancellationToken;

pub struct Tui {
pub terminal: Terminal<Backend<std::io::Stdout>>,
pub task: JoinHandle<Result<()>>,
pub cancellation_token: CancellationToken,
pub event_rx: UnboundedReceiver<Event>,
pub event_tx: UnboundedSender<Event>,
}

impl Tui {
pub(crate) fn new() -> Result<Self> {
let terminal = Terminal::new(Backend::new(std::io::stdout()))?;
let (event_tx, event_rx) = mpsc::unbounded_channel();
let cancellation_token = CancellationToken::new();
let task = tokio::spawn(async { Ok(()) });
Ok(Self {
terminal,
task,
cancellation_token,
event_rx,
event_tx,
})
}

fn handle_crossterm_event(
event: Option<Result<Event, io::Error>>,
event_tx: &UnboundedSender<Event>,
) -> Result<()> {
match event {
Some(Ok(Event::Key(key))) => {
if key.kind == KeyEventKind::Press {
event_tx.send(Event::Key(key)).unwrap();
}
}
Some(Ok(Event::Resize(x, y))) => event_tx.send(Event::Resize(x, y)).unwrap(),
Some(Err(e)) => Err(e)?,
_ => (),
}
Ok(())
}

pub(crate) fn enter(&mut self) -> Result<()> {
crossterm::terminal::enable_raw_mode()?;
crossterm::execute!(std::io::stdout(), EnterAlternateScreen, cursor::Hide)?;
self.start()?;
Ok(())
}

pub fn start(&mut self) -> Result<()> {
self.cancellation_token = CancellationToken::new();
let cancellation_token = self.cancellation_token.clone();
let event_tx = self.event_tx.clone();

self.task = tokio::spawn(async move {
let mut reader = crossterm::event::EventStream::new();
loop {
let crossterm_event = reader.next().fuse();
tokio::select! {
_ = cancellation_token.cancelled() => break,
event = crossterm_event => Self::handle_crossterm_event(event, &event_tx)?,
}
}
Ok(())
});
Ok(())
}

pub(crate) fn exit(&mut self) -> Result<()> {
self.cancellation_token.cancel();
let mut counter = 0;
while !self.task.is_finished() {
std::thread::sleep(Duration::from_millis(1));
counter += 1;
if counter > 50 {
self.task.abort();
}
if counter > 100 {
break;
}
}
if crossterm::terminal::is_raw_mode_enabled()? {
self.terminal.flush()?;
crossterm::execute!(std::io::stdout(), LeaveAlternateScreen, cursor::Show)?;
crossterm::terminal::disable_raw_mode()?;
}
Ok(())
}

pub async fn next(&mut self) -> Option<Event> {
self.event_rx.recv().await
}
}
mod components;
mod global_popups;
pub mod main_window;
mod tabs;
pub mod terminal;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rm_config::CONFIG;
use rm_shared::action::{Action, UpdateAction};
use throbber_widgets_tui::ThrobberState;

use crate::tui::{app, ui::components::Component};
use crate::tui::{app, components::Component};

use super::{ConfiguredProvider, ProviderState};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
transmission::TorrentAction,
tui::{
app,
ui::components::{table::GenericTable, Component, ComponentAction},
components::{table::GenericTable, Component, ComponentAction},
},
};
use rm_shared::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use ratatui::Frame;
use rm_shared::action::Action;

use crate::tui::app;
use crate::tui::ui::components::Component;
use crate::tui::ui::components::ComponentAction;
use crate::tui::components::Component;
use crate::tui::components::ComponentAction;

use super::ConfiguredProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use ratatui::{
use rm_config::CONFIG;
use rm_shared::action::Action;

use crate::tui::ui::{
centered_rect,
use crate::tui::{
components::{Component, ComponentAction},
main_window::centered_rect,
tabs::search::{ConfiguredProvider, ProviderState},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ratatui::{
use rm_shared::utils::bytes_to_human_format;
use transmission_rpc::types::{FreeSpace, SessionStats};

use crate::tui::ui::components::Component;
use crate::tui::components::Component;

use super::table_manager::TableManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ratatui::{
use rm_config::CONFIG;
use tui_input::{backend::crossterm::to_input_request, Input, InputResponse};

use crate::tui::ui::components::Component;
use crate::tui::components::Component;

pub struct InputManager {
input: Input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod tasks;

use crate::transmission::TorrentAction;
use crate::tui::app;
use crate::tui::ui::components::{Component, ComponentAction};
use crate::tui::components::{Component, ComponentAction};

use popups::stats::StatisticsPopup;
use ratatui::prelude::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ use crate::{
transmission::TorrentAction,
tui::{
app,
ui::{
centered_rect,
components::{Component, ComponentAction},
},
components::{Component, ComponentAction},
main_window::centered_rect,
},
};
use rm_shared::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::tui::{
app,
ui::components::{Component, ComponentAction},
components::{Component, ComponentAction},
};

use self::{files::FilesPopup, stats::StatisticsPopup};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use transmission_rpc::types::SessionStats;

use rm_shared::{action::Action, utils::bytes_to_human_format};

use crate::tui::ui::{
centered_rect,
use crate::tui::{
components::{Component, ComponentAction},
main_window::centered_rect,
};

pub struct StatisticsPopup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rm_config::CONFIG;
use rm_shared::header::Header;
use std::collections::HashMap;

use crate::tui::ui::components::table::GenericTable;
use crate::tui::components::table::GenericTable;

use super::rustmission_torrent::RustmissionTorrent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rm_shared::{

use crate::tui::{
app,
ui::components::{Component, ComponentAction},
components::{Component, ComponentAction},
};

use super::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use crate::{
transmission::TorrentAction,
tui::{
app,
ui::{
components::{Component, ComponentAction},
tabs::torrents::input_manager::InputManager,
},
components::{Component, ComponentAction},
tabs::torrents::input_manager::InputManager,
},
};
use rm_shared::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ratatui::prelude::*;
use rm_config::CONFIG;

use crate::tui::ui::components::Component;
use crate::tui::components::Component;

pub struct DefaultBar {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use transmission_rpc::types::Id;

use crate::transmission::TorrentAction;
use crate::tui::app;
use crate::tui::ui::components::{Component, ComponentAction};
use crate::tui::ui::tabs::torrents::input_manager::InputManager;
use crate::tui::components::{Component, ComponentAction};
use crate::tui::tabs::torrents::input_manager::InputManager;
use rm_shared::action::{Action, UpdateAction};
use rm_shared::status_task::StatusTask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use rm_shared::action::{Action, UpdateAction};

use crate::tui::{
app,
ui::{
components::{Component, ComponentAction},
tabs::torrents::{input_manager::InputManager, table_manager::Filter},
},
components::{Component, ComponentAction},
tabs::torrents::{input_manager::InputManager, table_manager::Filter},
};

pub struct FilterBar {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use crate::{
transmission::TorrentAction,
tui::{
app,
ui::{
components::{Component, ComponentAction},
tabs::torrents::input_manager::InputManager,
},
components::{Component, ComponentAction},
tabs::torrents::input_manager::InputManager,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rm_shared::{
use throbber_widgets_tui::ThrobberState;
use tokio::time::{self, Instant};

use crate::tui::{app, ui::components::Component};
use crate::tui::{app, components::Component};

pub struct StatusBar {
task: StatusTask,
Expand Down
Loading

0 comments on commit 443acf3

Please sign in to comment.