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: display torrent errors #94

Merged
merged 1 commit into from
Aug 18, 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
23 changes: 20 additions & 3 deletions rm-main/src/tui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,33 @@ impl Component for TorrentsTab {

impl TorrentsTab {
fn render_table(&mut self, f: &mut Frame, rect: Rect) {
self.table_manager.torrents_displaying_no = rect.height;
let offset = self.table_manager.table.state.borrow().offset();
let mut torrents_displaying_no = 0;
let mut space_left = rect.height;
for torrent in self.table_manager.table.items.iter().skip(offset) {
if space_left <= 0 {
break;
}

if torrent.error.is_some() {
space_left = space_left.saturating_sub(2);
} else {
space_left -= 1;
}

torrents_displaying_no += 1;
}
self.table_manager.torrents_displaying_no = torrents_displaying_no;

let highlight_table_style = Style::default()
.on_black()
.bold()
.fg(CONFIG.general.accent_color);

let rows = self.table_manager.rows();
let table_widget = {
let table = Table::new(self.table_manager.rows(), &self.table_manager.widths)
.highlight_style(highlight_table_style);
let table =
Table::new(rows, &self.table_manager.widths).highlight_style(highlight_table_style);
if !CONFIG.general.headers_hide {
table.header(Row::new(self.table_manager.headers().iter().cloned()))
} else {
Expand Down
65 changes: 36 additions & 29 deletions rm-main/src/tui/tabs/torrents/rustmission_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::{Datelike, NaiveDateTime};
use ratatui::{
style::{Style, Stylize},
text::{Line, Span},
widgets::Row,
widgets::{Cell, Row},
};
use rm_config::CONFIG;
use rm_shared::{
Expand Down Expand Up @@ -35,9 +35,10 @@ impl RustmissionTorrent {
pub fn to_row(&self, headers: &[Header]) -> ratatui::widgets::Row {
headers
.iter()
.map(|header| self.header_to_line(*header))
.map(|header| self.header_to_cell(*header))
.collect::<Row>()
.style(self.style)
.height(if self.error.is_some() { 2 } else { 1 })
}

pub fn to_row_with_higlighted_indices(
Expand Down Expand Up @@ -116,9 +117,9 @@ impl RustmissionTorrent {

for header in headers {
if *header == Header::Name {
cells.push(std::mem::take(&mut torrent_name_line))
cells.push(std::mem::take(&mut torrent_name_line).into())
} else {
cells.push(self.header_to_line(*header).style(self.style))
cells.push(self.header_to_cell(*header).style(self.style))
}
}

Expand All @@ -129,42 +130,48 @@ impl RustmissionTorrent {
format!("{}/{}", self.download_dir, self.torrent_name)
}

fn header_to_line(&self, header: Header) -> Line {
fn header_to_cell(&self, header: Header) -> Cell {
match header {
Header::Name => Line::from(self.torrent_name.as_str()),
Header::SizeWhenDone => Line::from(self.size_when_done.as_str()),
Header::Progress => Line::from(self.progress.as_str()),
Header::Eta => Line::from(self.eta_secs.as_str()),
Header::DownloadRate => Line::from(download_speed_format(&self.download_speed)),
Header::UploadRate => Line::from(upload_speed_format(&self.upload_speed)),
Header::DownloadDir => Line::from(self.download_dir.as_str()),
Header::Padding => Line::raw(""),
Header::Name => {
if let Some(error) = &self.error {
Cell::from(format!("{}\n{error}", self.torrent_name))
} else {
Cell::from(self.torrent_name.as_str())
}
}
Header::SizeWhenDone => Cell::from(self.size_when_done.as_str()),
Header::Progress => Cell::from(self.progress.as_str()),
Header::Eta => Cell::from(self.eta_secs.as_str()),
Header::DownloadRate => Cell::from(download_speed_format(&self.download_speed)),
Header::UploadRate => Cell::from(upload_speed_format(&self.upload_speed)),
Header::DownloadDir => Cell::from(self.download_dir.as_str()),
Header::Padding => Cell::from(""),
Header::Id => match &self.id {
Id::Id(id) => Line::from(id.to_string()),
Id::Hash(hash) => Line::from(hash.as_str()),
Id::Id(id) => Cell::from(id.to_string()),
Id::Hash(hash) => Cell::from(hash.as_str()),
},
Header::UploadRatio => Line::from(self.upload_ratio.as_str()),
Header::UploadedEver => Line::from(self.uploaded_ever.as_str()),
Header::ActivityDate => time_to_line(self.activity_date),
Header::AddedDate => time_to_line(self.added_date),
Header::PeersConnected => Line::from(self.peers_connected.to_string()),
Header::UploadRatio => Cell::from(self.upload_ratio.as_str()),
Header::UploadedEver => Cell::from(self.uploaded_ever.as_str()),
Header::ActivityDate => time_to_line(self.activity_date).into(),
Header::AddedDate => time_to_line(self.added_date).into(),
Header::PeersConnected => Cell::from(self.peers_connected.to_string()),
Header::SmallStatus => {
if self.error.is_some() {
return Line::from(CONFIG.icons.failure.as_str());
return Cell::from(CONFIG.icons.failure.as_str());
}

match self.status() {
TorrentStatus::Stopped => Line::from(CONFIG.icons.pause.as_str()),
TorrentStatus::QueuedToVerify => Line::from(CONFIG.icons.loading.as_str()),
TorrentStatus::Verifying => Line::from(CONFIG.icons.verifying.as_str()),
TorrentStatus::QueuedToDownload => Line::from(CONFIG.icons.loading.as_str()),
TorrentStatus::QueuedToSeed => Line::from(CONFIG.icons.loading.as_str()),
TorrentStatus::Downloading => Line::from(CONFIG.icons.download.as_str()),
TorrentStatus::Stopped => Cell::from(CONFIG.icons.pause.as_str()),
TorrentStatus::QueuedToVerify => Cell::from(CONFIG.icons.loading.as_str()),
TorrentStatus::Verifying => Cell::from(CONFIG.icons.verifying.as_str()),
TorrentStatus::QueuedToDownload => Cell::from(CONFIG.icons.loading.as_str()),
TorrentStatus::QueuedToSeed => Cell::from(CONFIG.icons.loading.as_str()),
TorrentStatus::Downloading => Cell::from(CONFIG.icons.download.as_str()),
TorrentStatus::Seeding => {
if !self.upload_speed.is_empty() {
Line::from(CONFIG.icons.upload.as_str())
Cell::from(CONFIG.icons.upload.as_str())
} else {
Line::from(CONFIG.icons.success.as_str())
Cell::from(CONFIG.icons.success.as_str())
}
}
}
Expand Down
Loading