Skip to content

Commit

Permalink
refactor: various code cleanups (#24)
Browse files Browse the repository at this point in the history
* refactor: move getting rows to table manager

* chore: move rm-color to rm-config

* chore: remove rm-color crate

* fix: add missing deps for color tests
  • Loading branch information
micielski authored Jun 21, 2024
1 parent 55b21bb commit e4f0a63
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 73 deletions.
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"rm-main",
"rm-config", "rm-color",
"rm-config",
]

# Config for 'cargo dist'
Expand Down
11 changes: 0 additions & 11 deletions rm-color/Cargo.toml

This file was deleted.

5 changes: 4 additions & 1 deletion rm-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ serde = { version = "1", features = ["derive"] }
anyhow = "1"
url = "2.5"
ratatui = "0.26"
rm-color = { version = "0.1.0", path = "../rm-color" }

[dev-dependencies]
serde_json = "1"

File renamed without changes.
6 changes: 4 additions & 2 deletions rm-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod color;

use std::{
fs::File,
io::{Read, Write},
Expand All @@ -6,7 +8,7 @@ use std::{
};

use anyhow::{bail, Context, Result};
use rm_color::Color;
use color::Color;
use serde::{Deserialize, Serialize};
use toml::Table;
use xdg::BaseDirectories;
Expand All @@ -21,7 +23,7 @@ pub struct Config {
pub struct General {
#[serde(default)]
pub auto_hide: bool,
#[serde(default, with = "rm_color")]
#[serde(default, with = "color")]
pub accent_color: Color,
#[serde(default = "default_beginner_mode")]
pub beginner_mode: bool,
Expand Down
18 changes: 2 additions & 16 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{app, transmission};
use self::bottom_stats::BottomStats;
use self::popups::files::FilesPopup;
use self::popups::{CurrentPopup, PopupManager};
use self::rustmission_torrent::RustmissionTorrent;
use self::table_manager::TableManager;
use self::task_manager::TaskManager;

Expand All @@ -41,7 +40,7 @@ impl TorrentsTab {
let table_manager = Arc::new(Mutex::new(TableManager::new(ctx.clone(), table)));
let stats = Arc::new(Mutex::new(None));
let free_space = Arc::new(Mutex::new(None));
let bottom_stats = BottomStats::new(stats, free_space, table_manager.clone());
let bottom_stats = BottomStats::new(stats, free_space, Arc::clone(&table_manager));

tokio::spawn(transmission::fetchers::stats(
ctx.clone(),
Expand Down Expand Up @@ -113,20 +112,7 @@ impl TorrentsTab {
let table_manager_lock = &mut *self.table_manager.lock().unwrap();
table_manager_lock.torrents_displaying_no = rect.height;

let torrent_rows: Vec<_> = if let Some(filter) = &*table_manager_lock.filter.lock().unwrap()
{
let torrent_rows =
table_manager_lock.filtered_torrents_rows(&table_manager_lock.table.items, filter);
table_manager_lock.table.overwrite_len(torrent_rows.len());
torrent_rows
} else {
table_manager_lock
.table
.items
.iter()
.map(RustmissionTorrent::to_row)
.collect()
};
let torrent_rows = table_manager_lock.rows();

let highlight_table_style = Style::default().on_black().bold().fg(self
.ctx
Expand Down
78 changes: 46 additions & 32 deletions rm-main/src/ui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::Row};
use std::sync::{Arc, Mutex};

use crate::{app, ui::components::table::GenericTable};
Expand Down Expand Up @@ -35,39 +35,22 @@ impl TableManager {
}
}

pub const fn header(&self) -> &Vec<String> {
&self.header
}

const fn default_widths() -> [Constraint; 6] {
[
Constraint::Max(70), // Name
Constraint::Length(10), // Size
Constraint::Length(10), // Progress
Constraint::Length(10), // ETA
Constraint::Length(10), // Download
Constraint::Length(10), // Upload
]
}

pub fn filtered_torrents_rows<'a>(
&self,
torrents: &'a [RustmissionTorrent],
filter: &str,
) -> Vec<ratatui::widgets::Row<'a>> {
let matcher = SkimMatcherV2::default();
let mut rows = vec![];

let highlight_style =
Style::default().fg(self.ctx.config.general.accent_color.as_ratatui());

for torrent in torrents {
if let Some((_, indices)) = matcher.fuzzy_indices(&torrent.torrent_name, filter) {
rows.push(torrent.to_row_with_higlighted_indices(indices, highlight_style))
}
pub fn rows(&self) -> Vec<Row<'_>> {
if let Some(filter) = &*self.filter.lock().unwrap() {
let rows = self.filtered_torrents_rows(&self.table.items, filter);
self.table.overwrite_len(rows.len());
rows
} else {
self.table
.items
.iter()
.map(RustmissionTorrent::to_row)
.collect()
}
}

rows
pub const fn header(&self) -> &Vec<String> {
&self.header
}

pub fn current_torrent(&mut self) -> Option<&mut RustmissionTorrent> {
Expand Down Expand Up @@ -97,6 +80,37 @@ impl TableManager {
self.widths = self.header_widths(&self.table.items);
}

fn filtered_torrents_rows<'a>(
&self,
torrents: &'a [RustmissionTorrent],
filter: &str,
) -> Vec<Row<'a>> {
let matcher = SkimMatcherV2::default();
let mut rows = vec![];

let highlight_style =
Style::default().fg(self.ctx.config.general.accent_color.as_ratatui());

for torrent in torrents {
if let Some((_, indices)) = matcher.fuzzy_indices(&torrent.torrent_name, filter) {
rows.push(torrent.to_row_with_higlighted_indices(indices, highlight_style))
}
}

rows
}

const fn default_widths() -> [Constraint; 6] {
[
Constraint::Max(70), // Name
Constraint::Length(10), // Size
Constraint::Length(10), // Progress
Constraint::Length(10), // ETA
Constraint::Length(10), // Download
Constraint::Length(10), // Upload
]
}

fn header_widths(&self, rows: &[RustmissionTorrent]) -> [Constraint; 6] {
if !self.ctx.config.general.auto_hide {
return Self::default_widths();
Expand Down

0 comments on commit e4f0a63

Please sign in to comment.