Skip to content

Commit

Permalink
fix(pane): handle ^M and ^[K codes properly
Browse files Browse the repository at this point in the history
* ^M is the control code for carriage return ('\r')
* ^[K is equivalent to ^[0K
  • Loading branch information
Edward Shin authored and Hylian committed Jul 10, 2024
1 parent 8e33b20 commit e999e5b
Showing 1 changed file with 15 additions and 39 deletions.
54 changes: 15 additions & 39 deletions zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,28 +1519,6 @@ impl Grid {
self.cursor.y - count
};
}
pub fn move_cursor_up_with_scrolling(&mut self, count: usize) {
let (scroll_region_top, scroll_region_bottom) =
self.scroll_region.unwrap_or((0, self.height - 1));
for _ in 0..count {
let current_line_index = self.cursor.y;
if current_line_index == scroll_region_top {
// if we're at the top line, we create a new line and remove the last line that
// would otherwise overflow
if scroll_region_bottom < self.viewport.len() {
self.viewport.remove(scroll_region_bottom);
}

self.viewport
.insert(current_line_index, Row::new().canonical());
} else if current_line_index > scroll_region_top
&& current_line_index <= scroll_region_bottom
{
self.move_cursor_up(count);
}
}
self.output_buffer.update_all_lines();
}
pub fn move_cursor_down_until_edge_of_screen(
&mut self,
count: usize,
Expand Down Expand Up @@ -2466,21 +2444,20 @@ impl Perform for Grid {
self.move_cursor_forward_until_edge(move_by);
} else if c == 'K' {
// clear line (0 => right, 1 => left, 2 => all)
if let Some(clear_type) = params_iter.next().map(|param| param[0]) {
let mut char_to_replace = EMPTY_TERMINAL_CHARACTER;
if let Some(background_color) = self.cursor.pending_styles.background {
char_to_replace
.styles
.update(|styles| styles.background = Some(background_color));
}
if clear_type == 0 {
self.replace_characters_in_line_after_cursor(char_to_replace);
} else if clear_type == 1 {
self.replace_characters_in_line_before_cursor(char_to_replace);
} else if clear_type == 2 {
self.clear_cursor_line();
}
};
let clear_type = params_iter.next().map(|param| param[0]).unwrap_or(0);
let mut char_to_replace = EMPTY_TERMINAL_CHARACTER;
if let Some(background_color) = self.cursor.pending_styles.background {
char_to_replace
.styles
.update(|styles| styles.background = Some(background_color));
}
if clear_type == 0 {
self.replace_characters_in_line_after_cursor(char_to_replace);
} else if clear_type == 1 {
self.replace_characters_in_line_before_cursor(char_to_replace);
} else if clear_type == 2 {
self.clear_cursor_line();
}
} else if c == 'J' {
// clear all (0 => below, 1 => above, 2 => all, 3 => saved)
let mut char_to_replace = EMPTY_TERMINAL_CHARACTER;
Expand Down Expand Up @@ -3085,8 +3062,7 @@ impl Perform for Grid {
self.move_cursor_to_beginning_of_line();
},
(b'M', None) => {
// TODO: if cursor is at the top, it should go down one
self.move_cursor_up_with_scrolling(1);
self.move_cursor_to_beginning_of_line();
},
(b'c', None) => {
self.reset_terminal_state();
Expand Down

0 comments on commit e999e5b

Please sign in to comment.