Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add upload/download icon in upload/download stat + allow hiding header row #30

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rm-config/defaults/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ accent_color = "LightMagenta"
# a little bit cluttered interface.
beginner_mode = true

# If enabled, hides header row of torrents tab
headers_hide = false

[connection]
url = "http://CHANGE_ME:9091/transmission/rpc" # REQUIRED!

Expand Down
2 changes: 2 additions & 0 deletions rm-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct General {
pub accent_color: Color,
#[serde(default = "default_beginner_mode")]
pub beginner_mode: bool,
#[serde(default)]
pub headers_hide: bool,
}

fn default_accent_color() -> Color {
Expand Down
12 changes: 9 additions & 3 deletions rm-main/src/ui/tabs/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,15 @@ impl Component for SearchTab {
.config
.general
.accent_color);
let table = Table::new(items, widths)
.header(header)
.highlight_style(table_higlight_style);

let table = {
let table = Table::new(items, widths).highlight_style(table_higlight_style);
if !self.ctx.config.general.headers_hide {
table.header(header)
} else {
table
}
};

f.render_stateful_widget(table, rest, &mut *table_lock.state.borrow_mut());

Expand Down
16 changes: 11 additions & 5 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@ impl TorrentsTab {
.general
.accent_color);

let table_widget = Table::new(torrent_rows, table_manager_lock.widths)
.header(Row::new(
table_manager_lock.header().iter().map(|s| s.as_str()),
))
.highlight_style(highlight_table_style);
let table_widget = {
let table = Table::new(torrent_rows, table_manager_lock.widths)
.highlight_style(highlight_table_style);
if !self.ctx.config.general.headers_hide {
table.header(Row::new(
table_manager_lock.header().iter().map(|s| s.as_str()),
))
} else {
table
}
};

f.render_stateful_widget(
table_widget,
Expand Down
24 changes: 14 additions & 10 deletions rm-main/src/ui/tabs/torrents/rustmission_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use ratatui::{
};
use transmission_rpc::types::{Id, Torrent, TorrentStatus};

use crate::utils::{bytes_to_human_format, seconds_to_human_format};
use crate::utils::{
bytes_to_human_format, download_speed_format, seconds_to_human_format, upload_speed_format,
};

#[derive(Clone)]
pub struct RustmissionTorrent {
Expand All @@ -23,12 +25,13 @@ pub struct RustmissionTorrent {
impl RustmissionTorrent {
pub fn to_row(&self) -> ratatui::widgets::Row {
Row::new([
self.torrent_name.as_str(),
self.size_when_done.as_str(),
self.progress.as_str(),
self.eta_secs.as_str(),
self.download_speed.as_str(),
self.upload_speed.as_str(),
Line::from(self.torrent_name.as_str()),
Line::from(""),
Line::from(self.size_when_done.as_str()),
Line::from(self.progress.as_str()),
Line::from(self.eta_secs.as_str()),
Line::from(download_speed_format(&self.download_speed)),
Line::from(upload_speed_format(&self.upload_speed)),
])
.style(self.style)
}
Expand All @@ -49,12 +52,13 @@ impl RustmissionTorrent {
}

Row::new([
torrent_name_line,
Line::from(torrent_name_line),
Line::from(""),
Line::from(self.size_when_done.as_str()),
Line::from(self.progress.as_str()),
Line::from(self.eta_secs.as_str()),
Line::from(self.download_speed.as_str()),
Line::from(self.upload_speed.as_str()),
Line::from(download_speed_format(&self.download_speed)),
Line::from(upload_speed_format(&self.upload_speed)),
])
}

Expand Down
29 changes: 16 additions & 13 deletions rm-main/src/ui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::rustmission_torrent::RustmissionTorrent;
pub struct TableManager {
ctx: app::Ctx,
pub table: GenericTable<RustmissionTorrent>,
pub widths: [Constraint; 6],
pub widths: [Constraint; 7],
pub filter: Arc<Mutex<Option<String>>>,
pub torrents_displaying_no: u16,
header: Vec<String>,
Expand All @@ -26,6 +26,7 @@ impl TableManager {
torrents_displaying_no: 0,
header: vec![
"Name".to_owned(),
"".to_owned(),
"Size".to_owned(),
"Progress".to_owned(),
"ETA".to_owned(),
Expand Down Expand Up @@ -99,18 +100,19 @@ impl TableManager {
rows
}

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

fn header_widths(&self, rows: &[RustmissionTorrent]) -> [Constraint; 6] {
fn header_widths(&self, rows: &[RustmissionTorrent]) -> [Constraint; 7] {
if !self.ctx.config.general.auto_hide {
return Self::default_widths();
}
Expand All @@ -122,23 +124,24 @@ impl TableManager {

for row in rows {
if !row.download_speed.is_empty() {
download_width = 9;
download_width = 11;
}
if !row.upload_speed.is_empty() {
upload_width = 9;
upload_width = 11;
}
if !row.progress.is_empty() {
progress_width = 9;
progress_width = 11;
}

if !row.eta_secs.is_empty() {
eta_width = 9;
eta_width = 11;
}
}

[
Constraint::Max(70), // Name
Constraint::Length(9), // Size
Constraint::Length(5), // <padding>
Constraint::Length(11), // Size
Constraint::Length(progress_width), // Progress
Constraint::Length(eta_width), // ETA
Constraint::Length(download_width), // Download
Expand Down
14 changes: 14 additions & 0 deletions rm-main/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ pub fn seconds_to_human_format(seconds: i64) -> String {
curr_string = format!("{curr_string}{rest}s");
curr_string
}

pub fn download_speed_format(download_speed: &str) -> String {
if download_speed.len() > 0 {
return format!("▼ {}", download_speed);
}
download_speed.to_string()
}

pub fn upload_speed_format(upload_speed: &str) -> String {
if upload_speed.len() > 0 {
return format!("▲ {}", upload_speed);
}
upload_speed.to_string()
}
Loading