From 474eea16044bd873861869e208088f5e16eb3c6a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 9 Jan 2024 06:37:44 +0000 Subject: [PATCH] Remove most uses of ColouredString::slice(). The new flexibility means we don't have to use an explicit method call to turn a ColouredString into a ColouredStringSlice before passing it to ColouredString::push_str(). Also, in tui.rs, ratatui_set_string demonstrates that functions outside the coloured_string module can also be given the flexibility to take either an owned ColouredString or a slice, by taking an argument 'impl ColouredStringCommon'. However, in FileStatusLineFinal::render_highlighted(), where I pass ColouredStringSlices to a closure, I _couldn't_ do that - closures are apparently more restricted than ordinary functions in that way. So that's the one place where I still had to explicitly call .slice(). --- src/editor.rs | 12 ++++++------ src/html.rs | 2 +- src/text.rs | 27 +++++++++++++-------------- src/tui.rs | 6 +++--- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index ebb0e02..1ed6e04 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -462,7 +462,7 @@ impl SingleLineEditor { pub fn draw(&self, width: usize) -> (ColouredString, Option) { let mut s = self.prompt.clone(); if self.first_visible > 0 { - s.push_str(&ColouredString::uniform("<", '>').slice()); + s.push_str(ColouredString::uniform("<", '>')); } let mut pos = self.first_visible; let mut cursor = None; @@ -478,11 +478,11 @@ impl SingleLineEditor { (width_so_far + w == width && pos + b < self.core.text.len()) { - s.push_str(&ColouredString::uniform(">", '>').slice()); + s.push_str(ColouredString::uniform(">", '>')); break; } else { - s.push_str(&ColouredString::plain( - &self.core.text[pos..pos+b]).slice()); + s.push_str(ColouredString::plain( + &self.core.text[pos..pos+b])); pos += b; } } @@ -1023,8 +1023,8 @@ impl Composer { } let start = max(start_pos, region.start); let end = min(end_pos, region.end); - cs.push_str(&ColouredString::uniform( - &self.core.text[start..end], region.colour).slice()); + cs.push_str(ColouredString::uniform( + &self.core.text[start..end], region.colour)); } Some(cs) diff --git a/src/html.rs b/src/html.rs index 83faca1..8119c97 100644 --- a/src/html.rs +++ b/src/html.rs @@ -210,7 +210,7 @@ fn to_coloured_string(tl: &TaggedLine>) -> ColouredString { Some(c) => *c, None => ' ', }; - cs.push_str(&ColouredString::uniform(&ts.s, c).slice()); + cs.push_str(ColouredString::uniform(&ts.s, c)); } } cs diff --git a/src/text.rs b/src/text.rs index f95a246..8f60dba 100644 --- a/src/text.rs +++ b/src/text.rs @@ -578,7 +578,7 @@ impl TextFragment for Paragraph { let mut line = ColouredString::plain(" ") .repeat(curr_indent); for i in start_pos..break_pos { - line.push_str(&self.words[i].slice()); + line.push_str(&self.words[i]); } lines.push(line); start_pos = break_pos; @@ -597,7 +597,7 @@ impl TextFragment for Paragraph { let mut line = ColouredString::plain(" ").repeat(curr_indent); for i in start_pos..break_pos { - line.push_str(&self.words[i].slice()); + line.push_str(&self.words[i]); } lines.push(line); @@ -1467,14 +1467,13 @@ impl TextFragment for FileStatusLineFinal { let space = ColouredString::plain(" ").repeat(FileStatusLine::SPACING); let push = |line: &mut ColouredString, s: ColouredStringSlice<'_>| { if !line.is_empty() { - line.push_str(&space.slice()); + line.push_str(&space); } line.push_str(&s); }; if let Some(msg) = &self.fs.message { - let cmsg = ColouredString::plain(msg); - push(&mut line, cmsg.slice()); + push(&mut line, ColouredString::plain(msg).slice()); } let cprop = self.fs.proportion.as_ref() @@ -1499,9 +1498,9 @@ impl TextFragment for FileStatusLineFinal { let mut ckey = ColouredString::plain("["); let ckp = ColouredString::uniform( &key_to_string(kp.key), 'k'); - ckey.push_str(&ckp.slice()); - ckey.push_str(&ColouredString::plain("]:").slice()); - ckey.push_str(&kp.description.slice()); + ckey.push_str(&ckp); + ckey.push_str(ColouredString::plain("]:")); + ckey.push_str(&kp.description); push(&mut line, ckey.slice()); } @@ -1511,7 +1510,7 @@ impl TextFragment for FileStatusLineFinal { if let Some(cprop) = cprop { push(&mut line, cprop.slice()); } else { - line.push_str(&ColouredString::plain(".").slice()); + line.push_str(ColouredString::plain(".")); } // Done. Now centre it in the available space. @@ -1828,9 +1827,9 @@ impl StatusDisplay { let mut desc = ColouredString::uniform("Poll: ", 'H'); for (i, extra) in extras.iter().enumerate() { if i > 0 { - desc.push_str(&ColouredString::uniform(", ", 'H').slice()); + desc.push_str(ColouredString::uniform(", ", 'H')); } - desc.push_str(&extra.slice()); + desc.push_str(extra); } let title = Paragraph::new().set_indent(0, 2).add(&desc); @@ -1841,10 +1840,10 @@ impl StatusDisplay { votes.iter().any(|i| *i == thisindex) }); let mut desc = ColouredString::plain(" "); - desc.push_str(&ColouredString::plain(&opt.title).slice()); + desc.push_str(ColouredString::plain(&opt.title)); if let Some(n) = opt.votes_count { - desc.push_str(&ColouredString::uniform( - &format!(" ({})", n), 'H').slice()); + desc.push_str(ColouredString::uniform( + &format!(" ({})", n), 'H')); } options.push((voted, desc)); } diff --git a/src/tui.rs b/src/tui.rs index 60c8e20..a79c7fd 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -116,7 +116,7 @@ fn ratatui_style_from_colour(colour: char) -> Style { } fn ratatui_set_string(buf: &mut Buffer, x: usize, y: usize, - text: &ColouredStringSlice<'_>) { + text: impl ColouredStringCommon) { let mut x = x; if let Ok(y) = y.try_into() { for (frag, colour) in text.frags() { @@ -553,7 +553,7 @@ impl TuiLogicalState { if y >= h { break; } - ratatui_set_string(buf, 0, y, &line.slice()); + ratatui_set_string(buf, 0, y, line); last_y = y; last_x = line.width(); } @@ -576,7 +576,7 @@ impl TuiLogicalState { for x in 0..area.width { buf.get_mut(x, y as u16).reset(); } - ratatui_set_string(buf, 0, y, &line.slice()); + ratatui_set_string(buf, 0, y, line); last_y = y; last_x = line.width(); } -- 2.30.2