feat: 备份时显示速度和已备份总量

This commit is contained in:
2026-11-27 09:56:52 +08:00
parent 44ff5de655
commit 981998b654
4 changed files with 3105 additions and 1 deletions
+56 -1
View File
@@ -49,6 +49,8 @@ struct Device {
status: String,
rate: String,
progress: i32,
bytes_done: u64,
bytes_total: u64,
pending: Option<JobKind>,
queued: bool,
present: bool,
@@ -62,6 +64,7 @@ enum EventMsg {
Chip(String, String),
Disk(String, String),
Progress(String, i32),
Bytes(String, u64, u64),
Status(String, String),
Rate(String, String),
Finished(String, &'static str),
@@ -342,6 +345,14 @@ fn apply_line(line: &str, out: &mut WorkerOut, tx: &mpsc::Sender<EventMsg>, loca
let _ = tx.send(EventMsg::Progress(location.to_string(), n));
}
}
"BYTES" => {
let mut nums = value.split('\t');
if let (Some(done), Some(total)) = (nums.next(), nums.next()) {
if let (Ok(done), Ok(total)) = (done.parse::<u64>(), total.parse::<u64>()) {
let _ = tx.send(EventMsg::Bytes(location.to_string(), done, total));
}
}
}
"STATUS" => {
let _ = tx.send(EventMsg::Status(location.to_string(), value));
}
@@ -587,7 +598,7 @@ fn main() {
table.set_col_resize(true);
table.set_col_resize_min(48);
table.set_type(TableRowSelectMode::Single);
for (index, width) in [46, 110, 110, 210, 100, 250, 70].into_iter().enumerate() {
for (index, width) in [46, 110, 110, 190, 90, 230, 160].into_iter().enumerate() {
table.set_col_width(index as i32, width);
}
table.end();
@@ -868,6 +879,9 @@ fn enqueue(queue: &Arc<Mutex<VecDeque<Job>>>, devices: &Arc<Mutex<Vec<Device>>>,
dev.queued = true;
dev.pending = Some(kind);
dev.progress = 0;
dev.bytes_done = 0;
dev.bytes_total = 0;
dev.rate.clear();
dev.status = job_status(kind).into();
queue.lock().unwrap().push_back(Job { location: location.to_string(), kind, resume: false });
}
@@ -929,6 +943,8 @@ fn apply_found(found: &[(String, String, String, String)], devices: &Arc<Mutex<V
status: "9008".into(),
rate: String::new(),
progress: 0,
bytes_done: 0,
bytes_total: 0,
pending: None,
queued: false,
present: true,
@@ -972,6 +988,9 @@ fn cell_text(devices: &Mutex<Vec<Device>>, row: i32, col: i32) -> String {
4 => dev.disk.clone(),
5 if dev.rate.is_empty() => dev.status.clone(),
5 => format!("{} {}", dev.status, dev.rate),
6 if dev.bytes_total > 0 && dev.status.contains('备') => {
format!("{}/{}", format_amount(dev.bytes_done), format_amount(dev.bytes_total))
}
6 => format!("{}%", dev.progress),
_ => String::new(),
}
@@ -987,6 +1006,27 @@ fn draw_mark(checked: bool, x: i32, y: i32, w: i32, h: i32) {
}
}
fn format_amount(bytes: u64) -> String {
const KB: f64 = 1024.0;
const MB: f64 = 1024.0 * 1024.0;
const GB: f64 = 1024.0 * 1024.0 * 1024.0;
let value = bytes as f64;
let (scaled, unit) = if value >= GB {
(value / GB, "GB")
} else if value >= MB {
(value / MB, "MB")
} else if value >= KB {
(value / KB, "KB")
} else {
return format!("{bytes}B");
};
if scaled >= 100.0 || (scaled - scaled.round()).abs() < 0.05 {
format!("{:.0}{unit}", scaled)
} else {
format!("{:.1}{unit}", scaled)
}
}
fn refresh_table(table: &mut TableRow, devices: &Arc<Mutex<Vec<Device>>>) {
let count = devices.lock().unwrap().len() as i32;
if table.rows() != count {
@@ -1018,10 +1058,21 @@ fn apply(devices: &Arc<Mutex<Vec<Device>>>, msg: EventMsg) {
dev.progress = value;
}
}
EventMsg::Bytes(location, done, total) => {
if let Some(dev) = list.iter_mut().find(|d| d.location == location) {
dev.bytes_done = done;
dev.bytes_total = total;
if total > 0 {
dev.progress = ((done.saturating_mul(100)) / total).min(100) as i32;
}
}
}
EventMsg::Status(location, status) => {
if let Some(dev) = list.iter_mut().find(|d| d.location == location) {
dev.status = status;
dev.rate.clear();
dev.bytes_done = 0;
dev.bytes_total = 0;
}
}
EventMsg::Rate(location, rate) => {
@@ -1034,6 +1085,8 @@ fn apply(devices: &Arc<Mutex<Vec<Device>>>, msg: EventMsg) {
dev.queued = false;
dev.pending = None;
dev.rate.clear();
dev.bytes_done = 0;
dev.bytes_total = 0;
if kind == "info" {
dev.info_done = true;
dev.progress = 0;
@@ -1053,6 +1106,8 @@ fn apply(devices: &Arc<Mutex<Vec<Device>>>, msg: EventMsg) {
dev.queued = false;
dev.pending = None;
dev.rate.clear();
dev.bytes_done = 0;
dev.bytes_total = 0;
dev.info_done = true;
if err == "设备已拔出" {
dev.present = false;