Skip to content

Commit 3b55f63

Browse files
committed
refactor: Simplify replace_character_at logic
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
1 parent a9a5a69 commit 3b55f63

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

zellij-server/src/panes/grid.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,16 +3301,13 @@ impl Row {
33013301
}
33023302
pub fn replace_character_at(&mut self, terminal_character: TerminalCharacter, x: usize) {
33033303
let absolute_x_index = self.absolute_character_index(x);
3304-
if absolute_x_index < self.columns.len() {
3304+
if let Some(character) = self.columns.get_mut(absolute_x_index) {
33053305
let terminal_character_width = terminal_character.width();
3306-
self.columns.push_back(terminal_character);
3307-
// this is much more performant than remove/insert
3308-
if let Some(character) = self.columns.swap_remove_back(absolute_x_index) {
3309-
let excess_width = character.width().saturating_sub(terminal_character_width);
3310-
for _ in 0..excess_width {
3311-
self.columns
3312-
.insert(absolute_x_index, EMPTY_TERMINAL_CHARACTER);
3313-
}
3306+
let character = std::mem::replace(character, terminal_character);
3307+
let excess_width = character.width().saturating_sub(terminal_character_width);
3308+
for _ in 0..excess_width {
3309+
self.columns
3310+
.insert(absolute_x_index, EMPTY_TERMINAL_CHARACTER);
33143311
}
33153312
}
33163313
self.width = None;

0 commit comments

Comments
 (0)