diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-17 15:47:33 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-17 15:47:33 +0100 |
| commit | 6a2aed6d816c524ee6529df2ae55bbe40ced0341 (patch) | |
| tree | 06aec43964593a883c18b69bdfd8bc4a7eafa201 /src | |
| parent | chore: move from to_string to to_owned (diff) | |
| download | sl0pstatus-6a2aed6d816c524ee6529df2ae55bbe40ced0341.tar.gz sl0pstatus-6a2aed6d816c524ee6529df2ae55bbe40ced0341.tar.xz sl0pstatus-6a2aed6d816c524ee6529df2ae55bbe40ced0341.zip | |
feat: add bench for render_bar
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 54 | ||||
| -rw-r--r-- | src/render.rs | 17 |
2 files changed, 36 insertions, 35 deletions
diff --git a/src/config.rs b/src/config.rs index 584898b..8591788 100644 --- a/src/config.rs +++ b/src/config.rs @@ -88,16 +88,10 @@ impl ModuleConfig { let widget_string_left = self .left_parts .iter() - .map(|p| p.content.clone()) - .fold(String::new(), |a, b| a + b.as_str()); + .fold(String::new(), |a, b| a + &b.content); - let left_len = self.process_widget_click( - click_pos, - widget_string_left, - widget_map.clone(), - &state, - 0, - ); + let left_len = + self.process_widget_click(click_pos, widget_string_left, widget_map.clone(), &state, 0); if click_pos <= left_len { return; @@ -112,16 +106,15 @@ impl ModuleConfig { }); let widget_spacer = strip_ansi_escapes::strip_str(self.get_spacer( - " ".repeat(left_len), - output_right, + &" ".repeat(left_len), + &output_right, state.cols, )); let widget_string_right = self .right_parts .iter() - .map(|p| p.content.clone()) - .fold(String::new(), |a, b| a + b.as_str()); + .fold(String::new(), |a, b| a + &b.content); self.process_widget_click( click_pos, @@ -191,27 +184,38 @@ impl ModuleConfig { ) }); - let mut border_top = "".to_owned(); - if self.border.enabled && self.border.position == BorderPosition::Top { - border_top = format!("{}\n", self.border.draw(state.cols)); - } + if self.border.enabled { + let mut border_top = "".to_owned(); + if self.border.enabled && self.border.position == BorderPosition::Top { + border_top = format!("{}\n", self.border.draw(state.cols)); + } + + let mut border_bottom = "".to_owned(); + if self.border.enabled && self.border.position == BorderPosition::Bottom { + border_bottom = format!("\n{}", self.border.draw(state.cols)); + } - let mut border_bottom = "".to_owned(); - if self.border.enabled && self.border.position == BorderPosition::Bottom { - border_bottom = format!("\n{}", self.border.draw(state.cols)); + print!( + "{}{}{}{}{}", + border_top, + output_left, + self.get_spacer(&output_left, &output_right, state.cols), + output_right, + border_bottom, + ); + + return; } print!( - "{}{}{}{}{}", - border_top, + "{}{}{}", output_left, - self.get_spacer(output_left.clone(), output_right.clone(), state.cols), + self.get_spacer(&output_left, &output_right, state.cols), output_right, - border_bottom, ); } - fn get_spacer(&self, output_left: String, output_right: String, cols: usize) -> String { + fn get_spacer(&self, output_left: &str, output_right: &str, cols: usize) -> String { let text_count = strip_ansi_escapes::strip_str(output_left).chars().count() + strip_ansi_escapes::strip_str(output_right).chars().count(); diff --git a/src/render.rs b/src/render.rs index 4c2c74f..f4cc14f 100644 --- a/src/render.rs +++ b/src/render.rs @@ -36,15 +36,14 @@ impl FormattedPart { let mut result = FormattedPart::default(); - let format_content_split = format.split(']'); + let mut format_content_split = format.split(']').collect::<Vec<&str>>(); - if format_content_split.clone().count() == 1 { + if format_content_split.len() == 1 { result.content = format; return result; } - let mut format_content_split = format_content_split.collect::<Vec<&str>>(); let parts = format_content_split[0].split(','); format_content_split.remove(0); @@ -103,17 +102,15 @@ impl FormattedPart { for widget in WIDGET_REGEX.captures_iter(&self.content) { let match_name = widget.get(0).unwrap().as_str(); - let mut widget_key = match_name.trim_matches(|c| c == '{' || c == '}'); + let widget_key = match_name.trim_matches(|c| c == '{' || c == '}'); + let mut widget_key_name = widget_key; if widget_key.starts_with("command_") { - widget_key = "command"; + widget_key_name = "command"; } - let result = match widgets.get(widget_key) { - Some(widget) => widget.process( - match_name.trim_matches(|c| c == '{' || c == '}'), - state, - ), + let result = match widgets.get(widget_key_name) { + Some(widget) => widget.process(widget_key, state), None => "Use of uninitialized widget".to_owned(), }; |
