-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |