fix(style): Deduplicate a massive chunk of code

This commit is contained in:
Brooks J Rady 2021-01-05 13:09:01 +00:00
parent c7db38d0c5
commit 8dd9a6c7b2

View File

@ -280,246 +280,6 @@ impl Pane for TerminalPane {
}
}
impl Pane for &mut TerminalPane {
fn x(&self) -> usize {
self.get_x()
}
fn y(&self) -> usize {
self.get_y()
}
fn rows(&self) -> usize {
self.get_rows()
}
fn columns(&self) -> usize {
self.get_columns()
}
fn reset_size_and_position_override(&mut self) {
self.position_and_size_override = None;
self.reflow_lines();
self.mark_for_rerender();
}
fn change_size_p(&mut self, position_and_size: &PositionAndSize) {
self.position_and_size = *position_and_size;
self.reflow_lines();
self.mark_for_rerender();
}
fn get_rows(&self) -> usize {
match &self.position_and_size_override.as_ref() {
Some(position_and_size_override) => position_and_size_override.rows,
None => self.position_and_size.rows as usize,
}
}
fn get_columns(&self) -> usize {
match &self.position_and_size_override.as_ref() {
Some(position_and_size_override) => position_and_size_override.columns,
None => self.position_and_size.columns as usize,
}
}
fn change_size(&mut self, ws: &PositionAndSize) {
self.position_and_size.columns = ws.columns;
self.position_and_size.rows = ws.rows;
self.reflow_lines();
self.mark_for_rerender();
}
fn override_size_and_position(&mut self, x: usize, y: usize, size: &PositionAndSize) {
let position_and_size_override = PositionAndSize {
x,
y,
rows: size.rows,
columns: size.columns,
};
self.position_and_size_override = Some(position_and_size_override);
self.reflow_lines();
self.mark_for_rerender();
}
fn handle_event(&mut self, event: VteEvent) {
match event {
VteEvent::Print(c) => {
self.print(c);
self.mark_for_rerender();
}
VteEvent::Execute(byte) => {
self.execute(byte);
}
VteEvent::Hook(params, intermediates, ignore, c) => {
self.hook(&params, &intermediates, ignore, c);
}
VteEvent::Put(byte) => {
self.put(byte);
}
VteEvent::Unhook => {
self.unhook();
}
VteEvent::OscDispatch(params, bell_terminated) => {
let params: Vec<&[u8]> = params.iter().map(|p| &p[..]).collect();
self.osc_dispatch(&params[..], bell_terminated);
}
VteEvent::CsiDispatch(params, intermediates, ignore, c) => {
self.csi_dispatch(&params, &intermediates, ignore, c);
}
VteEvent::EscDispatch(intermediates, ignore, byte) => {
self.esc_dispatch(&intermediates, ignore, byte);
}
}
}
fn cursor_coordinates(&self) -> Option<(usize, usize)> {
// (x, y)
self.scroll.cursor_coordinates_on_screen()
}
fn adjust_input_to_terminal(&self, input_bytes: Vec<u8>) -> Vec<u8> {
// there are some cases in which the terminal state means that input sent to it
// needs to be adjusted.
// here we match against those cases - if need be, we adjust the input and if not
// we send back the original input
match input_bytes.as_slice() {
[27, 91, 68] => {
// left arrow
if self.cursor_key_mode {
// please note that in the line below, there is an ANSI escape code (27) at the beginning of the string,
// some editors will not show this
return "OD".as_bytes().to_vec();
}
}
[27, 91, 67] => {
// right arrow
if self.cursor_key_mode {
// please note that in the line below, there is an ANSI escape code (27) at the beginning of the string,
// some editors will not show this
return "OC".as_bytes().to_vec();
}
}
[27, 91, 65] => {
// up arrow
if self.cursor_key_mode {
// please note that in the line below, there is an ANSI escape code (27) at the beginning of the string,
// some editors will not show this
return "OA".as_bytes().to_vec();
}
}
[27, 91, 66] => {
// down arrow
if self.cursor_key_mode {
// please note that in the line below, there is an ANSI escape code (27) at the beginning of the string,
// some editors will not show this
return "OB".as_bytes().to_vec();
}
}
_ => {}
};
input_bytes
}
fn position_and_size_override(&self) -> Option<PositionAndSize> {
self.position_and_size_override
}
fn should_render(&self) -> bool {
self.should_render
}
fn set_should_render(&mut self, should_render: bool) {
self.should_render = should_render;
}
fn buffer_as_vte_output(&mut self) -> Option<String> {
// TODO: rename to render
// if self.should_render {
if true {
// while checking should_render rather than rendering each pane every time
// is more performant, it causes some problems when the pane to the left should be
// rendered and has wide characters (eg. Chinese characters or emoji)
// as a (hopefully) temporary hack, we render all panes until we find a better solution
let mut vte_output = String::new();
let buffer_lines = &self.read_buffer_as_lines();
let display_cols = self.get_columns();
let mut character_styles = CharacterStyles::new();
for (row, line) in buffer_lines.iter().enumerate() {
let x = self.get_x();
let y = self.get_y();
vte_output = format!("{}\u{1b}[{};{}H\u{1b}[m", vte_output, y + row + 1, x + 1); // goto row/col and reset styles
for (col, t_character) in line.iter().enumerate() {
if col < display_cols {
// in some cases (eg. while resizing) some characters will spill over
// before they are corrected by the shell (for the prompt) or by reflowing
// lines
if let Some(new_styles) =
character_styles.update_and_return_diff(&t_character.styles)
{
// the terminal keeps the previous styles as long as we're in the same
// line, so we only want to update the new styles here (this also
// includes resetting previous styles as needed)
vte_output = format!("{}{}", vte_output, new_styles);
}
vte_output.push(t_character.character);
}
}
character_styles.clear();
}
self.mark_for_rerender();
Some(vte_output)
} else {
None
}
}
fn pid(&self) -> RawFd {
self.pid
}
fn reduce_height_down(&mut self, count: usize) {
self.position_and_size.y += count;
self.position_and_size.rows -= count;
self.reflow_lines();
self.mark_for_rerender();
}
fn increase_height_down(&mut self, count: usize) {
self.position_and_size.rows += count;
self.reflow_lines();
self.mark_for_rerender();
}
fn increase_height_up(&mut self, count: usize) {
self.position_and_size.y -= count;
self.position_and_size.rows += count;
self.reflow_lines();
self.mark_for_rerender();
}
fn reduce_height_up(&mut self, count: usize) {
self.position_and_size.rows -= count;
self.reflow_lines();
self.mark_for_rerender();
}
fn reduce_width_right(&mut self, count: usize) {
self.position_and_size.x += count;
self.position_and_size.columns -= count;
self.reflow_lines();
self.mark_for_rerender();
}
fn reduce_width_left(&mut self, count: usize) {
self.position_and_size.columns -= count;
self.reflow_lines();
self.mark_for_rerender();
}
fn increase_width_left(&mut self, count: usize) {
self.position_and_size.x -= count;
self.position_and_size.columns += count;
self.reflow_lines();
self.mark_for_rerender();
}
fn increase_width_right(&mut self, count: usize) {
self.position_and_size.columns += count;
self.reflow_lines();
self.mark_for_rerender();
}
fn scroll_up(&mut self, count: usize) {
self.scroll.move_viewport_up(count);
self.mark_for_rerender();
}
fn scroll_down(&mut self, count: usize) {
self.scroll.move_viewport_down(count);
self.mark_for_rerender();
}
fn clear_scroll(&mut self) {
self.scroll.reset_viewport();
self.mark_for_rerender();
}
}
impl TerminalPane {
pub fn new(pid: RawFd, ws: PositionAndSize, x: usize, y: usize) -> TerminalPane {
let scroll = Scroll::new(ws.columns, ws.rows);