cubic 0.14.0

Cubic is a lightweight command line manager for virtual machines. It has a simple, daemon-less and rootless design. All Cubic virtual machines run isolated in the user context. Cubic is built on top of QEMU, KVM and cloud-init. Show all supported images: $ cubic images Create a new virtual machine instance: $ cubic create mymachine --image ubuntu:noble List all virtual machine instances: $ cubic instances Start an instance: $ cubic start <instance name> Stop an instance: $ cubic stop <instance name> Open a shell in the instance: $ cubic ssh <machine name> Copy a file from the host to the instance: $ cubic scp <path/to/host/file> <machine>:<path/to/guest/file> Copy a file from the instance to the hots: $ cubic scp <machine>:<path/to/guest/file> <path/to/host/file>
use crate::util;
use std::io;
use std::io::Write;
use std::time::Instant;

pub struct TransferView {
    message: String,
    start_time: Instant,
    bytes_per_second: u64,
}

impl TransferView {
    pub fn new(message: &str) -> Self {
        TransferView {
            message: message.to_string(),
            start_time: Instant::now(),
            bytes_per_second: 0,
        }
    }

    pub fn update(&mut self, transferred_bytes: u64, total_bytes: Option<u64>) {
        print!(
            "\r{}: {:>10}",
            self.message,
            util::bytes_to_human_readable(transferred_bytes)
        );

        if let Some(total_bytes) = total_bytes {
            print!(
                " / {:>10} [{:>3.0}%]",
                util::bytes_to_human_readable(total_bytes),
                transferred_bytes as f64 / total_bytes as f64 * 100_f64
            );
        }

        let transfer_time_sec = self.start_time.elapsed().as_secs();
        if transfer_time_sec != 0 {
            self.bytes_per_second += transferred_bytes / transfer_time_sec;
            self.bytes_per_second /= 2;
            print!(
                " {:>10}/s",
                util::bytes_to_human_readable(self.bytes_per_second)
            );
        }

        if total_bytes
            .map(|total| transferred_bytes >= total)
            .unwrap_or(false)
        {
            println!();
        }

        io::stdout().flush().ok();
    }
}