diff --git a/runcc/src/label.rs b/runcc/src/label.rs index cc36c0d..b5781a2 100644 --- a/runcc/src/label.rs +++ b/runcc/src/label.rs @@ -1,15 +1,37 @@ -pub fn display_label(label: &str, max_label_length: usize) -> String { - let len = label.len(); - if len > max_label_length { - let len_trim = std::cmp::min(len - max_label_length, 3); - - let label = &label[0..(max_label_length - len_trim)]; - let padding = ".".repeat(len_trim); - format!("{}{}", label, padding) - } else if len < max_label_length { - let padding = " ".repeat(max_label_length - len); - format!("{}{}", label, padding) - } else { - label.to_string() +#[derive(Debug, Clone)] +pub struct Label { + label: String, + display: Option, +} + +impl Label { + pub fn new(label: String, display: Option) -> Self { + Self { label, display } + } + + pub fn label(&self) -> &str { + &self.label + } + + pub fn display(&self) -> &str { + self.display.as_ref().unwrap_or(&self.label) + } + + pub fn from_label(label: String, max_label_length: usize) -> Self { + let len = label.len(); + let display = if len > max_label_length { + let len_trim = std::cmp::min(len - max_label_length, 3); + + let label = &label[0..(max_label_length - len_trim)]; + let padding = ".".repeat(len_trim); + Some(format!("{}{}", label, padding)) + } else if len < max_label_length { + let padding = " ".repeat(max_label_length - len); + Some(format!("{}{}", label, padding)) + } else { + None + }; + + Self::new(label, display) } }