Skip to content

Commit

Permalink
feat: colored tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jun 18, 2024
1 parent 52b2f97 commit 71a1072
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
22 changes: 16 additions & 6 deletions rm-main/src/ui/tabs/torrents/input_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ use ratatui::{
};
use tui_input::{Input, InputRequest};

use crate::{action::Action, ui::components::Component};
use crate::{action::Action, app, ui::components::Component};

pub struct InputManager {
input: Input,
prompt: String,
ctx: app::Ctx,
}

impl InputManager {
pub fn new(prompt: String) -> Self {
pub fn new(ctx: app::Ctx, prompt: String) -> Self {
Self {
ctx,
prompt,
input: Input::default(),
}
}

pub fn new_with_value(prompt: String, value: String) -> Self {
pub fn new_with_value(ctx: app::Ctx, prompt: String, value: String) -> Self {
Self {
ctx,
prompt,
input: Input::default().with_value(value),
}
Expand All @@ -43,12 +46,19 @@ impl Component for InputManager {
fn render(&mut self, f: &mut Frame, rect: Rect) {
f.render_widget(Clear, rect);

let paragraph_text = format!("{}{}", self.prompt, self.text());
let mut spans = vec![];

spans.push(Span::styled(
self.prompt.as_str(),
Style::default().fg(self.ctx.config.general.accent_color.as_ratatui()),
));

spans.push(Span::raw(self.text()));

let input = self.input.to_string();
let prefix_len = paragraph_text.len() - input.len();
let prefix_len = self.prompt.len() + self.text().len() - input.len();

let paragraph = Paragraph::new(paragraph_text);
let paragraph = Paragraph::new(Line::from(spans));
f.render_widget(paragraph, rect);

let cursor_offset = self.input.visual_cursor() + prefix_len;
Expand Down
6 changes: 4 additions & 2 deletions rm-main/src/ui/tabs/torrents/task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ impl TaskManager {
Action::DeleteWithFiles => self.delete_torrent(delete_torrent::Mode::WithFiles),
Action::DeleteWithoutFiles => self.delete_torrent(delete_torrent::Mode::WithoutFiles),
Action::Search => {
self.current_task =
CurrentTask::FilterBar(FilterBar::new(self.table_manager.clone()));
self.current_task = CurrentTask::FilterBar(FilterBar::new(
self.ctx.clone(),
self.table_manager.clone(),
));
Some(Action::SwitchToInputMode)
}
_ => None,
Expand Down
6 changes: 5 additions & 1 deletion rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ enum Stage {
impl AddMagnetBar {
pub fn new(ctx: app::Ctx) -> Self {
Self {
input_magnet_mgr: InputManager::new("Add (Magnet URL/ Torrent path): ".to_string()),
input_magnet_mgr: InputManager::new(
ctx.clone(),
"Add (Magnet URL/ Torrent path): ".to_string(),
),
input_location_mgr: InputManager::new_with_value(
ctx.clone(),
"Directory: ".to_string(),
ctx.session_info.download_dir.clone(),
),
Expand Down
2 changes: 1 addition & 1 deletion rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl DeleteBar {

Self {
torrents_to_delete: to_delete,
input_mgr: InputManager::new(ctx.clone(), prompt),
ctx,
input_mgr: InputManager::new(prompt),
mode,
}
}
Expand Down
4 changes: 3 additions & 1 deletion rm-main/src/ui/tabs/torrents/tasks/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ratatui::prelude::*;

use crate::{
action::Action,
app,
ui::{
components::Component,
tabs::torrents::{input_manager::InputManager, TableManager},
Expand All @@ -18,9 +19,10 @@ pub struct FilterBar {
}

impl FilterBar {
pub fn new(table_manager: Arc<Mutex<TableManager>>) -> Self {
pub fn new(ctx: app::Ctx, table_manager: Arc<Mutex<TableManager>>) -> Self {
let current_filter = table_manager.lock().unwrap().filter.lock().unwrap().clone();
let input = InputManager::new_with_value(
ctx,
"Search: ".to_string(),
current_filter.unwrap_or_default(),
);
Expand Down

0 comments on commit 71a1072

Please sign in to comment.