mirror of
https://github.com/kristoferssolo/traxor.git
synced 2026-02-04 06:42:04 +00:00
test(app): add basic tab tests
This commit is contained in:
@@ -1,31 +1,24 @@
|
||||
use std::fmt;
|
||||
|
||||
const KB: f64 = 1e3;
|
||||
const MB: f64 = 1e6;
|
||||
const GB: f64 = 1e9;
|
||||
const TB: f64 = 1e12;
|
||||
pub struct FileSize(i64);
|
||||
|
||||
pub struct FileSize(pub i64);
|
||||
impl From<i64> for FileSize {
|
||||
fn from(bytes: i64) -> Self {
|
||||
FileSize(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FileSize {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.0 == 0 {
|
||||
return write!(f, "0");
|
||||
let bytes = self.0;
|
||||
if bytes < 1024 {
|
||||
write!(f, "{} B", bytes)
|
||||
} else if bytes < 1024 * 1024 {
|
||||
write!(f, "{:.2} KB", bytes as f64 / 1024.0)
|
||||
} else if bytes < 1024 * 1024 * 1024 {
|
||||
write!(f, "{:.2} MB", bytes as f64 / (1024.0 * 1024.0))
|
||||
} else {
|
||||
write!(f, "{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
|
||||
}
|
||||
let size = self.0 as f64;
|
||||
let (value, unit) = match size {
|
||||
s if s >= TB => (s / TB, "TB"),
|
||||
s if s >= GB => (s / GB, "GB"),
|
||||
s if s >= MB => (s / MB, "MB"),
|
||||
s if s >= KB => (s / KB, "KB"),
|
||||
_ => (size, "B"),
|
||||
};
|
||||
write!(f, "{:.2} {}", value, unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for FileSize {
|
||||
fn from(size: i64) -> Self {
|
||||
FileSize(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus};
|
||||
|
||||
mod filesize;
|
||||
mod netspeed;
|
||||
pub mod filesize;
|
||||
pub mod netspeed;
|
||||
|
||||
use crate::app::utils::filesize::FileSize;
|
||||
use crate::app::utils::netspeed::NetSpeed;
|
||||
|
||||
@@ -1,35 +1,24 @@
|
||||
use std::fmt;
|
||||
|
||||
const KBPS: f64 = 1_000.0;
|
||||
const MBPS: f64 = 1_000_000.0;
|
||||
const GBPS: f64 = 1_000_000_000.0;
|
||||
pub struct NetSpeed(i64);
|
||||
|
||||
pub struct NetSpeed(pub i64);
|
||||
impl From<i64> for NetSpeed {
|
||||
fn from(bytes_per_second: i64) -> Self {
|
||||
NetSpeed(bytes_per_second)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for NetSpeed {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let speed = self.0 as f64;
|
||||
if speed == 0.0 {
|
||||
return write!(f, "0 bps");
|
||||
}
|
||||
|
||||
let (value, unit) = match speed {
|
||||
s if s >= GBPS => (s / GBPS, "Gbps"),
|
||||
s if s >= MBPS => (s / MBPS, "Mbps"),
|
||||
s if s >= KBPS => (s / KBPS, "kbps"),
|
||||
_ => (speed, "bps"),
|
||||
};
|
||||
|
||||
if unit == "bps" {
|
||||
write!(f, "{:.0} {}", value, unit)
|
||||
let bytes_per_second = self.0;
|
||||
if bytes_per_second < 1024 {
|
||||
write!(f, "{} B/s", bytes_per_second)
|
||||
} else if bytes_per_second < 1024 * 1024 {
|
||||
write!(f, "{:.2} KB/s", bytes_per_second as f64 / 1024.0)
|
||||
} else if bytes_per_second < 1024 * 1024 * 1024 {
|
||||
write!(f, "{:.2} MB/s", bytes_per_second as f64 / (1024.0 * 1024.0))
|
||||
} else {
|
||||
write!(f, "{:.2} {}", value, unit)
|
||||
write!(f, "{:.2} GB/s", bytes_per_second as f64 / (1024.0 * 1024.0 * 1024.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for NetSpeed {
|
||||
fn from(speed: i64) -> Self {
|
||||
NetSpeed(speed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,3 +12,5 @@ pub mod tui;
|
||||
|
||||
/// Event handler.
|
||||
pub mod handler;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user