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 4 weeks 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)
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() {
1
@ -1834,7 +1838,11 @@ impl Component for Listing {
return true;
}
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() {
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 {
fn as_ref(&self) -> &'static str {
csi!($value)
@ -131,6 +125,16 @@ derive_csi_sequence!(
(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!(
#[doc = "`CSI Ps ; Ps ; Ps t`, where `Ps = 2 2 ; 0` -> Save xterm icon and window title on \
stack."]

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

Loading…
Cancel
Save