A little refactoring (#1663)

* general refactors

* applied `cargo fmt`

* adding BRACKETED_PASTE_BEGIN and BRACKETED_PASTE_END constans

* removing csi.rs trait
This commit is contained in:
TornaxO7 2022-08-16 18:29:45 +02:00 committed by GitHub
parent d9d7ef3aef
commit 3da1cb1521
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 79 deletions

View file

@ -1,12 +1,13 @@
pub mod alacritty_functions; pub mod alacritty_functions;
mod floating_panes;
pub mod grid; pub mod grid;
pub mod link_handler; pub mod link_handler;
mod plugin_pane;
mod search;
pub mod selection; pub mod selection;
pub mod sixel; pub mod sixel;
pub mod terminal_character; pub mod terminal_character;
mod floating_panes;
mod plugin_pane;
mod search;
mod terminal_pane; mod terminal_pane;
mod tiled_panes; mod tiled_panes;

View file

@ -668,6 +668,20 @@ pub enum CursorShape {
BlinkingBeam, BlinkingBeam,
} }
impl CursorShape {
pub fn get_csi_str(&self) -> &str {
match self {
CursorShape::Initial => "\u{1b}[0 q",
CursorShape::Block => "\u{1b}[2 q",
CursorShape::BlinkingBlock => "\u{1b}[1 q",
CursorShape::Underline => "\u{1b}[4 q",
CursorShape::BlinkingUnderline => "\u{1b}[3 q",
CursorShape::Beam => "\u{1b}[6 q",
CursorShape::BlinkingBeam => "\u{1b}[5 q",
}
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Cursor { pub struct Cursor {
pub x: usize, pub x: usize,

View file

@ -2,7 +2,7 @@ use crate::output::{CharacterChunk, SixelImageChunk};
use crate::panes::sixel::SixelImageStore; use crate::panes::sixel::SixelImageStore;
use crate::panes::{ use crate::panes::{
grid::Grid, grid::Grid,
terminal_character::{CursorShape, TerminalCharacter, EMPTY_TERMINAL_CHARACTER}, terminal_character::{TerminalCharacter, EMPTY_TERMINAL_CHARACTER},
}; };
use crate::panes::{AnsiCode, LinkHandler}; use crate::panes::{AnsiCode, LinkHandler};
use crate::pty::VteBytes; use crate::pty::VteBytes;
@ -24,9 +24,53 @@ use zellij_utils::{
vte, vte,
}; };
use crate::ui::pane_boundaries_frame::{FrameParams, PaneFrame};
pub const SELECTION_SCROLL_INTERVAL_MS: u64 = 10; pub const SELECTION_SCROLL_INTERVAL_MS: u64 = 10;
use crate::ui::pane_boundaries_frame::{FrameParams, PaneFrame}; // Some keys in different formats but are used in the code
const LEFT_ARROW: &[u8] = &[27, 91, 68];
const RIGHT_ARROW: &[u8] = &[27, 91, 67];
const UP_ARROW: &[u8] = &[27, 91, 65];
const DOWN_ARROW: &[u8] = &[27, 91, 66];
const HOME_KEY: &[u8] = &[27, 91, 72];
const END_KEY: &[u8] = &[27, 91, 70];
const BRACKETED_PASTE_BEGIN: &[u8] = &[27, 91, 50, 48, 48, 126];
const BRACKETED_PASTE_END: &[u8] = &[27, 91, 50, 48, 49, 126];
const TERMINATING_STRING: &str = "\0";
const DELETE_KEY: &str = "\u{007F}";
const BACKSPACE_KEY: &str = "\u{0008}";
/// The ansi encoding of some keys
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum AnsiEncoding {
Left,
Right,
Up,
Down,
Home,
End,
}
impl AnsiEncoding {
/// Returns the ANSI representation of the entries.
/// NOTE: There is an ANSI escape code (27) at the beginning of the string,
/// some editors will not show this
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Left => "OD".as_bytes(),
Self::Right => "OC".as_bytes(),
Self::Up => "OC".as_bytes(),
Self::Down => "OB".as_bytes(),
Self::Home => &[27, 79, 72], // ESC O H
Self::End => &[27, 79, 70], // ESC O F
}
}
pub fn as_vec_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
#[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy, Debug)]
pub enum PaneId { pub enum PaneId {
@ -118,63 +162,39 @@ impl Pane for TerminalPane {
// needs to be adjusted. // needs to be adjusted.
// here we match against those cases - if need be, we adjust the input and if not // here we match against those cases - if need be, we adjust the input and if not
// we send back the original input // we send back the original input
match input_bytes.as_slice() { if self.grid.cursor_key_mode {
[27, 91, 68] => { match input_bytes.as_slice() {
// left arrow LEFT_ARROW => {
if self.grid.cursor_key_mode { return AnsiEncoding::Left.as_vec_bytes();
// 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 RIGHT_ARROW => {
return "OD".as_bytes().to_vec(); return AnsiEncoding::Right.as_vec_bytes();
} },
}, UP_ARROW => {
[27, 91, 67] => { return AnsiEncoding::Up.as_vec_bytes();
// right arrow },
if self.grid.cursor_key_mode { DOWN_ARROW => {
// please note that in the line below, there is an ANSI escape code (27) at the beginning of the string, return AnsiEncoding::Down.as_vec_bytes();
// some editors will not show this },
return "OC".as_bytes().to_vec();
}
},
[27, 91, 65] => {
// up arrow
if self.grid.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, 72] => { HOME_KEY => {
// home key return AnsiEncoding::Home.as_vec_bytes();
if self.grid.cursor_key_mode { },
return vec![27, 79, 72]; // ESC O H END_KEY => {
} return AnsiEncoding::End.as_vec_bytes();
}, },
[27, 91, 70] => { BRACKETED_PASTE_BEGIN | BRACKETED_PASTE_END => {
// end key if !self.grid.bracketed_paste_mode {
if self.grid.cursor_key_mode { // Zellij itself operates in bracketed paste mode, so the terminal sends these
return vec![27, 79, 70]; // ESC O F // instructions (bracketed paste start and bracketed paste end respectively)
} // when pasting input. We only need to make sure not to send them to terminal
}, // panes who do not work in this mode
[27, 91, 66] => { return vec![];
// down arrow }
if self.grid.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(); }
}
},
[27, 91, 50, 48, 48, 126] | [27, 91, 50, 48, 49, 126] => {
if !self.grid.bracketed_paste_mode {
// Zellij itself operates in bracketed paste mode, so the terminal sends these
// instructions (bracketed paste start and bracketed paste end respectively)
// when pasting input. We only need to make sure not to send them to terminal
// panes who do not work in this mode
return vec![];
}
},
_ => {},
};
input_bytes input_bytes
} }
fn position_and_size(&self) -> PaneGeom { fn position_and_size(&self) -> PaneGeom {
@ -324,6 +344,7 @@ impl Pane for TerminalPane {
pane_title, pane_title,
frame_params, frame_params,
); );
match self.frame.get(&client_id) { match self.frame.get(&client_id) {
// TODO: use and_then or something? // TODO: use and_then or something?
Some(last_frame) => { Some(last_frame) => {
@ -388,11 +409,10 @@ impl Pane for TerminalPane {
} }
fn update_name(&mut self, name: &str) { fn update_name(&mut self, name: &str) {
match name { match name {
"\0" => { TERMINATING_STRING => {
self.pane_name = String::new(); self.pane_name = String::new();
}, },
"\u{007F}" | "\u{0008}" => { DELETE_KEY | BACKSPACE_KEY => {
//delete and backspace keys
self.pane_name.pop(); self.pane_name.pop();
}, },
c => { c => {
@ -470,15 +490,7 @@ impl Pane for TerminalPane {
self.active_at = time; self.active_at = time;
} }
fn cursor_shape_csi(&self) -> String { fn cursor_shape_csi(&self) -> String {
match self.grid.cursor_shape() { self.grid.cursor_shape().get_csi_str().to_string()
CursorShape::Initial => "\u{1b}[0 q".to_string(),
CursorShape::Block => "\u{1b}[2 q".to_string(),
CursorShape::BlinkingBlock => "\u{1b}[1 q".to_string(),
CursorShape::Underline => "\u{1b}[4 q".to_string(),
CursorShape::BlinkingUnderline => "\u{1b}[3 q".to_string(),
CursorShape::Beam => "\u{1b}[6 q".to_string(),
CursorShape::BlinkingBeam => "\u{1b}[5 q".to_string(),
}
} }
fn drain_messages_to_pty(&mut self) -> Vec<Vec<u8>> { fn drain_messages_to_pty(&mut self) -> Vec<Vec<u8>> {
self.grid.pending_messages_to_pty.drain(..).collect() self.grid.pending_messages_to_pty.drain(..).collect()
@ -496,14 +508,18 @@ impl Pane for TerminalPane {
fn update_selection(&mut self, to: &Position, _client_id: ClientId) { fn update_selection(&mut self, to: &Position, _client_id: ClientId) {
let should_scroll = self.selection_scrolled_at.elapsed() let should_scroll = self.selection_scrolled_at.elapsed()
>= time::Duration::from_millis(SELECTION_SCROLL_INTERVAL_MS); >= time::Duration::from_millis(SELECTION_SCROLL_INTERVAL_MS);
let cursor_at_the_bottom = to.line.0 < 0 && should_scroll;
let cursor_at_the_top = to.line.0 as usize >= self.grid.height && should_scroll;
let cursor_in_the_middle = to.line.0 >= 0 && (to.line.0 as usize) < self.grid.height;
// TODO: check how far up/down mouse is relative to pane, to increase scroll lines? // TODO: check how far up/down mouse is relative to pane, to increase scroll lines?
if to.line.0 < 0 && should_scroll { if cursor_at_the_bottom {
self.grid.scroll_up_one_line(); self.grid.scroll_up_one_line();
self.selection_scrolled_at = time::Instant::now(); self.selection_scrolled_at = time::Instant::now();
} else if to.line.0 as usize >= self.grid.height && should_scroll { } else if cursor_at_the_top {
self.grid.scroll_down_one_line(); self.grid.scroll_down_one_line();
self.selection_scrolled_at = time::Instant::now(); self.selection_scrolled_at = time::Instant::now();
} else if to.line.0 >= 0 && (to.line.0 as usize) < self.grid.height { } else if cursor_in_the_middle {
self.grid.update_selection(to); self.grid.update_selection(to);
} }
@ -561,11 +577,10 @@ impl Pane for TerminalPane {
fn update_search_term(&mut self, needle: &str) { fn update_search_term(&mut self, needle: &str) {
match needle { match needle {
"\0" => { TERMINATING_STRING => {
self.search_term = String::new(); self.search_term = String::new();
}, },
"\u{007F}" | "\u{0008}" => { DELETE_KEY | BACKSPACE_KEY => {
//delete and backspace keys
self.search_term.pop(); self.search_term.pop();
}, },
c => { c => {