mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 17:15:28 +00:00
implement format_bytes as usize trait
This commit is contained in:
parent
8ea11a215a
commit
6f814dd382
2 changed files with 23 additions and 20 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
use super::{Feature, Subject};
|
use super::{Feature, Subject};
|
||||||
use crate::tool::format_bytes;
|
|
||||||
use ggemini::client::{
|
use ggemini::client::{
|
||||||
connection::response::{data::Text, meta::Status},
|
connection::response::{data::Text, meta::Status},
|
||||||
Client, Request,
|
Client, Request,
|
||||||
|
|
@ -217,6 +216,7 @@ fn handle(
|
||||||
Some(&cancellable),
|
Some(&cancellable),
|
||||||
) {
|
) {
|
||||||
Ok(file_output_stream) => {
|
Ok(file_output_stream) => {
|
||||||
|
use crate::tool::Format;
|
||||||
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
|
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
|
||||||
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
|
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
|
||||||
// show bytes count in loading widget, validate max size for incoming data
|
// show bytes count in loading widget, validate max size for incoming data
|
||||||
|
|
@ -237,7 +237,7 @@ fn handle(
|
||||||
let action = action.clone();
|
let action = action.clone();
|
||||||
move |_, total| action.update.activate(&format!(
|
move |_, total| action.update.activate(&format!(
|
||||||
"Received {}...",
|
"Received {}...",
|
||||||
format_bytes(total)
|
total.bytes()
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
// on complete
|
// on complete
|
||||||
|
|
@ -248,7 +248,7 @@ fn handle(
|
||||||
action.complete.activate(&format!(
|
action.complete.activate(&format!(
|
||||||
"Saved to {} ({} total)",
|
"Saved to {} ({} total)",
|
||||||
file.parse_name(),
|
file.parse_name(),
|
||||||
format_bytes(total)
|
total.bytes()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
Err(e) => action.cancel.activate(&e.to_string())
|
Err(e) => action.cancel.activate(&e.to_string())
|
||||||
|
|
|
||||||
37
src/tool.rs
37
src/tool.rs
|
|
@ -1,23 +1,26 @@
|
||||||
//! Some shared helpers collection
|
//! Some shared helpers collection
|
||||||
|
|
||||||
/// Format bytes to KB/MB/GB presentation
|
pub trait Format {
|
||||||
pub fn format_bytes(value: usize) -> String {
|
/// Format bytes to KB/MB/GB presentation
|
||||||
const KB: f32 = 1024.0;
|
fn bytes(&self) -> String;
|
||||||
const MB: f32 = KB * KB;
|
}
|
||||||
const GB: f32 = MB * KB;
|
|
||||||
|
|
||||||
let f = value as f32;
|
impl Format for usize {
|
||||||
|
fn bytes(&self) -> String {
|
||||||
|
const KB: f32 = 1024.0;
|
||||||
|
const MB: f32 = KB * KB;
|
||||||
|
const GB: f32 = MB * KB;
|
||||||
|
|
||||||
if f < KB {
|
let f = *self as f32;
|
||||||
format!(
|
|
||||||
"{value} {}",
|
if f < KB {
|
||||||
plurify::ns(value, &["byte", "bytes", "bytes"])
|
format!("{self} {}", plurify::ns(*self, &["byte", "bytes", "bytes"]))
|
||||||
)
|
} else if f < MB {
|
||||||
} else if f < MB {
|
format!("{:.2} KB", f / KB)
|
||||||
format!("{:.2} KB", f / KB)
|
} else if f < GB {
|
||||||
} else if f < GB {
|
format!("{:.2} MB", f / MB)
|
||||||
format!("{:.2} MB", f / MB)
|
} else {
|
||||||
} else {
|
format!("{:.2} GB", f / GB)
|
||||||
format!("{:.2} GB", f / GB)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue