chore: update ratatui version

This commit is contained in:
Kristofers Solo
2024-01-30 15:41:14 +02:00
parent 695a0c3b27
commit 7dac02029d
8 changed files with 212 additions and 54 deletions

View File

@@ -1,5 +1,6 @@
mod tab;
mod torrent;
pub mod utils;
use ratatui::widgets::TableState;

View File

@@ -38,6 +38,7 @@ impl Tab {
],
Tab::Downloading => &[
TorrentGetField::TotalSize,
TorrentGetField::LeftUntilDone,
TorrentGetField::PercentDone,
TorrentGetField::RateDownload,
TorrentGetField::Eta,

35
src/app/utils/filesize.rs Normal file
View File

@@ -0,0 +1,35 @@
pub struct FileSize(pub i64);
impl FileSize {
pub fn to_b(&self) -> String {
format!("{} b", self.0)
}
pub fn to_kb(&self) -> String {
format!("{:.2} KB", self.0 as f64 / 1e3)
}
pub fn to_mb(&self) -> String {
format!("{:.2} MB", self.0 as f64 / 1e6)
}
pub fn to_gb(&self) -> String {
format!("{:.2} GB", self.0 as f64 / 1e9)
}
pub fn to_tb(&self) -> String {
format!("{:.2} TB", self.0 as f64 / 1e12)
}
}
impl ToString for FileSize {
fn to_string(&self) -> String {
match self.0 as f64 {
b if b >= 1e12 => self.to_tb(),
b if b >= 1e9 => self.to_gb(),
b if b >= 1e6 => self.to_mb(),
b if b >= 1e3 => self.to_kb(),
_ => self.to_b(),
}
}
}

View File

@@ -1,5 +1,11 @@
use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus};
mod filesize;
mod netspeed;
use filesize::FileSize;
use self::netspeed::NetSpeed;
pub trait Wrapper {
fn title(&self) -> String {
String::from("")
@@ -79,8 +85,9 @@ impl Wrapper for TorrentGetField {
TorrentGetField::ErrorString => optional_to_string(torrent.error_string),
TorrentGetField::Eta => match torrent.eta {
Some(eta) => match eta {
-1 => String::from(""),
_ => eta.to_string(),
-1 => "".into(),
-2 => "?".into(),
_ => format!("{} s", eta),
},
None => String::from(""),
},
@@ -101,7 +108,9 @@ impl Wrapper for TorrentGetField {
Some(labels) => labels.join(" "),
None => String::from("N/A"),
},
TorrentGetField::LeftUntilDone => optional_to_string(torrent.left_until_done),
TorrentGetField::LeftUntilDone => {
FileSize(torrent.left_until_done.unwrap_or(0)).to_string()
}
TorrentGetField::MetadataPercentComplete => {
optional_to_string(torrent.metadata_percent_complete)
}
@@ -120,13 +129,17 @@ impl Wrapper for TorrentGetField {
None => String::from("N/A"),
},
TorrentGetField::QueuePosition => String::from("N/A"),
TorrentGetField::RateDownload => optional_to_string(torrent.rate_download),
TorrentGetField::RateUpload => optional_to_string(torrent.rate_upload),
TorrentGetField::RateDownload => {
NetSpeed(torrent.rate_download.unwrap_or(0)).to_string()
}
TorrentGetField::RateUpload => NetSpeed(torrent.rate_upload.unwrap_or(0)).to_string(),
TorrentGetField::RecheckProgress => optional_to_string(torrent.recheck_progress),
TorrentGetField::SecondsSeeding => optional_to_string(torrent.seconds_seeding),
TorrentGetField::SeedRatioLimit => optional_to_string(torrent.seed_ratio_limit),
TorrentGetField::SeedRatioMode => String::from("N/A"),
TorrentGetField::SizeWhenDone => optional_to_string(torrent.size_when_done),
TorrentGetField::SizeWhenDone => {
FileSize(torrent.size_when_done.unwrap_or(0)).to_string()
}
TorrentGetField::Status => match torrent.status {
Some(status) => match status {
TorrentStatus::Stopped => String::from("Stopped"),
@@ -140,7 +153,7 @@ impl Wrapper for TorrentGetField {
None => String::from("N/A"),
},
TorrentGetField::TorrentFile => optional_to_string(torrent.torrent_file),
TorrentGetField::TotalSize => optional_to_string(torrent.total_size),
TorrentGetField::TotalSize => FileSize(torrent.total_size.unwrap_or(0)).to_string(),
TorrentGetField::Trackers => match torrent.trackers {
Some(trackers) => trackers.iter().map(|x| x.announce.to_string()).collect(),
None => String::from("N/A"),
@@ -149,7 +162,9 @@ impl Wrapper for TorrentGetField {
Some(upload_ratio) => format!("{:.2}", upload_ratio),
None => String::from("N/A"),
},
TorrentGetField::UploadedEver => optional_to_string(torrent.uploaded_ever),
TorrentGetField::UploadedEver => {
FileSize(torrent.uploaded_ever.unwrap_or(0)).to_string()
}
TorrentGetField::Wanted => match torrent.wanted {
Some(wanted) => wanted.iter().map(|x| x.to_string()).collect(),
None => String::from("N/A"),
@@ -205,8 +220,5 @@ impl Wrapper for TorrentGetField {
}
fn optional_to_string<T: ToString>(option: Option<T>) -> String {
match option {
Some(val) => val.to_string(),
None => String::from("N/A"),
}
option.map_or_else(|| "N/A".into(), |val| val.to_string())
}

30
src/app/utils/netspeed.rs Normal file
View File

@@ -0,0 +1,30 @@
pub struct NetSpeed(pub i64);
impl NetSpeed {
pub fn to_bps(&self) -> String {
format!("{} bps", self.0)
}
pub fn to_kbps(&self) -> String {
format!("{:.2} kbps", self.0 as f64 / 1e3)
}
pub fn to_mbps(&self) -> String {
format!("{:.2} mbps", self.0 as f64 / 1e6)
}
pub fn to_gbps(&self) -> String {
format!("{:.2} gbps", self.0 as f64 / 1e9)
}
}
impl ToString for NetSpeed {
fn to_string(&self) -> String {
match self.0 as f64 {
b if b >= 1e9 => self.to_gbps(),
b if b >= 1e6 => self.to_mbps(),
b if b >= 1e3 => self.to_kbps(),
_ => self.to_bps(),
}
}
}

View File

@@ -1,5 +1,4 @@
use ratatui::{
backend::Backend,
layout::Alignment,
prelude::{Constraint, Direction, Layout},
style::{Color, Style},
@@ -8,13 +7,9 @@ use ratatui::{
Frame,
};
use crate::app::{App, Tab};
use crate::app::{utils::Wrapper, App, Tab};
use self::utils::Wrapper;
mod utils;
fn render_table<'a>(app: &mut App, tab: Tab) -> (Table<'a>, Vec<Constraint>) {
fn render_table<'a>(app: &mut App, tab: Tab) -> Table<'a> {
let fields = tab.fields();
let torrents = app.torrents.set_fields(None).torrents();
@@ -33,7 +28,7 @@ fn render_table<'a>(app: &mut App, tab: Tab) -> (Table<'a>, Vec<Constraint>) {
let widths = fields
.iter()
.map(|&field| Constraint::Length(field.width()))
.collect();
.collect::<Vec<_>>();
let header = Row::new(
fields
@@ -42,23 +37,20 @@ fn render_table<'a>(app: &mut App, tab: Tab) -> (Table<'a>, Vec<Constraint>) {
.collect::<Vec<_>>(),
)
.style(Style::default().fg(Color::Yellow));
(
Table::new(rows)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded),
)
.header(header)
.highlight_style(Style::default().fg(Color::Red))
.highlight_symbol(">> ")
.column_spacing(1),
widths,
)
Table::new(rows, widths)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded),
)
.header(header)
.highlight_style(Style::default().fg(Color::Red))
.highlight_symbol(">> ")
.column_spacing(1)
}
/// Renders the user interface widgets.
pub fn render<'a, B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
pub fn render(app: &mut App, frame: &mut Frame) {
// This is where you add new widgets.
// See the following resources:
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
@@ -88,12 +80,13 @@ pub fn render<'a, B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
.highlight_style(Style::default().fg(Color::Green))
.divider("|");
frame.render_widget(tabs, chunks[0]);
let (inner, widths) = match app.index() {
frame.render_widget(tabs, chunks[0]); // renders tab
let table = match app.index() {
0 => render_table(app, Tab::All),
1 => render_table(app, Tab::Active),
2 => render_table(app, Tab::Downloading),
_ => unreachable!(),
};
frame.render_stateful_widget(inner.widths(widths.as_ref()), chunks[1], &mut app.state)
frame.render_stateful_widget(table, chunks[1], &mut app.state) // renders table
}