Skip to content

Commit

Permalink
feat(lib): label
Browse files Browse the repository at this point in the history
  • Loading branch information
EqualMa committed Sep 8, 2021
1 parent a338c9d commit 9d1ed9e
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions runcc/src/label.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

impl Label {
pub fn new(label: String, display: Option<String>) -> 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)
}
}

0 comments on commit 9d1ed9e

Please sign in to comment.