1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use std::fmt::Debug; pub trait Format: Debug { fn left(&self, hold: usize) -> String; fn right(&self, hold: usize) -> String; fn mid(&self, hold: usize) -> String; fn left_lim(&self, hold: usize) -> String; fn right_lim(&self, hold: usize) -> String; fn mid_lim(&self, hold: usize) -> String; fn cut(&self, hold: usize) -> String; } pub fn hold_spaces(num: usize) -> String { let mut result = "".to_string(); for _ in 0..num { result += " "; } result } impl Format for String { fn left(&self, hold: usize) -> String { if self.len() > hold { return format!("{}", self); } format!("{}{}", self, hold_spaces(hold-self.len())) } fn right(&self, hold: usize) -> String { if self.len() > hold { return format!("{}", self); } format!("{}{}", hold_spaces(hold-self.len()), self) } fn mid(&self, hold: usize) -> String { let temp = hold-self.len(); let half = temp/2; format!("{}{}{}", hold_spaces(temp - half), self, hold_spaces(temp)) } fn left_lim(&self, hold: usize) -> String { if self.len() > hold { return self.cut(hold-3)+"..."; } self.left(hold) } fn right_lim(&self, hold: usize) -> String { if self.len() > hold { return self.cut(hold-3)+"..."; } self.right(hold) } fn mid_lim(&self, hold: usize) -> String { if self.len() > hold { return self.cut(hold-3)+"..."; } self.mid(hold) } fn cut(&self, hold: usize) -> String { if self.len() <= hold { return format!("{}", self); } let mut c = self.chars(); let mut result = "".to_string(); for _ in 0..hold { if let Some(x) = c.next() { result.push(x); } } result } }