Apply this alignment to a string within a given width. # Arguments `text` - The text to align `width` - The total width to fill # Returns A string padded to the specified width according to this alignment. # Example ```rust,ignore let aligned = Alignment::Right.apply("42", 10); assert_eq!(aligned, " 42"); ```
(&self, text: &str, width: usize)
| 80 | /// assert_eq!(aligned, " 42"); |
| 81 | /// ``` |
| 82 | pub fn apply(&self, text: &str, width: usize) -> String { |
| 83 | let text_width = console::measure_text_width(text); |
| 84 | if text_width >= width { |
| 85 | return text.to_string(); |
| 86 | } |
| 87 | |
| 88 | let padding = width - text_width; |
| 89 | match self { |
| 90 | Alignment::Left => format!("{}{}", text, " ".repeat(padding)), |
| 91 | Alignment::Right => format!("{}{}", " ".repeat(padding), text), |
| 92 | Alignment::Center => { |
| 93 | let left_pad = padding / 2; |
| 94 | let right_pad = padding - left_pad; |
| 95 | format!("{}{}{}", " ".repeat(left_pad), text, " ".repeat(right_pad)) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Column Configuration |
no outgoing calls