Skip to content

Commit

Permalink
refactor: Simplify replace_character_at logic
Browse files Browse the repository at this point in the history
The original condition checked absolute_x_index was in bounds, then
used the index to manipulate it. This is equivalent to getting a
ref to the character at that position and manipulating directly
  • Loading branch information
aidanhs committed Dec 30, 2023
1 parent a9a5a69 commit 3b55f63
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3301,16 +3301,13 @@ impl Row {
}
pub fn replace_character_at(&mut self, terminal_character: TerminalCharacter, x: usize) {
let absolute_x_index = self.absolute_character_index(x);
if absolute_x_index < self.columns.len() {
if let Some(character) = self.columns.get_mut(absolute_x_index) {
let terminal_character_width = terminal_character.width();
self.columns.push_back(terminal_character);
// this is much more performant than remove/insert
if let Some(character) = self.columns.swap_remove_back(absolute_x_index) {
let excess_width = character.width().saturating_sub(terminal_character_width);
for _ in 0..excess_width {
self.columns
.insert(absolute_x_index, EMPTY_TERMINAL_CHARACTER);
}
let character = std::mem::replace(character, terminal_character);
let excess_width = character.width().saturating_sub(terminal_character_width);
for _ in 0..excess_width {
self.columns
.insert(absolute_x_index, EMPTY_TERMINAL_CHARACTER);
}
}
self.width = None;
Expand Down

0 comments on commit 3b55f63

Please sign in to comment.