cubic 0.17.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::view::Console;

pub enum Alignment {
    Left,
    Right,
}

#[derive(Default)]
pub struct Row {
    pub entries: Vec<(String, Alignment)>,
}

impl Row {
    pub fn add(&mut self, entry: &str, alignment: Alignment) -> &mut Self {
        self.entries.push((entry.to_string(), alignment));
        self
    }
}

#[derive(Default)]
pub struct TableView {
    rows: Vec<Row>,
}

impl TableView {
    pub fn new() -> Self {
        TableView::default()
    }

    pub fn add_row(&mut self) -> &mut Row {
        let row = Row::default();
        self.rows.push(row);
        self.rows.last_mut().unwrap()
    }

    pub fn print(&self, console: &mut dyn Console) {
        let mut column_size = Vec::new();
        for row in &self.rows {
            for (index, (entry, _)) in row.entries.iter().enumerate() {
                while index >= column_size.len() {
                    column_size.push(0);
                }

                column_size[index] = column_size[index].max(entry.len());
            }
        }

        for row in &self.rows {
            let line = row
                .entries
                .iter()
                .enumerate()
                .map(|(index, (entry, alignment))| match alignment {
                    Alignment::Left => format!("{entry:<width$}", width = column_size[index]),
                    Alignment::Right => format!("{entry:>width$}", width = column_size[index]),
                })
                .collect::<Vec<_>>()
                .join("   ");
            console.info(line.trim_end());
        }
    }
}