terminal: add support for Alternate Scroll Mode (xterm)

If mouse mode is on, using the mouse scroll wheel will scroll inside
meli.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/398/head
Manos Pitsidianakis 1 month ago
parent 06ec2790d0
commit 0e1e5b9ea7
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -1811,7 +1811,11 @@ impl Component for Listing {
_ => {} _ => {}
}, },
UIEvent::Input(ref key) UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::LISTING]["scroll_up"]) => if shortcut!(key == shortcuts[Shortcuts::LISTING]["scroll_up"])
|| matches!(
key,
Key::Mouse(MouseEvent::Press(MouseButton::WheelUp, _, __))
) =>
{ {
let amount = if self.cmd_buf.is_empty() { let amount = if self.cmd_buf.is_empty() {
1 1
@ -1834,7 +1838,11 @@ impl Component for Listing {
return true; return true;
} }
UIEvent::Input(ref key) UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::LISTING]["scroll_down"]) => if shortcut!(key == shortcuts[Shortcuts::LISTING]["scroll_down"])
|| matches!(
key,
Key::Mouse(MouseEvent::Press(MouseButton::WheelDown, _, __))
) =>
{ {
let amount = if self.cmd_buf.is_empty() { let amount = if self.cmd_buf.is_empty() {
1 1

@ -97,12 +97,6 @@ macro_rules! derive_csi_sequence {
} }
} }
impl AsRef<[u8]> for $name {
fn as_ref(&self) -> &'static [u8] {
csi!($value).as_bytes()
}
}
impl AsRef<str> for $name { impl AsRef<str> for $name {
fn as_ref(&self) -> &'static str { fn as_ref(&self) -> &'static str {
csi!($value) csi!($value)
@ -131,6 +125,16 @@ derive_csi_sequence!(
(DisableSGRMouse, "?1006l") (DisableSGRMouse, "?1006l")
); );
derive_csi_sequence!(
///Ps = 1 0 0 7 Enable Alternate Scroll Mode, xterm.
(EnableAlternateScrollMode, "?1007h")
);
derive_csi_sequence!(
///Ps = 1 0 0 7 Disable Alternate Scroll Mode, xterm.
(DisableAlternateScrollMode, "?1007l")
);
derive_csi_sequence!( derive_csi_sequence!(
#[doc = "`CSI Ps ; Ps ; Ps t`, where `Ps = 2 2 ; 0` -> Save xterm icon and window title on \ #[doc = "`CSI Ps ; Ps ; Ps t`, where `Ps = 2 2 ; 0` -> Save xterm icon and window title on \
stack."] stack."]

@ -27,8 +27,9 @@ use termion::{clear, cursor, raw::IntoRawMode, screen::AlternateScreen};
use crate::{ use crate::{
terminal::{ terminal::{
cells::CellBuffer, Alignment, BracketModeEnd, BracketModeStart, Cell, Color, DisableMouse, cells::CellBuffer, Alignment, BracketModeEnd, BracketModeStart, Cell, Color,
DisableSGRMouse, EnableMouse, EnableSGRMouse, Pos, RestoreWindowTitleIconFromStack, DisableAlternateScrollMode, DisableMouse, DisableSGRMouse, EnableAlternateScrollMode,
EnableMouse, EnableSGRMouse, Pos, RestoreWindowTitleIconFromStack,
SaveWindowTitleIconToStack, SaveWindowTitleIconToStack,
}, },
Attr, Context, Attr, Context,
@ -88,6 +89,31 @@ impl Tty {
#[inline] #[inline]
pub fn set_mouse(&mut self, mouse: bool) -> &mut Self { pub fn set_mouse(&mut self, mouse: bool) -> &mut Self {
self.mouse = mouse; self.mouse = mouse;
let Some(stdout) = self.stdout.as_mut() else {
return self;
};
write!(
stdout,
"{enable_mouse}{enable_sgr_mouse}{enable_alt_scroll}",
enable_mouse = if mouse {
EnableMouse.as_ref()
} else {
DisableMouse.as_ref()
},
enable_sgr_mouse = if mouse {
EnableSGRMouse.as_ref()
} else {
DisableSGRMouse.as_ref()
},
enable_alt_scroll = if mouse {
EnableAlternateScrollMode.as_ref()
} else {
DisableAlternateScrollMode.as_ref()
},
)
.expect("Could not write to stdout");
_ = stdout.flush();
self self
} }
@ -315,13 +341,18 @@ impl Screen<Tty> {
let mouse = self.display.mouse; let mouse = self.display.mouse;
write!( write!(
stdout, stdout,
"{}{}{}{}{disable_sgr_mouse}{disable_mouse}", "{}{}{}{}{disable_sgr_mouse}{disable_mouse}{disable_alt_scroll}",
termion::screen::ToMainScreen, termion::screen::ToMainScreen,
cursor::Show, cursor::Show,
RestoreWindowTitleIconFromStack, RestoreWindowTitleIconFromStack,
BracketModeEnd, BracketModeEnd,
disable_sgr_mouse = if mouse { DisableSGRMouse.as_ref() } else { "" }, disable_sgr_mouse = if mouse { DisableSGRMouse.as_ref() } else { "" },
disable_mouse = if mouse { DisableMouse.as_ref() } else { "" }, disable_mouse = if mouse { DisableMouse.as_ref() } else { "" },
disable_alt_scroll = if mouse {
DisableAlternateScrollMode.as_ref()
} else {
""
},
) )
.unwrap(); .unwrap();
self.flush(); self.flush();
@ -336,7 +367,7 @@ impl Screen<Tty> {
write!( write!(
&mut stdout, &mut stdout,
"{save_title_to_stack}{}{}{}{window_title}{}{}{enable_mouse}{enable_sgr_mouse}", "{save_title_to_stack}{}{}{}{window_title}{}{}{enable_mouse}{enable_sgr_mouse}{enable_alt_scroll}",
termion::screen::ToAlternateScreen, termion::screen::ToAlternateScreen,
cursor::Hide, cursor::Hide,
clear::All, clear::All,
@ -358,6 +389,11 @@ impl Screen<Tty> {
} else { } else {
"" ""
}, },
enable_alt_scroll = if self.display.mouse {
EnableAlternateScrollMode.as_ref()
} else {
""
},
) )
.unwrap(); .unwrap();

Loading…
Cancel
Save