MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / apply

Method apply

atomic-cli/src/output/table.rs:82–98  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 7

renderMethod · 0.80
test_alignment_leftFunction · 0.80
test_alignment_rightFunction · 0.80
test_alignment_centerFunction · 0.80

Calls

no outgoing calls

Tested by 6

test_alignment_leftFunction · 0.64
test_alignment_rightFunction · 0.64
test_alignment_centerFunction · 0.64