1 use unicode_width::UnicodeWidthChar;
4 fn center(&self, term_width: usize) -> Self;
7 impl Center for String {
8 fn center(&self, term_width: usize) -> Self {
9 let mut result = String::new();
10 let mut buffer = String::new();
11 let mut buffer_width = 0;
13 let max = self.chars().count() - 1;
15 for (i, c) in self.chars().enumerate() {
16 buffer_width += UnicodeWidthChar::width(c)
21 if i == max || buffer_width >= term_width - 12 {
22 result.push_str(&String::from(" ")
23 .repeat((term_width - buffer_width) / 2));
24 result.push_str(&buffer);