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

114
Cargo.lock generated
View File

@ -2,6 +2,24 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "allocator-api2"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
[[package]]
name = "anyhow"
version = "1.0.75"
@ -122,7 +140,7 @@ checksum = "eecf8589574ce9b895052fa12d69af7a233f99e6107f5cb8dd1044f2a17bfdcb"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
]
[[package]]
@ -163,7 +181,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
]
[[package]]
@ -217,6 +235,16 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]]
name = "heck"
version = "0.4.1"
@ -318,7 +346,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
@ -335,9 +363,9 @@ checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
[[package]]
name = "itertools"
version = "0.11.0"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
@ -382,6 +410,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "lru"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db2c024b41519440580066ba82aab04092b333e09066a5eb86c7c4890df31f22"
dependencies = [
"hashbrown 0.14.3",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -483,16 +520,18 @@ dependencies = [
[[package]]
name = "ratatui"
version = "0.23.0"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e2e4cd95294a85c3b4446e63ef054eea43e0205b1fd60120c16b74ff7ff96ad"
checksum = "a5659e52e4ba6e07b2dad9f1158f578ef84a73762625ddb51536019f34d180eb"
dependencies = [
"bitflags 2.4.0",
"cassowary",
"crossterm",
"indoc",
"itertools",
"lru",
"paste",
"stability",
"strum",
"unicode-segmentation",
"unicode-width",
@ -637,7 +676,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
]
[[package]]
@ -659,7 +698,7 @@ checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
]
[[package]]
@ -735,6 +774,16 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "stability"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebd1b177894da2a2d9120208c3386066af06a488255caabc5de8ddca22dbc3ce"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "strum"
version = "0.25.0"
@ -754,7 +803,18 @@ dependencies = [
"proc-macro2",
"quote",
"rustversion",
"syn",
"syn 2.0.31",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
@ -810,7 +870,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
]
[[package]]
@ -878,7 +938,7 @@ dependencies = [
[[package]]
name = "traxor"
version = "0.0.1"
version = "0.1.0"
dependencies = [
"anyhow",
"crossterm",
@ -944,6 +1004,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "want"
version = "0.3.1"
@ -980,7 +1046,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
"wasm-bindgen-shared",
]
@ -1014,7 +1080,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.31",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1195,3 +1261,23 @@ dependencies = [
"cfg-if",
"windows-sys 0.48.0",
]
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
]

View File

@ -1,14 +1,14 @@
[package]
name = "traxor"
version = "0.0.1"
version = "0.1.0"
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
license = "GPLv3"
license = "GPL3"
edition = "2021"
[dependencies]
crossterm = "0.27"
ratatui = { version = "0.23" }
ratatui = { version = "0.25" }
transmission-rpc = "0.4"
# transmission-rpc = { path = "/home/kristofers/repos/transmission-rpc" }
url = "2.4"

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
}