Skip to content

Commit

Permalink
Track arrow key application mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jun 20, 2024
1 parent 4f4c7be commit 8fcffb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub(crate) struct Terminal {
origin_mode: OriginMode,
auto_wrap_mode: bool,
new_line_mode: bool,
arrow_key_mode: ArrowKeyMode,
next_print_wraps: bool,
top_margin: usize,
bottom_margin: usize,
Expand All @@ -53,6 +54,12 @@ pub enum OriginMode {
Relative,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ArrowKeyMode {
Normal,
Application,
}

#[derive(Debug, PartialEq)]
pub struct SavedCtx {
pub cursor_col: usize,
Expand Down Expand Up @@ -100,6 +107,7 @@ impl Terminal {
origin_mode: OriginMode::Absolute,
auto_wrap_mode: true,
new_line_mode: false,
arrow_key_mode: ArrowKeyMode::Normal,
next_print_wraps: false,
top_margin: 0,
bottom_margin: (rows - 1),
Expand Down Expand Up @@ -396,6 +404,10 @@ impl Terminal {
self.primary_buffer().text()
}

pub fn arrow_key_app_mode(&self) -> bool {
self.arrow_key_mode == ArrowKeyMode::Application
}

#[cfg(test)]
pub fn verify(&self) {
assert!(self.cursor.row < self.rows);
Expand All @@ -420,6 +432,7 @@ impl Terminal {
assert_eq!(self.origin_mode, other.origin_mode);
assert_eq!(self.auto_wrap_mode, other.auto_wrap_mode);
assert_eq!(self.new_line_mode, other.new_line_mode);
assert_eq!(self.arrow_key_mode, other.arrow_key_mode);
assert_eq!(self.next_print_wraps, other.next_print_wraps);
assert_eq!(self.top_margin, other.top_margin);
assert_eq!(self.bottom_margin, other.bottom_margin);
Expand Down Expand Up @@ -1064,6 +1077,10 @@ impl Executor for Terminal {
fn prv_sm(&mut self, params: &Params) {
for param in params.iter() {
match param.as_slice() {
[1] => {
self.arrow_key_mode = ArrowKeyMode::Application;
}

[6] => {
self.origin_mode = OriginMode::Relative;
self.move_cursor_home();
Expand Down Expand Up @@ -1097,6 +1114,10 @@ impl Executor for Terminal {
fn prv_rm(&mut self, params: &Params) {
for param in params.iter() {
match param.as_slice() {
[1] => {
self.arrow_key_mode = ArrowKeyMode::Normal;
}

[6] => {
self.origin_mode = OriginMode::Absolute;
self.move_cursor_home();
Expand Down Expand Up @@ -1320,7 +1341,7 @@ impl Dump for Terminal {
seq.push_str("\u{9b}?25l");
}

// Below 3 must happen after ALL prints as they alter print behaviour,
// Following 3 steps must happen after ALL prints as they alter print behaviour,
// including the "move cursor past right border one" above.

// 10. setup charset
Expand Down Expand Up @@ -1361,6 +1382,13 @@ impl Dump for Terminal {
seq.push_str("\u{9b}20h");
}

// 14. setup cursor key mode

if self.arrow_key_mode == ArrowKeyMode::Application {
// enable new line mode
seq.push_str("\u{9b}?1h");
}

seq
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/vt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl Vt {
self.terminal.cursor()
}

pub fn arrow_key_app_mode(&self) -> bool {
self.terminal.arrow_key_app_mode()
}

pub fn dump(&self) -> String {
let mut seq = self.terminal.dump();
seq.push_str(&self.parser.dump());
Expand Down Expand Up @@ -1200,7 +1204,7 @@ mod tests {
}

fn gen_csi_sm_rm_param() -> impl Strategy<Value = Vec<char>> {
let modes = vec![4, 6, 7, 20, 25, 47, 1047, 1048, 1049];
let modes = vec![1, 4, 6, 7, 20, 25, 47, 1047, 1048, 1049];

prop_oneof![
prop::sample::select(modes).prop_map(|n| n.to_string().chars().collect()),
Expand Down

0 comments on commit 8fcffb9

Please sign in to comment.