Skip to main content

pbox_core/
ui.rs

1use crate::ClusterResource;
2use serde::Serialize;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ColorMode {
6    Auto,
7    Always,
8    Never,
9}
10
11impl ColorMode {
12    pub fn enabled(self, stdout_is_tty: bool, no_color: bool, json: bool) -> bool {
13        if json || no_color {
14            return false;
15        }
16        match self {
17            Self::Auto => stdout_is_tty,
18            Self::Always => true,
19            Self::Never => false,
20        }
21    }
22}
23
24pub fn json<T: Serialize>(value: &T) -> Result<String, serde_json::Error> {
25    serde_json::to_string_pretty(value)
26}
27
28pub fn format_resources(resources: &[ClusterResource], colour: bool) -> String {
29    let mut output = String::from("ID             State      Node\n");
30    for resource in resources {
31        let state = resource.status.as_deref().unwrap_or("unknown");
32        let node = resource.node.as_deref().unwrap_or("-");
33        let name = resource.name.as_deref().unwrap_or("-");
34        let label = if colour {
35            format!("\x1b[1;36m{name:<14}\x1b[0m")
36        } else {
37            format!("{name:<14}")
38        };
39        output.push_str(&format!("{label} {state:<10} {node}\n"));
40    }
41    if resources.is_empty() {
42        output.push_str("No pbox-managed containers found.\n");
43    }
44    output
45}