diff options
author | Marcin Puc <5671049+tranzystorek-io@users.noreply.github.com> | 2021-12-07 11:24:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-07 10:24:42 +0000 |
commit | 56e85f87d6c365816cca71c496aa7e49709e0b11 (patch) | |
tree | 1dbc6db878e8da9544df9445d77d1cd665b9126b /zellij-server/src/ui | |
parent | d34e6240101d246f02921cbc909dcd04f648203e (diff) |
fix(style): various internal refactorings
Diffstat (limited to 'zellij-server/src/ui')
-rw-r--r-- | zellij-server/src/ui/boundaries.rs | 7 | ||||
-rw-r--r-- | zellij-server/src/ui/overlay/prompt.rs | 11 | ||||
-rw-r--r-- | zellij-server/src/ui/pane_boundaries_frame.rs | 84 | ||||
-rw-r--r-- | zellij-server/src/ui/pane_contents_and_ui.rs | 6 | ||||
-rw-r--r-- | zellij-server/src/ui/pane_resizer.rs | 10 |
5 files changed, 64 insertions, 54 deletions
diff --git a/zellij-server/src/ui/boundaries.rs b/zellij-server/src/ui/boundaries.rs index 317c2e5dc..d26db14ac 100644 --- a/zellij-server/src/ui/boundaries.rs +++ b/zellij-server/src/ui/boundaries.rs @@ -3,6 +3,7 @@ use zellij_utils::{pane_size::Viewport, zellij_tile}; use crate::tab::Pane; use ansi_term::Colour::{Fixed, RGB}; use std::collections::HashMap; +use std::fmt::Write; use zellij_tile::data::PaletteColor; use zellij_utils::shared::colors; @@ -513,12 +514,14 @@ impl Boundaries { pub fn vte_output(&self) -> String { let mut vte_output = String::new(); for (coordinates, boundary_character) in &self.boundary_characters { - vte_output.push_str(&format!( + write!( + &mut vte_output, "\u{1b}[{};{}H\u{1b}[m{}", coordinates.y + 1, coordinates.x + 1, boundary_character - )); // goto row/col + boundary character + ) + .unwrap(); // goto row/col + boundary character } vte_output } diff --git a/zellij-server/src/ui/overlay/prompt.rs b/zellij-server/src/ui/overlay/prompt.rs index f28e3c405..dc5171f52 100644 --- a/zellij-server/src/ui/overlay/prompt.rs +++ b/zellij-server/src/ui/overlay/prompt.rs @@ -3,6 +3,8 @@ use zellij_utils::pane_size::Size; use super::{Overlay, OverlayType, Overlayable}; use crate::{ClientId, ServerInstruction}; +use std::fmt::Write; + #[derive(Clone, Debug)] pub struct Prompt { pub message: String, @@ -37,7 +39,14 @@ impl Overlayable for Prompt { let mut vte_output = self.message.clone(); Overlay::pad_cols(&mut vte_output, size.cols); for (x, h) in vte_output.chars().enumerate() { - output.push_str(&format!("\u{1b}[{};{}H\u{1b}[48;5;238m{}", rows, x + 1, h,)); + write!( + &mut output, + "\u{1b}[{};{}H\u{1b}[48;5;238m{}", + rows, + x + 1, + h, + ) + .unwrap(); } output } diff --git a/zellij-server/src/ui/pane_boundaries_frame.rs b/zellij-server/src/ui/pane_boundaries_frame.rs index 5ab381582..1ed29d838 100644 --- a/zellij-server/src/ui/pane_boundaries_frame.rs +++ b/zellij-server/src/ui/pane_boundaries_frame.rs @@ -7,29 +7,25 @@ use zellij_utils::zellij_tile::prelude::{Palette, PaletteColor}; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; +use std::fmt::Write; + fn color_string(character: &str, color: Option<PaletteColor>) -> String { match color { - Some(color) => match color { - PaletteColor::Rgb((r, g, b)) => { - format!("{}", RGB(r, g, b).bold().paint(character)) - } - PaletteColor::EightBit(color) => { - format!("{}", Fixed(color).bold().paint(character)) - } - }, - None => format!("{}", Style::new().bold().paint(character)), + Some(PaletteColor::Rgb((r, g, b))) => RGB(r, g, b).bold().paint(character).to_string(), + Some(PaletteColor::EightBit(color)) => Fixed(color).bold().paint(character).to_string(), + None => Style::new().bold().paint(character).to_string(), } } fn background_color(character: &str, color: Option<PaletteColor>) -> String { match color { Some(PaletteColor::Rgb((r, g, b))) => { - format!("{}", Style::new().on(RGB(r, g, b)).paint(character)) + Style::new().on(RGB(r, g, b)).paint(character).to_string() } Some(PaletteColor::EightBit(color)) => { - format!("{}", Style::new().on(Fixed(color)).paint(character)) + Style::new().on(Fixed(color)).paint(character).to_string() } - None => String::from(character), + None => character.to_string(), } } @@ -475,7 +471,7 @@ impl PaneFrame { let mut left_side = self.render_title_left_side(length_of_each_side); let mut right_side = self.render_title_right_side(length_of_each_side); - match (left_side.as_mut(), right_side.as_mut()) { + match (&mut left_side, &mut right_side) { (Some((left_side, left_side_len)), Some((right_side, right_side_len))) => self .three_part_title_line( left_side, @@ -511,23 +507,26 @@ impl PaneFrame { fn render_title(&self, vte_output: &mut String) { let total_title_length = self.geom.cols.saturating_sub(2); // 2 for the left and right corners - if let Some((middle, middle_length)) = self.render_title_middle(total_title_length).as_mut() - { + if let Some((middle, middle_length)) = &self.render_title_middle(total_title_length) { let title_text = self.title_line_with_middle(middle, middle_length); - vte_output.push_str(&format!( + write!( + vte_output, "\u{1b}[{};{}H\u{1b}[m{}", self.geom.y + 1, // +1 because goto is 1 indexed self.geom.x + 1, // +1 because goto is 1 indexed color_string(&title_text, self.color), - )); // goto row/col + boundary character + ) + .unwrap(); // goto row/col + boundary character } else { let title_text = self.title_line_without_middle(); - vte_output.push_str(&format!( + write!( + vte_output, "\u{1b}[{};{}H\u{1b}[m{}", self.geom.y + 1, // +1 because goto is 1 indexed self.geom.x + 1, // +1 because goto is 1 indexed color_string(&title_text, self.color), - )); // goto row/col + boundary character + ) + .unwrap(); // goto row/col + boundary character } } pub fn render(&self) -> String { @@ -539,44 +538,43 @@ impl PaneFrame { } else if row == self.geom.y + self.geom.rows - 1 { // bottom row for col in self.geom.x..(self.geom.x + self.geom.cols) { - if col == self.geom.x { + let boundary = if col == self.geom.x { // bottom left corner - vte_output.push_str(&format!( - "\u{1b}[{};{}H\u{1b}[m{}", - row + 1, // +1 because goto is 1 indexed - col + 1, - color_string(boundary_type::BOTTOM_LEFT, self.color), - )); // goto row/col + boundary character + boundary_type::BOTTOM_LEFT } else if col == self.geom.x + self.geom.cols - 1 { // bottom right corner - vte_output.push_str(&format!( - "\u{1b}[{};{}H\u{1b}[m{}", - row + 1, // +1 because goto is 1 indexed - col + 1, - color_string(boundary_type::BOTTOM_RIGHT, self.color), - )); // goto row/col + boundary character + boundary_type::BOTTOM_RIGHT } else { - vte_output.push_str(&format!( - "\u{1b}[{};{}H\u{1b}[m{}", - row + 1, // +1 because goto is 1 indexed - col + 1, - color_string(boundary_type::HORIZONTAL, self.color), - )); // goto row/col + boundary character - } + boundary_type::HORIZONTAL + }; + + let boundary_rendered = color_string(boundary, self.color); + write!( + &mut vte_output, + "\u{1b}[{};{}H\u{1b}[m{}", + row + 1, + col + 1, + boundary_rendered + ) + .unwrap(); } } else { - vte_output.push_str(&format!( + write!( + &mut vte_output, "\u{1b}[{};{}H\u{1b}[m{}", row + 1, // +1 because goto is 1 indexed self.geom.x + 1, color_string(boundary_type::VERTICAL, self.color), - )); // goto row/col + boundary character - vte_output.push_str(&format!( + ) + .unwrap(); // goto row/col + boundary character + write!( + &mut vte_output, "\u{1b}[{};{}H\u{1b}[m{}", row + 1, // +1 because goto is 1 indexed self.geom.x + self.geom.cols, color_string(boundary_type::VERTICAL, self.color), - )); // goto row/col + boundary character + ) + .unwrap(); // goto row/col + boundary character } } vte_output diff --git a/zellij-server/src/ui/pane_contents_and_ui.rs b/zellij-server/src/ui/pane_contents_and_ui.rs index 4f700f5dc..402724360 100644 --- a/zellij-server/src/ui/pane_contents_and_ui.rs +++ b/zellij-server/src/ui/pane_contents_and_ui.rs @@ -55,14 +55,14 @@ impl<'a> PaneContentsAndUi<'a> { let pane_focused_for_different_client = self .focused_clients .iter() - .filter(|c_id| **c_id != client_id) + .filter(|&&c_id| c_id != client_id) .count() > 0; if pane_focused_for_different_client && !pane_focused_for_client_id { let fake_cursor_client_id = self .focused_clients .iter() - .find(|c_id| **c_id != client_id) + .find(|&&c_id| c_id != client_id) .unwrap(); if let Some(colors) = client_id_to_colors(*fake_cursor_client_id, self.colors) { if let Some(vte_output) = self.pane.render_fake_cursor(colors.0, colors.1) { @@ -84,7 +84,7 @@ impl<'a> PaneContentsAndUi<'a> { let other_focused_clients: Vec<ClientId> = self .focused_clients .iter() - .filter(|c_id| **c_id != client_id) + .filter(|&&c_id| c_id != client_id) .copied() .collect(); let pane_focused_for_differet_client = !other_focused_clients.is_empty(); diff --git a/zellij-server/src/ui/pane_resizer.rs b/zellij-server/src/ui/pane_resizer.rs index 76153597c..53455e3e4 100644 --- a/zellij-server/src/ui/pane_resizer.rs +++ b/zellij-server/src/ui/pane_resizer.rs @@ -30,8 +30,8 @@ struct Span { type Grid = Vec<Vec<Span>>; impl<'a> PaneResizer<'a> { - pub fn new(panes: impl Iterator<Item = (&'a PaneId, &'a mut Box<dyn Pane>)>) -> Self { - let panes: HashMap<_, _> = panes.collect(); + pub fn new(panes: impl IntoIterator<Item = (&'a PaneId, &'a mut Box<dyn Pane>)>) -> Self { + let panes: HashMap<_, _> = panes.into_iter().collect(); let mut vars = HashMap::new(); for &&k in panes.keys() { vars.insert(k, Variable::new()); @@ -84,7 +84,7 @@ impl<'a> PaneResizer<'a> { // Round f64 pane sizes to usize without gaps or overlap let mut finalised = Vec::new(); - for spans in grid.iter_mut() { + for spans in &mut grid { let rounded_size: isize = spans.iter().map(|s| rounded_sizes[&s.size_var]).sum(); let mut error = space as isize - rounded_size; let mut flex_spans: Vec<_> = spans @@ -105,9 +105,9 @@ impl<'a> PaneResizer<'a> { } // Update span positions based on their rounded sizes - for spans in grid.iter_mut() { + for spans in &mut grid { let mut offset = 0; - for span in spans.iter_mut() { + for span in spans { span.pos = offset; let sz = rounded_sizes[&span.size_var]; if sz < 1 { |