2024-03-30 16:10:12 +01:00
|
|
|
use libc::STDOUT_FILENO;
|
|
|
|
|
|
|
|
|
|
use crate::common::{
|
2024-10-12 10:49:03 +02:00
|
|
|
fish_reserved_codepoint, is_windows_subsystem_for_linux, read_blocked, shell_modes,
|
2025-01-26 12:08:48 +01:00
|
|
|
str2wcstring, write_loop, ScopeGuard, WSL,
|
2024-03-30 16:10:12 +01:00
|
|
|
};
|
2023-10-08 23:22:27 +02:00
|
|
|
use crate::env::{EnvStack, Environment};
|
2025-01-04 18:22:38 -06:00
|
|
|
use crate::fd_readable_set::{FdReadableSet, Timeout};
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
use crate::flog::{FloggableDebug, FLOG};
|
2024-10-21 09:16:02 +02:00
|
|
|
use crate::fork_exec::flog_safe::FLOG_SAFE;
|
2024-04-18 10:21:28 +02:00
|
|
|
use crate::global_safety::RelaxedAtomicBool;
|
2024-03-30 16:10:12 +01:00
|
|
|
use crate::key::{
|
2025-01-04 12:36:16 +01:00
|
|
|
self, alt, canonicalize_control_char, canonicalize_keyed_control_char, char_to_symbol, ctrl,
|
|
|
|
|
function_key, shift, Key, Modifiers, ViewportPosition,
|
2024-03-30 16:10:12 +01:00
|
|
|
};
|
|
|
|
|
use crate::reader::{reader_current_data, reader_test_and_clear_interrupted};
|
2024-10-12 09:00:24 +02:00
|
|
|
use crate::threads::{iothread_port, is_main_thread};
|
2023-11-25 14:37:42 -08:00
|
|
|
use crate::universal_notifier::default_notifier;
|
2024-02-27 22:25:54 +01:00
|
|
|
use crate::wchar::{encode_byte_to_char, prelude::*};
|
2024-03-30 16:10:12 +01:00
|
|
|
use crate::wutil::encoding::{mbrtowc, mbstate_t, zero_mbstate};
|
2024-12-29 14:36:11 +01:00
|
|
|
use crate::wutil::fish_wcstol;
|
2023-11-25 14:37:42 -08:00
|
|
|
use std::collections::VecDeque;
|
2024-04-23 00:04:05 +02:00
|
|
|
use std::ops::ControlFlow;
|
2023-11-25 14:37:42 -08:00
|
|
|
use std::os::fd::RawFd;
|
2024-10-12 10:49:03 +02:00
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2023-11-25 14:37:42 -08:00
|
|
|
use std::ptr;
|
2025-01-26 12:08:48 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
|
2023-10-08 23:22:27 +02:00
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
// The range of key codes for inputrc-style keyboard functions.
|
2024-01-01 21:29:05 +01:00
|
|
|
pub const R_END_INPUT_FUNCTIONS: usize = (ReadlineCmd::ReverseRepeatJump as usize) + 1;
|
2023-12-17 15:41:14 -08:00
|
|
|
|
2024-01-01 21:29:05 +01:00
|
|
|
/// Hackish: the input style, which describes how char events (only) are applied to the command
|
|
|
|
|
/// line. Note this is set only after applying bindings; it is not set from readb().
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum CharInputStyle {
|
|
|
|
|
// Insert characters normally.
|
|
|
|
|
Normal,
|
|
|
|
|
|
|
|
|
|
// Insert characters only if the cursor is not at the beginning. Otherwise, discard them.
|
|
|
|
|
NotFirst,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
|
#[repr(u8)]
|
|
|
|
|
pub enum ReadlineCmd {
|
|
|
|
|
BeginningOfLine,
|
|
|
|
|
EndOfLine,
|
|
|
|
|
ForwardChar,
|
|
|
|
|
BackwardChar,
|
2024-03-29 14:09:34 -05:00
|
|
|
BackwardCharPassive,
|
2024-01-01 21:29:05 +01:00
|
|
|
ForwardSingleChar,
|
2024-03-28 00:13:34 -05:00
|
|
|
ForwardCharPassive,
|
2024-01-01 21:29:05 +01:00
|
|
|
ForwardWord,
|
|
|
|
|
BackwardWord,
|
|
|
|
|
ForwardBigword,
|
|
|
|
|
BackwardBigword,
|
2024-09-21 15:55:44 +00:00
|
|
|
ForwardToken,
|
|
|
|
|
BackwardToken,
|
2024-01-01 21:29:05 +01:00
|
|
|
NextdOrForwardWord,
|
|
|
|
|
PrevdOrBackwardWord,
|
2025-01-10 09:09:35 +01:00
|
|
|
HistoryDelete,
|
2024-01-01 21:29:05 +01:00
|
|
|
HistorySearchBackward,
|
|
|
|
|
HistorySearchForward,
|
|
|
|
|
HistoryPrefixSearchBackward,
|
|
|
|
|
HistoryPrefixSearchForward,
|
|
|
|
|
HistoryPager,
|
2025-01-10 09:09:35 +01:00
|
|
|
#[deprecated]
|
2024-01-01 21:29:05 +01:00
|
|
|
HistoryPagerDelete,
|
|
|
|
|
DeleteChar,
|
|
|
|
|
BackwardDeleteChar,
|
|
|
|
|
KillLine,
|
|
|
|
|
Yank,
|
|
|
|
|
YankPop,
|
|
|
|
|
Complete,
|
|
|
|
|
CompleteAndSearch,
|
|
|
|
|
PagerToggleSearch,
|
|
|
|
|
BeginningOfHistory,
|
|
|
|
|
EndOfHistory,
|
|
|
|
|
BackwardKillLine,
|
|
|
|
|
KillWholeLine,
|
|
|
|
|
KillInnerLine,
|
|
|
|
|
KillWord,
|
|
|
|
|
KillBigword,
|
2024-09-21 15:55:44 +00:00
|
|
|
KillToken,
|
2024-01-01 21:29:05 +01:00
|
|
|
BackwardKillWord,
|
|
|
|
|
BackwardKillPathComponent,
|
|
|
|
|
BackwardKillBigword,
|
2024-09-21 15:55:44 +00:00
|
|
|
BackwardKillToken,
|
2024-01-01 21:29:05 +01:00
|
|
|
HistoryTokenSearchBackward,
|
|
|
|
|
HistoryTokenSearchForward,
|
|
|
|
|
SelfInsert,
|
|
|
|
|
SelfInsertNotFirst,
|
|
|
|
|
TransposeChars,
|
|
|
|
|
TransposeWords,
|
|
|
|
|
UpcaseWord,
|
|
|
|
|
DowncaseWord,
|
|
|
|
|
CapitalizeWord,
|
|
|
|
|
TogglecaseChar,
|
|
|
|
|
TogglecaseSelection,
|
|
|
|
|
Execute,
|
|
|
|
|
BeginningOfBuffer,
|
|
|
|
|
EndOfBuffer,
|
|
|
|
|
RepaintMode,
|
|
|
|
|
Repaint,
|
|
|
|
|
ForceRepaint,
|
|
|
|
|
UpLine,
|
|
|
|
|
DownLine,
|
|
|
|
|
SuppressAutosuggestion,
|
|
|
|
|
AcceptAutosuggestion,
|
|
|
|
|
BeginSelection,
|
|
|
|
|
SwapSelectionStartStop,
|
|
|
|
|
EndSelection,
|
|
|
|
|
KillSelection,
|
|
|
|
|
InsertLineUnder,
|
|
|
|
|
InsertLineOver,
|
|
|
|
|
ForwardJump,
|
|
|
|
|
BackwardJump,
|
|
|
|
|
ForwardJumpTill,
|
|
|
|
|
BackwardJumpTill,
|
2024-06-28 23:23:36 +02:00
|
|
|
JumpToMatchingBracket,
|
2024-06-29 02:20:15 +02:00
|
|
|
JumpTillMatchingBracket,
|
2024-01-01 21:29:05 +01:00
|
|
|
FuncAnd,
|
|
|
|
|
FuncOr,
|
|
|
|
|
ExpandAbbr,
|
|
|
|
|
DeleteOrExit,
|
|
|
|
|
Exit,
|
2025-01-14 19:58:26 +01:00
|
|
|
ClearCommandline,
|
2024-01-01 21:29:05 +01:00
|
|
|
CancelCommandline,
|
|
|
|
|
Cancel,
|
|
|
|
|
Undo,
|
|
|
|
|
Redo,
|
|
|
|
|
BeginUndoGroup,
|
|
|
|
|
EndUndoGroup,
|
|
|
|
|
RepeatJump,
|
|
|
|
|
ClearScreenAndRepaint,
|
2024-12-21 19:41:41 +01:00
|
|
|
ScrollbackPush,
|
2024-01-01 21:29:05 +01:00
|
|
|
// NOTE: This one has to be last.
|
|
|
|
|
ReverseRepeatJump,
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
/// Represents an event on the character input stream.
|
2024-03-02 07:58:01 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2023-11-25 14:37:42 -08:00
|
|
|
pub enum CharEventType {
|
|
|
|
|
/// A character was entered.
|
2024-03-30 16:10:12 +01:00
|
|
|
Char(Key),
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
/// A readline event.
|
|
|
|
|
Readline(ReadlineCmd),
|
|
|
|
|
|
2024-03-02 07:58:01 +01:00
|
|
|
/// A shell command.
|
|
|
|
|
Command(WString),
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// end-of-file was reached.
|
|
|
|
|
Eof,
|
|
|
|
|
|
|
|
|
|
/// An event was handled internally, or an interrupt was received. Check to see if the reader
|
|
|
|
|
/// loop should exit.
|
|
|
|
|
CheckExit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2024-03-30 13:01:57 +01:00
|
|
|
pub struct ReadlineCmdEvent {
|
|
|
|
|
pub cmd: ReadlineCmd,
|
|
|
|
|
/// The sequence of characters in the input mapping which generated this event.
|
|
|
|
|
/// Note that the generic self-insert case does not have any characters, so this would be empty.
|
|
|
|
|
/// This is also empty for invalid Unicode code points, which produce multiple characters.
|
|
|
|
|
pub seq: WString,
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
|
2024-03-30 13:01:57 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2024-03-30 16:10:12 +01:00
|
|
|
pub struct KeyEvent {
|
2024-03-30 13:01:57 +01:00
|
|
|
// The key.
|
2024-03-30 16:10:12 +01:00
|
|
|
pub key: Key,
|
2023-11-25 14:37:42 -08:00
|
|
|
// The style to use when inserting characters into the command line.
|
|
|
|
|
pub input_style: CharInputStyle,
|
|
|
|
|
/// The sequence of characters in the input mapping which generated this event.
|
|
|
|
|
/// Note that the generic self-insert case does not have any characters, so this would be empty.
|
2024-03-30 13:01:57 +01:00
|
|
|
/// This is also empty for invalid Unicode code points, which produce multiple characters.
|
2023-11-25 14:37:42 -08:00
|
|
|
pub seq: WString,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 19:31:15 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum ImplicitEvent {
|
|
|
|
|
/// end-of-file was reached.
|
|
|
|
|
Eof,
|
|
|
|
|
/// An event was handled internally, or an interrupt was received. Check to see if the reader
|
|
|
|
|
/// loop should exit.
|
|
|
|
|
CheckExit,
|
|
|
|
|
/// Our terminal window gained focus.
|
|
|
|
|
FocusIn,
|
|
|
|
|
/// Our terminal window lost focus.
|
|
|
|
|
FocusOut,
|
|
|
|
|
/// Request to disable mouse tracking.
|
|
|
|
|
DisableMouseTracking,
|
2025-01-25 16:07:15 +01:00
|
|
|
/// Primary DA response.
|
|
|
|
|
PrimaryDeviceAttribute,
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
/// Handle mouse left click.
|
|
|
|
|
MouseLeftClickContinuation(ViewportPosition, ViewportPosition),
|
2024-12-21 19:41:41 +01:00
|
|
|
/// Push prompt to top.
|
|
|
|
|
ScrollbackPushContinuation(usize),
|
2024-12-29 19:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 13:01:57 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum CharEvent {
|
|
|
|
|
/// A character was entered.
|
2024-03-30 16:10:12 +01:00
|
|
|
Key(KeyEvent),
|
2024-03-30 13:01:57 +01:00
|
|
|
|
|
|
|
|
/// A readline event.
|
|
|
|
|
Readline(ReadlineCmdEvent),
|
|
|
|
|
|
|
|
|
|
/// A shell command.
|
|
|
|
|
Command(WString),
|
|
|
|
|
|
2024-12-29 19:31:15 +01:00
|
|
|
/// Any event that has no user-visible representation.
|
|
|
|
|
Implicit(ImplicitEvent),
|
2024-03-30 13:01:57 +01:00
|
|
|
}
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
impl FloggableDebug for CharEvent {}
|
2024-03-30 13:01:57 +01:00
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
impl CharEvent {
|
|
|
|
|
pub fn is_char(&self) -> bool {
|
2024-03-30 16:10:12 +01:00
|
|
|
matches!(self, CharEvent::Key(_))
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_readline(&self) -> bool {
|
2024-03-30 13:01:57 +01:00
|
|
|
matches!(self, CharEvent::Readline(_))
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-02 07:58:01 +01:00
|
|
|
pub fn is_readline_or_command(&self) -> bool {
|
2024-03-30 13:01:57 +01:00
|
|
|
matches!(self, CharEvent::Readline(_) | CharEvent::Command(_))
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
pub fn get_char(&self) -> char {
|
|
|
|
|
let CharEvent::Key(kevt) = self else {
|
|
|
|
|
panic!("Not a char type");
|
|
|
|
|
};
|
|
|
|
|
kevt.key.codepoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_key(&self) -> Option<&KeyEvent> {
|
|
|
|
|
match self {
|
|
|
|
|
CharEvent::Key(kevt) => Some(kevt),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
pub fn get_readline(&self) -> ReadlineCmd {
|
2024-03-30 13:01:57 +01:00
|
|
|
let CharEvent::Readline(c) = self else {
|
2023-11-25 14:37:42 -08:00
|
|
|
panic!("Not a readline type");
|
|
|
|
|
};
|
2024-03-30 13:01:57 +01:00
|
|
|
c.cmd
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-02 07:58:01 +01:00
|
|
|
pub fn get_command(&self) -> Option<&wstr> {
|
2024-03-30 13:01:57 +01:00
|
|
|
match self {
|
|
|
|
|
CharEvent::Command(c) => Some(c),
|
2024-03-02 07:58:01 +01:00
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
pub fn from_char(c: char) -> CharEvent {
|
2024-03-30 16:10:12 +01:00
|
|
|
Self::from_key(Key::from_raw(c))
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
pub fn from_key(key: Key) -> CharEvent {
|
|
|
|
|
Self::from_key_seq(key, WString::new())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_key_seq(key: Key, seq: WString) -> CharEvent {
|
|
|
|
|
CharEvent::Key(KeyEvent {
|
|
|
|
|
key,
|
|
|
|
|
input_style: CharInputStyle::Normal,
|
|
|
|
|
seq,
|
|
|
|
|
})
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 13:01:57 +01:00
|
|
|
pub fn from_readline(cmd: ReadlineCmd) -> CharEvent {
|
|
|
|
|
Self::from_readline_seq(cmd, WString::new())
|
2024-03-02 07:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 13:01:57 +01:00
|
|
|
pub fn from_readline_seq(cmd: ReadlineCmd, seq: WString) -> CharEvent {
|
|
|
|
|
CharEvent::Readline(ReadlineCmdEvent { cmd, seq })
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 13:01:57 +01:00
|
|
|
pub fn from_check_exit() -> CharEvent {
|
2024-12-29 19:31:15 +01:00
|
|
|
CharEvent::Implicit(ImplicitEvent::CheckExit)
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Time in milliseconds to wait for another byte to be available for reading
|
|
|
|
|
/// after \x1B is read before assuming that escape key was pressed, and not an
|
|
|
|
|
/// escape sequence.
|
2024-04-30 10:28:47 +02:00
|
|
|
const WAIT_ON_ESCAPE_DEFAULT: usize = 30;
|
|
|
|
|
static WAIT_ON_ESCAPE_MS: AtomicUsize = AtomicUsize::new(WAIT_ON_ESCAPE_DEFAULT);
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
const WAIT_ON_SEQUENCE_KEY_INFINITE: usize = usize::MAX;
|
|
|
|
|
static WAIT_ON_SEQUENCE_KEY_MS: AtomicUsize = AtomicUsize::new(WAIT_ON_SEQUENCE_KEY_INFINITE);
|
|
|
|
|
|
2025-01-02 15:14:15 +01:00
|
|
|
pub(crate) static READING_BUFFERED_INPUT: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// Internal function used by readch to read one byte.
|
|
|
|
|
/// This calls select() on three fds: input (e.g. stdin), the ioport notifier fd (for main thread
|
|
|
|
|
/// requests), and the uvar notifier. This returns either the byte which was read, or one of the
|
|
|
|
|
/// special values below.
|
|
|
|
|
enum ReadbResult {
|
|
|
|
|
// A byte was successfully read.
|
|
|
|
|
Byte(u8),
|
|
|
|
|
|
|
|
|
|
// The in fd has been closed.
|
|
|
|
|
Eof,
|
|
|
|
|
|
|
|
|
|
// select() was interrupted by a signal.
|
|
|
|
|
Interrupted,
|
|
|
|
|
|
|
|
|
|
// Our uvar notifier reported a change (either through poll() or its fd).
|
|
|
|
|
UvarNotified,
|
|
|
|
|
|
|
|
|
|
// Our ioport reported a change, so service main thread requests.
|
|
|
|
|
IOPortNotified,
|
2024-03-30 16:10:12 +01:00
|
|
|
|
|
|
|
|
NothingToRead,
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
fn readb(in_fd: RawFd, blocking: bool) -> ReadbResult {
|
2023-11-25 14:37:42 -08:00
|
|
|
assert!(in_fd >= 0, "Invalid in fd");
|
|
|
|
|
let mut fdset = FdReadableSet::new();
|
|
|
|
|
loop {
|
|
|
|
|
fdset.clear();
|
|
|
|
|
fdset.add(in_fd);
|
|
|
|
|
|
|
|
|
|
// Add the completion ioport.
|
|
|
|
|
let ioport_fd = iothread_port();
|
|
|
|
|
fdset.add(ioport_fd);
|
|
|
|
|
|
|
|
|
|
// Get the uvar notifier fd (possibly none).
|
|
|
|
|
let notifier = default_notifier();
|
|
|
|
|
let notifier_fd = notifier.notification_fd();
|
|
|
|
|
if let Some(notifier_fd) = notifier.notification_fd() {
|
|
|
|
|
fdset.add(notifier_fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Here's where we call select().
|
2025-01-02 15:14:15 +01:00
|
|
|
let select_res = fdset.check_readable(if blocking && !READING_BUFFERED_INPUT.load() {
|
2025-01-04 18:22:38 -06:00
|
|
|
Timeout::Forever
|
2024-03-30 16:10:12 +01:00
|
|
|
} else {
|
2025-01-04 18:22:38 -06:00
|
|
|
Timeout::ZERO
|
2024-03-30 16:10:12 +01:00
|
|
|
});
|
2023-11-25 14:37:42 -08:00
|
|
|
if select_res < 0 {
|
|
|
|
|
let err = errno::errno().0;
|
|
|
|
|
if err == libc::EINTR || err == libc::EAGAIN {
|
|
|
|
|
// A signal.
|
|
|
|
|
return ReadbResult::Interrupted;
|
|
|
|
|
} else {
|
|
|
|
|
// Some fd was invalid, so probably the tty has been closed.
|
|
|
|
|
return ReadbResult::Eof;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
if blocking {
|
|
|
|
|
// select() did not return an error, so we may have a readable fd.
|
|
|
|
|
// The priority order is: uvars, stdin, ioport.
|
|
|
|
|
// Check to see if we want a universal variable barrier.
|
|
|
|
|
if let Some(notifier_fd) = notifier_fd {
|
|
|
|
|
if fdset.test(notifier_fd) && notifier.notification_fd_became_readable(notifier_fd)
|
|
|
|
|
{
|
|
|
|
|
return ReadbResult::UvarNotified;
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check stdin.
|
|
|
|
|
if fdset.test(in_fd) {
|
|
|
|
|
let mut arr: [u8; 1] = [0];
|
2024-01-20 18:45:36 +01:00
|
|
|
if read_blocked(in_fd, &mut arr) != Ok(1) {
|
2023-11-25 14:37:42 -08:00
|
|
|
// The terminal has been closed.
|
|
|
|
|
return ReadbResult::Eof;
|
|
|
|
|
}
|
2025-01-04 12:36:16 +01:00
|
|
|
let c = arr[0];
|
|
|
|
|
FLOG!(reader, "Read byte", char_to_symbol(char::from(c)));
|
2023-11-25 14:37:42 -08:00
|
|
|
// The common path is to return a u8.
|
2025-01-04 12:36:16 +01:00
|
|
|
return ReadbResult::Byte(c);
|
2025-01-02 15:14:15 +01:00
|
|
|
} else {
|
|
|
|
|
READING_BUFFERED_INPUT.store(false);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
if !blocking {
|
|
|
|
|
return ReadbResult::NothingToRead;
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
// Check for iothread completions only if there is no data to be read from the stdin.
|
|
|
|
|
// This gives priority to the foreground.
|
|
|
|
|
if fdset.test(ioport_fd) {
|
|
|
|
|
return ReadbResult::IOPortNotified;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the wait_on_escape_ms value in response to the fish_escape_delay_ms user variable being
|
|
|
|
|
// set.
|
2023-10-08 23:22:27 +02:00
|
|
|
pub fn update_wait_on_escape_ms(vars: &EnvStack) {
|
|
|
|
|
let fish_escape_delay_ms = vars.get_unless_empty(L!("fish_escape_delay_ms"));
|
2023-11-25 14:37:42 -08:00
|
|
|
let Some(fish_escape_delay_ms) = fish_escape_delay_ms else {
|
2024-04-30 10:28:47 +02:00
|
|
|
WAIT_ON_ESCAPE_MS.store(WAIT_ON_ESCAPE_DEFAULT, Ordering::Relaxed);
|
2023-11-25 14:37:42 -08:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let fish_escape_delay_ms = fish_escape_delay_ms.as_string();
|
|
|
|
|
match fish_wcstol(&fish_escape_delay_ms) {
|
|
|
|
|
Ok(val) if (10..5000).contains(&val) => {
|
|
|
|
|
WAIT_ON_ESCAPE_MS.store(val.try_into().unwrap(), Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
eprintln!(
|
|
|
|
|
concat!(
|
|
|
|
|
"ignoring fish_escape_delay_ms: value '{}' ",
|
|
|
|
|
"is not an integer or is < 10 or >= 5000 ms"
|
|
|
|
|
),
|
|
|
|
|
fish_escape_delay_ms
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-08 23:22:27 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
// Update the wait_on_sequence_key_ms value in response to the fish_sequence_key_delay_ms user
|
|
|
|
|
// variable being set.
|
2023-10-08 23:22:27 +02:00
|
|
|
pub fn update_wait_on_sequence_key_ms(vars: &EnvStack) {
|
2023-11-25 14:37:42 -08:00
|
|
|
let sequence_key_time_ms = vars.get_unless_empty(L!("fish_sequence_key_delay_ms"));
|
|
|
|
|
let Some(sequence_key_time_ms) = sequence_key_time_ms else {
|
|
|
|
|
WAIT_ON_SEQUENCE_KEY_MS.store(WAIT_ON_SEQUENCE_KEY_INFINITE, Ordering::Relaxed);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let sequence_key_time_ms = sequence_key_time_ms.as_string();
|
|
|
|
|
match fish_wcstol(&sequence_key_time_ms) {
|
|
|
|
|
Ok(val) if (10..5000).contains(&val) => {
|
|
|
|
|
WAIT_ON_SEQUENCE_KEY_MS.store(val.try_into().unwrap(), Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
eprintln!(
|
|
|
|
|
concat!(
|
|
|
|
|
"ignoring fish_sequence_key_delay_ms: value '{}' ",
|
|
|
|
|
"is not an integer or is < 10 or >= 5000 ms"
|
|
|
|
|
),
|
|
|
|
|
sequence_key_time_ms
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 11:25:48 +02:00
|
|
|
static TERMINAL_PROTOCOLS: AtomicBool = AtomicBool::new(false);
|
2025-01-26 12:08:48 +01:00
|
|
|
static BRACKETED_PASTE: AtomicBool = AtomicBool::new(false);
|
2024-03-30 16:10:12 +01:00
|
|
|
|
2025-01-05 11:48:32 +01:00
|
|
|
pub(crate) static SCROLL_FORWARD_SUPPORTED: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
pub(crate) static CURSOR_UP_SUPPORTED: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
|
2025-01-26 12:08:48 +01:00
|
|
|
#[repr(u8)]
|
|
|
|
|
pub(crate) enum Capability {
|
|
|
|
|
Unknown,
|
|
|
|
|
Supported,
|
|
|
|
|
NotSupported,
|
|
|
|
|
}
|
|
|
|
|
pub(crate) static KITTY_KEYBOARD_SUPPORTED: AtomicU8 = AtomicU8::new(Capability::Unknown as _);
|
2025-01-25 16:07:15 +01:00
|
|
|
|
|
|
|
|
pub(crate) static SYNCHRONIZED_OUTPUT_SUPPORTED: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
|
|
|
|
|
pub(crate) static CURSOR_POSITION_REPORTING_SUPPORTED: RelaxedAtomicBool =
|
|
|
|
|
RelaxedAtomicBool::new(false);
|
2025-01-03 22:19:41 +01:00
|
|
|
|
2025-01-26 11:00:35 +01:00
|
|
|
pub fn kitty_progressive_enhancements_query() -> &'static [u8] {
|
|
|
|
|
if std::env::var_os("TERM").is_some_and(|term| term.as_os_str().as_bytes() == b"st-256color") {
|
|
|
|
|
return b"";
|
|
|
|
|
}
|
|
|
|
|
b"\x1b[?u"
|
|
|
|
|
}
|
2025-01-05 17:53:09 +01:00
|
|
|
|
2025-01-19 18:52:10 +01:00
|
|
|
static IS_TMUX: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
2024-10-08 23:34:39 +02:00
|
|
|
pub static IN_MIDNIGHT_COMMANDER_PRE_CSI_U: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
2024-10-12 10:49:03 +02:00
|
|
|
static IN_ITERM_PRE_CSI_U: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
|
|
|
|
|
pub fn terminal_protocol_hacks() {
|
|
|
|
|
use std::env::var_os;
|
|
|
|
|
IS_TMUX.store(var_os("TMUX").is_some());
|
|
|
|
|
IN_ITERM_PRE_CSI_U.store(
|
|
|
|
|
var_os("LC_TERMINAL").is_some_and(|term| term.as_os_str().as_bytes() == b"iTerm2")
|
|
|
|
|
&& var_os("LC_TERMINAL_VERSION").is_some_and(|version| {
|
|
|
|
|
let Some(version) = parse_version(&str2wcstring(version.as_os_str().as_bytes()))
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2025-01-05 20:36:08 +01:00
|
|
|
version < (99, 5, 6)
|
2024-10-12 10:49:03 +02:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_version(version: &wstr) -> Option<(i64, i64, i64)> {
|
|
|
|
|
let mut numbers = version.split('.');
|
|
|
|
|
let major = fish_wcstol(numbers.next()?).ok()?;
|
|
|
|
|
let minor = fish_wcstol(numbers.next()?).ok()?;
|
|
|
|
|
let patch = numbers.next()?;
|
|
|
|
|
let patch = &patch[..patch
|
|
|
|
|
.chars()
|
|
|
|
|
.position(|c| !c.is_ascii_digit())
|
|
|
|
|
.unwrap_or(patch.len())];
|
|
|
|
|
let patch = fish_wcstol(patch).ok()?;
|
|
|
|
|
Some((major, minor, patch))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_version() {
|
|
|
|
|
assert_eq!(parse_version(L!("3.5.2")), Some((3, 5, 2)));
|
|
|
|
|
assert_eq!(parse_version(L!("3.5.3beta")), Some((3, 5, 3)));
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
|
2024-06-25 19:54:38 +02:00
|
|
|
pub fn terminal_protocols_enable_ifn() {
|
2025-01-26 12:08:48 +01:00
|
|
|
let did_write = RelaxedAtomicBool::new(false);
|
|
|
|
|
let _save_screen_state = ScopeGuard::new((), |()| {
|
|
|
|
|
if did_write.load() {
|
|
|
|
|
reader_current_data().map(|data| data.save_screen_state());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if !BRACKETED_PASTE.load(Ordering::Relaxed) {
|
|
|
|
|
BRACKETED_PASTE.store(true, Ordering::Release);
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[?2004h");
|
|
|
|
|
if IS_TMUX.load() {
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, "\x1b[?1004h".as_bytes()); // focus reporting
|
|
|
|
|
}
|
|
|
|
|
did_write.store(true);
|
|
|
|
|
}
|
|
|
|
|
if IN_MIDNIGHT_COMMANDER_PRE_CSI_U.load() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let kitty_keyboard_supported = KITTY_KEYBOARD_SUPPORTED.load(Ordering::Relaxed);
|
|
|
|
|
if kitty_keyboard_supported == Capability::Unknown as _ {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-08 11:25:48 +02:00
|
|
|
if TERMINAL_PROTOCOLS.load(Ordering::Relaxed) {
|
2024-05-16 10:52:19 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-10-08 11:25:48 +02:00
|
|
|
TERMINAL_PROTOCOLS.store(true, Ordering::Release);
|
2025-01-26 12:08:48 +01:00
|
|
|
FLOG!(term_protocols, "Enabling extended keys");
|
|
|
|
|
if kitty_keyboard_supported == Capability::NotSupported as _ || IN_ITERM_PRE_CSI_U.load() {
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[>4;1m"); // XTerm's modifyOtherKeys
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b="); // set application keypad mode, so the keypad keys send unique codes
|
2024-10-08 11:25:48 +02:00
|
|
|
} else {
|
2025-01-26 12:08:48 +01:00
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[=5u"); // kitty progressive enhancements
|
2024-05-16 10:52:19 +02:00
|
|
|
}
|
2025-01-26 12:08:48 +01:00
|
|
|
did_write.store(true);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-08 11:25:48 +02:00
|
|
|
pub(crate) fn terminal_protocols_disable_ifn() {
|
2025-01-26 12:08:48 +01:00
|
|
|
let did_write = RelaxedAtomicBool::new(false);
|
|
|
|
|
let _save_screen_state = is_main_thread().then(|| {
|
|
|
|
|
ScopeGuard::new((), |()| {
|
|
|
|
|
if did_write.load() {
|
|
|
|
|
reader_current_data().map(|data| data.save_screen_state());
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
if BRACKETED_PASTE.load(Ordering::Acquire) {
|
|
|
|
|
BRACKETED_PASTE.store(false, Ordering::Release);
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[?2004l");
|
|
|
|
|
if IS_TMUX.load() {
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, "\x1b[?1004l".as_bytes());
|
|
|
|
|
}
|
|
|
|
|
did_write.store(true);
|
|
|
|
|
}
|
2024-10-08 11:25:48 +02:00
|
|
|
if !TERMINAL_PROTOCOLS.load(Ordering::Acquire) {
|
|
|
|
|
return;
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
2025-01-26 12:08:48 +01:00
|
|
|
FLOG_SAFE!(term_protocols, "Disabling extended keys");
|
|
|
|
|
let kitty_keyboard_supported = KITTY_KEYBOARD_SUPPORTED.load(Ordering::Acquire);
|
|
|
|
|
assert_ne!(kitty_keyboard_supported, Capability::Unknown as _);
|
|
|
|
|
if kitty_keyboard_supported == Capability::NotSupported as _ || IN_ITERM_PRE_CSI_U.load() {
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[>4;0m"); // XTerm's modifyOtherKeys
|
|
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b>"); // application keypad mode
|
2024-10-08 11:25:48 +02:00
|
|
|
} else {
|
2025-01-26 12:08:48 +01:00
|
|
|
let _ = write_loop(&STDOUT_FILENO, b"\x1b[=0u"); // kitty progressive enhancements
|
2024-10-12 09:00:24 +02:00
|
|
|
}
|
2024-10-08 11:25:48 +02:00
|
|
|
TERMINAL_PROTOCOLS.store(false, Ordering::Release);
|
2025-01-26 12:08:48 +01:00
|
|
|
did_write.store(true);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_mask(mask: u32) -> Modifiers {
|
|
|
|
|
Modifiers {
|
|
|
|
|
ctrl: (mask & 4) != 0,
|
|
|
|
|
alt: (mask & 2) != 0,
|
|
|
|
|
shift: (mask & 1) != 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 16:21:11 -07:00
|
|
|
// A data type used by the input machinery.
|
|
|
|
|
pub struct InputData {
|
|
|
|
|
// The file descriptor from which we read input, often stdin.
|
|
|
|
|
pub in_fd: RawFd,
|
|
|
|
|
|
|
|
|
|
// Queue of unread characters.
|
|
|
|
|
pub queue: VecDeque<CharEvent>,
|
|
|
|
|
|
|
|
|
|
// The current paste buffer, if any.
|
|
|
|
|
pub paste_buffer: Option<Vec<u8>>,
|
|
|
|
|
|
|
|
|
|
// The arguments to the most recently invoked input function.
|
|
|
|
|
pub input_function_args: Vec<char>,
|
|
|
|
|
|
|
|
|
|
// The return status of the most recently invoked input function.
|
|
|
|
|
pub function_status: bool,
|
|
|
|
|
|
|
|
|
|
// Transient storage to avoid repeated allocations.
|
|
|
|
|
pub event_storage: Vec<CharEvent>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InputData {
|
|
|
|
|
/// Construct from the fd from which to read.
|
|
|
|
|
pub fn new(in_fd: RawFd) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
in_fd,
|
|
|
|
|
queue: VecDeque::new(),
|
|
|
|
|
paste_buffer: None,
|
|
|
|
|
input_function_args: Vec::new(),
|
|
|
|
|
function_status: false,
|
|
|
|
|
event_storage: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Enqueue a char event to the queue of unread characters that input_readch will return before
|
|
|
|
|
/// actually reading from fd 0.
|
|
|
|
|
pub fn queue_char(&mut self, ch: CharEvent) {
|
|
|
|
|
self.queue.push_back(ch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the return status of the most recently executed input function.
|
|
|
|
|
pub fn function_set_status(&mut self, status: bool) {
|
|
|
|
|
self.function_status = status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 01:21:52 +01:00
|
|
|
#[derive(Eq, PartialEq)]
|
2025-01-25 16:07:15 +01:00
|
|
|
pub enum CursorPositionWait {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
MouseLeft(ViewportPosition),
|
2024-12-21 19:41:41 +01:00
|
|
|
ScrollbackPush,
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-04 01:21:52 +01:00
|
|
|
#[derive(Eq, PartialEq)]
|
2025-01-25 16:07:15 +01:00
|
|
|
pub enum Queried {
|
|
|
|
|
NotYet,
|
|
|
|
|
Once,
|
|
|
|
|
Twice,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
|
pub enum BlockingWait {
|
|
|
|
|
Startup(Queried),
|
|
|
|
|
CursorPosition(CursorPositionWait),
|
2025-01-04 01:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// A trait which knows how to produce a stream of input events.
|
|
|
|
|
/// Note this is conceptually a "base class" with override points.
|
|
|
|
|
pub trait InputEventQueuer {
|
2024-05-06 14:58:10 -05:00
|
|
|
/// Return the next event in the queue, or none if the queue is empty.
|
2023-11-25 14:37:42 -08:00
|
|
|
fn try_pop(&mut self) -> Option<CharEvent> {
|
2025-01-25 16:07:15 +01:00
|
|
|
if self.is_blocked() {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
match self.get_input_data().queue.front()? {
|
|
|
|
|
CharEvent::Key(_) | CharEvent::Readline(_) | CharEvent::Command(_) => {
|
2025-01-25 16:07:15 +01:00
|
|
|
return None; // No code execution while blocked.
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
}
|
|
|
|
|
CharEvent::Implicit(_) => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-26 16:21:11 -07:00
|
|
|
self.get_input_data_mut().queue.pop_front()
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 14:48:20 -06:00
|
|
|
/// An "infallible" version of [`try_readch`](Self::try_readch) to be used when the input pipe
|
|
|
|
|
/// fd is expected to outlive the input reader. Will panic upon EOF.
|
|
|
|
|
#[inline(always)]
|
2023-11-25 14:37:42 -08:00
|
|
|
fn readch(&mut self) -> CharEvent {
|
2024-11-20 14:48:20 -06:00
|
|
|
match self.try_readch(/*blocking*/ true) {
|
|
|
|
|
Some(c) => c,
|
|
|
|
|
None => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Function used by [`input_readch`] to read bytes from stdin until enough bytes have been read to
|
|
|
|
|
/// convert them to a wchar_t. Conversion is done using `mbrtowc`. If a character has previously
|
|
|
|
|
/// been read and then 'unread', that character is returned.
|
|
|
|
|
///
|
|
|
|
|
/// This is guaranteed to keep returning `Some(CharEvent)` so long as the input stream remains
|
|
|
|
|
/// open; `None` is only returned upon EOF as the main loop within blocks until input becomes
|
|
|
|
|
/// available.
|
|
|
|
|
///
|
|
|
|
|
/// This method is used directly by the fuzzing harness to avoid a panic on bounded inputs.
|
|
|
|
|
fn try_readch(&mut self, blocking: bool) -> Option<CharEvent> {
|
2023-11-25 14:37:42 -08:00
|
|
|
loop {
|
|
|
|
|
// Do we have something enqueued already?
|
|
|
|
|
// Note this may be initially true, or it may become true through calls to
|
|
|
|
|
// iothread_service_main() or env_universal_barrier() below.
|
|
|
|
|
if let Some(mevt) = self.try_pop() {
|
2024-11-20 14:48:20 -06:00
|
|
|
return Some(mevt);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We are going to block; but first allow any override to inject events.
|
|
|
|
|
self.prepare_to_select();
|
|
|
|
|
if let Some(mevt) = self.try_pop() {
|
2024-11-20 14:48:20 -06:00
|
|
|
return Some(mevt);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 14:48:20 -06:00
|
|
|
let rr = readb(self.get_in_fd(), blocking);
|
2023-11-25 14:37:42 -08:00
|
|
|
match rr {
|
|
|
|
|
ReadbResult::Eof => {
|
2024-12-29 19:31:15 +01:00
|
|
|
return Some(CharEvent::Implicit(ImplicitEvent::Eof));
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadbResult::Interrupted => {
|
|
|
|
|
self.select_interrupted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadbResult::UvarNotified => {
|
|
|
|
|
self.uvar_change_notified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadbResult::IOPortNotified => {
|
2024-05-27 12:04:08 -07:00
|
|
|
self.ioport_notified();
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadbResult::Byte(read_byte) => {
|
2024-03-30 16:10:12 +01:00
|
|
|
let mut have_escape_prefix = false;
|
|
|
|
|
let mut buffer = vec![read_byte];
|
|
|
|
|
let key_with_escape = if read_byte == 0x1b {
|
|
|
|
|
self.parse_escape_sequence(&mut buffer, &mut have_escape_prefix)
|
|
|
|
|
} else {
|
|
|
|
|
canonicalize_control_char(read_byte)
|
|
|
|
|
};
|
|
|
|
|
if self.paste_is_buffering() {
|
|
|
|
|
if read_byte != 0x1b {
|
|
|
|
|
self.paste_push_char(read_byte);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
continue;
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
let mut seq = WString::new();
|
|
|
|
|
let mut key = key_with_escape;
|
2024-04-03 19:56:46 +02:00
|
|
|
if key == Some(Key::from_raw(key::Invalid)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-12-30 08:12:32 +01:00
|
|
|
assert!(key.map_or(true, |key| key.codepoint != key::Invalid));
|
2024-03-30 16:10:12 +01:00
|
|
|
let mut consumed = 0;
|
2024-04-23 00:04:05 +02:00
|
|
|
let mut state = zero_mbstate();
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
let ok = loop {
|
|
|
|
|
if i == buffer.len() {
|
|
|
|
|
buffer.push(match readb(self.get_in_fd(), /*blocking=*/ true) {
|
|
|
|
|
ReadbResult::Byte(b) => b,
|
|
|
|
|
_ => 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
match self.parse_codepoint(
|
2024-03-30 16:10:12 +01:00
|
|
|
&mut state,
|
|
|
|
|
&mut key,
|
|
|
|
|
&mut seq,
|
|
|
|
|
&buffer,
|
|
|
|
|
i,
|
|
|
|
|
&mut consumed,
|
|
|
|
|
&mut have_escape_prefix,
|
2024-04-23 00:04:05 +02:00
|
|
|
) {
|
|
|
|
|
ControlFlow::Continue(codepoint_complete) => {
|
|
|
|
|
if codepoint_complete && i + 1 == buffer.len() {
|
|
|
|
|
break true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ControlFlow::Break(()) => {
|
|
|
|
|
break false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i += 1;
|
|
|
|
|
};
|
|
|
|
|
if !ok {
|
|
|
|
|
continue;
|
2024-02-27 22:25:54 +01:00
|
|
|
}
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
let (key_evt, extra) = if let Some(key) = key {
|
|
|
|
|
(CharEvent::from_key_seq(key, seq), None)
|
2024-03-30 16:10:12 +01:00
|
|
|
} else {
|
|
|
|
|
let Some(c) = seq.chars().next() else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
(
|
|
|
|
|
CharEvent::from_key_seq(Key::from_raw(c), seq.clone()),
|
|
|
|
|
Some(seq.chars().skip(1).map(CharEvent::from_char)),
|
|
|
|
|
)
|
2024-03-30 16:10:12 +01:00
|
|
|
};
|
2025-01-25 16:07:15 +01:00
|
|
|
if self.is_blocked() {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
2025-01-25 16:07:15 +01:00
|
|
|
"Still blocked on response from terminal, deferring key event",
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
key_evt
|
|
|
|
|
);
|
|
|
|
|
self.push_back(key_evt);
|
|
|
|
|
extra.map(|extra| {
|
|
|
|
|
for evt in extra {
|
|
|
|
|
self.push_back(evt);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let vintr = shell_modes().c_cc[libc::VINTR];
|
|
|
|
|
if vintr != 0 && key == Some(Key::from_single_byte(vintr)) {
|
|
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
2025-01-25 16:07:15 +01:00
|
|
|
"Received interrupt key, giving up waiting for response from terminal"
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
);
|
2025-01-25 16:07:15 +01:00
|
|
|
let ok = self.unblock_input();
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
assert!(ok);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
extra.map(|extra| self.insert_front(extra));
|
|
|
|
|
return Some(key_evt);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
2024-11-20 14:48:20 -06:00
|
|
|
ReadbResult::NothingToRead => return None,
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_readb(&mut self, buffer: &mut Vec<u8>) -> Option<u8> {
|
|
|
|
|
let ReadbResult::Byte(next) = readb(self.get_in_fd(), /*blocking=*/ false) else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
buffer.push(next);
|
|
|
|
|
Some(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_escape_sequence(
|
|
|
|
|
&mut self,
|
|
|
|
|
buffer: &mut Vec<u8>,
|
|
|
|
|
have_escape_prefix: &mut bool,
|
|
|
|
|
) -> Option<Key> {
|
2025-01-05 11:48:32 +01:00
|
|
|
assert!(buffer.len() <= 2);
|
|
|
|
|
let recursive_invocation = buffer.len() == 2;
|
2024-03-30 16:10:12 +01:00
|
|
|
let Some(next) = self.try_readb(buffer) else {
|
|
|
|
|
if !self.paste_is_buffering() {
|
2024-04-03 19:34:43 +02:00
|
|
|
return Some(Key::from_raw(key::Escape));
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
};
|
2024-12-30 07:47:54 +01:00
|
|
|
let invalid = Key::from_raw(key::Invalid);
|
2025-01-05 11:48:32 +01:00
|
|
|
if recursive_invocation && next == b'\x1b' {
|
2024-08-11 11:23:49 +02:00
|
|
|
return Some(
|
|
|
|
|
match self.parse_escape_sequence(buffer, have_escape_prefix) {
|
|
|
|
|
Some(mut nested_sequence) => {
|
2024-12-30 08:12:32 +01:00
|
|
|
if nested_sequence == invalid {
|
|
|
|
|
return Some(Key::from_raw(key::Escape));
|
|
|
|
|
}
|
2024-08-11 11:23:49 +02:00
|
|
|
nested_sequence.modifiers.alt = true;
|
|
|
|
|
nested_sequence
|
|
|
|
|
}
|
2024-12-30 08:12:32 +01:00
|
|
|
_ => invalid,
|
2024-08-11 11:23:49 +02:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
if next == b'[' {
|
|
|
|
|
// potential CSI
|
2024-12-30 07:47:54 +01:00
|
|
|
return Some(self.parse_csi(buffer).unwrap_or(invalid));
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
if next == b'O' {
|
|
|
|
|
// potential SS3
|
2024-12-30 07:47:54 +01:00
|
|
|
return Some(self.parse_ss3(buffer).unwrap_or(invalid));
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
2025-01-05 11:48:32 +01:00
|
|
|
if !recursive_invocation && next == b'P' {
|
|
|
|
|
// potential DCS
|
|
|
|
|
return Some(self.parse_dcs(buffer).unwrap_or(invalid));
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
match canonicalize_control_char(next) {
|
|
|
|
|
Some(mut key) => {
|
|
|
|
|
key.modifiers.alt = true;
|
|
|
|
|
Some(key)
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
*have_escape_prefix = true;
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_codepoint(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut mbstate_t,
|
|
|
|
|
out_key: &mut Option<Key>,
|
|
|
|
|
out_seq: &mut WString,
|
|
|
|
|
buffer: &[u8],
|
|
|
|
|
i: usize,
|
|
|
|
|
consumed: &mut usize,
|
|
|
|
|
have_escape_prefix: &mut bool,
|
2024-04-23 00:04:05 +02:00
|
|
|
) -> ControlFlow<(), bool> {
|
2024-03-30 16:10:12 +01:00
|
|
|
let mut res: char = '\0';
|
|
|
|
|
let read_byte = buffer[i];
|
|
|
|
|
if crate::libc::MB_CUR_MAX() == 1 {
|
|
|
|
|
// single-byte locale, all values are legal
|
|
|
|
|
// FIXME: this looks wrong, this falsely assumes that
|
|
|
|
|
// the single-byte locale is compatible with Unicode upper-ASCII.
|
|
|
|
|
res = read_byte.into();
|
|
|
|
|
out_seq.push(res);
|
2024-04-23 00:04:05 +02:00
|
|
|
return ControlFlow::Continue(true);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
let mut codepoint = u32::from(res);
|
|
|
|
|
let sz = unsafe {
|
|
|
|
|
mbrtowc(
|
|
|
|
|
std::ptr::addr_of_mut!(codepoint).cast(),
|
|
|
|
|
std::ptr::addr_of!(read_byte).cast(),
|
|
|
|
|
1,
|
|
|
|
|
state,
|
|
|
|
|
)
|
|
|
|
|
} as isize;
|
|
|
|
|
match sz {
|
|
|
|
|
-1 => {
|
|
|
|
|
FLOG!(reader, "Illegal input");
|
|
|
|
|
*consumed += 1;
|
|
|
|
|
self.push_front(CharEvent::from_check_exit());
|
2024-04-23 00:04:05 +02:00
|
|
|
return ControlFlow::Break(());
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
-2 => {
|
|
|
|
|
// Sequence not yet complete.
|
2024-04-23 00:04:05 +02:00
|
|
|
return ControlFlow::Continue(false);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
0 => {
|
|
|
|
|
// Actual nul char.
|
|
|
|
|
*consumed += 1;
|
|
|
|
|
out_seq.push('\0');
|
2024-04-23 00:04:05 +02:00
|
|
|
return ControlFlow::Continue(true);
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
if let Some(res) = char::from_u32(codepoint) {
|
|
|
|
|
// Sequence complete.
|
|
|
|
|
if !fish_reserved_codepoint(res) {
|
|
|
|
|
if *have_escape_prefix && i != 0 {
|
|
|
|
|
*have_escape_prefix = false;
|
|
|
|
|
*out_key = Some(alt(res));
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
*consumed += 1;
|
|
|
|
|
out_seq.push(res);
|
2024-04-23 00:04:05 +02:00
|
|
|
return ControlFlow::Continue(true);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
for &b in &buffer[*consumed..i] {
|
|
|
|
|
out_seq.push(encode_byte_to_char(b));
|
|
|
|
|
*consumed += 1;
|
|
|
|
|
}
|
2024-04-23 00:04:05 +02:00
|
|
|
ControlFlow::Continue(true)
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_csi(&mut self, buffer: &mut Vec<u8>) -> Option<Key> {
|
2024-10-24 10:36:00 -05:00
|
|
|
// The maximum number of CSI parameters is defined by NPAR, nominally 16.
|
2024-10-24 10:28:04 -05:00
|
|
|
let mut params = [[0_u32; 4]; 16];
|
2024-12-30 07:47:54 +01:00
|
|
|
let Some(mut c) = self.try_readb(buffer) else {
|
|
|
|
|
return Some(ctrl('['));
|
|
|
|
|
};
|
|
|
|
|
let mut next_char = |zelf: &mut Self| zelf.try_readb(buffer).unwrap_or(0xff);
|
2024-03-30 16:10:12 +01:00
|
|
|
let private_mode;
|
|
|
|
|
if matches!(c, b'?' | b'<' | b'=' | b'>') {
|
|
|
|
|
// private mode
|
|
|
|
|
private_mode = Some(c);
|
|
|
|
|
c = next_char(self);
|
|
|
|
|
} else {
|
|
|
|
|
private_mode = None;
|
|
|
|
|
}
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
let mut subcount = 0;
|
|
|
|
|
while count < 16 && c >= 0x30 && c <= 0x3f {
|
|
|
|
|
if c.is_ascii_digit() {
|
2024-11-20 14:53:39 -06:00
|
|
|
// Return None on invalid ascii numeric CSI parameter exceeding u32 bounds
|
|
|
|
|
params[count][subcount] = params[count][subcount]
|
|
|
|
|
.checked_mul(10)
|
|
|
|
|
.and_then(|result| result.checked_add(u32::from(c - b'0')))?;
|
2024-03-30 16:10:12 +01:00
|
|
|
} else if c == b':' && subcount < 3 {
|
|
|
|
|
subcount += 1;
|
|
|
|
|
} else if c == b';' {
|
|
|
|
|
count += 1;
|
|
|
|
|
subcount = 0;
|
|
|
|
|
} else {
|
2024-11-20 14:53:39 -06:00
|
|
|
// Unexpected character or unrecognized CSI
|
2024-03-30 16:10:12 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
c = next_char(self);
|
|
|
|
|
}
|
|
|
|
|
if c != b'$' && !(0x40..=0x7e).contains(&c) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let masked_key = |mut codepoint, shifted_codepoint| {
|
|
|
|
|
let mask = params[1][0].saturating_sub(1);
|
|
|
|
|
let mut modifiers = parse_mask(mask);
|
|
|
|
|
if let Some(shifted_codepoint) = shifted_codepoint {
|
|
|
|
|
if shifted_codepoint != '\0' && modifiers.shift {
|
|
|
|
|
modifiers.shift = false;
|
|
|
|
|
codepoint = shifted_codepoint;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Key {
|
|
|
|
|
modifiers,
|
|
|
|
|
codepoint,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let key = match c {
|
|
|
|
|
b'$' => {
|
2025-01-13 21:49:57 +01:00
|
|
|
if next_char(self) == b'y' {
|
|
|
|
|
if private_mode == Some(b'?') {
|
|
|
|
|
// DECRPM
|
|
|
|
|
if params[0][0] == 2026 && matches!(params[1][0], 1 | 2) {
|
2025-01-25 16:07:15 +01:00
|
|
|
FLOG!(reader, "Synchronized output is supported");
|
|
|
|
|
SYNCHRONIZED_OUTPUT_SUPPORTED.store(true);
|
2025-01-13 21:49:57 +01:00
|
|
|
}
|
2025-01-08 10:22:00 +01:00
|
|
|
}
|
2025-01-13 21:49:57 +01:00
|
|
|
// DECRQM
|
2024-03-30 16:10:12 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
match params[0][0] {
|
|
|
|
|
23 | 24 => shift(
|
|
|
|
|
char::from_u32(u32::from(function_key(11)) + params[0][0] - 23).unwrap(), // rxvt style
|
|
|
|
|
),
|
|
|
|
|
_ => return None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-14 15:39:53 +02:00
|
|
|
b'A' => masked_key(key::Up, None),
|
|
|
|
|
b'B' => masked_key(key::Down, None),
|
|
|
|
|
b'C' => masked_key(key::Right, None),
|
|
|
|
|
b'D' => masked_key(key::Left, None),
|
2024-03-30 16:10:12 +01:00
|
|
|
b'E' => masked_key('5', None), // Numeric keypad
|
|
|
|
|
b'F' => masked_key(key::End, None), // PC/xterm style
|
|
|
|
|
b'H' => masked_key(key::Home, None), // PC/xterm style
|
|
|
|
|
b'M' | b'm' => {
|
|
|
|
|
self.disable_mouse_tracking();
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
// Generic X10 or modified VT200 sequence, or extended (SGR/1006) mouse
|
|
|
|
|
// reporting mode, with semicolon-separated parameters for button code, Px,
|
|
|
|
|
// and Py, ending with 'M' for button press or 'm' for button release.
|
2024-03-30 16:10:12 +01:00
|
|
|
let sgr = private_mode == Some(b'<');
|
|
|
|
|
if !sgr && c == b'm' {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
let button = if sgr {
|
|
|
|
|
params[0][0]
|
|
|
|
|
} else {
|
|
|
|
|
u32::from(next_char(self)) - 32
|
|
|
|
|
};
|
|
|
|
|
let x = usize::try_from(
|
|
|
|
|
if sgr {
|
|
|
|
|
params[1][0]
|
|
|
|
|
} else {
|
|
|
|
|
u32::from(next_char(self)) - 32
|
|
|
|
|
} - 1,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let y = usize::try_from(
|
|
|
|
|
if sgr {
|
|
|
|
|
params[2][0]
|
|
|
|
|
} else {
|
|
|
|
|
u32::from(next_char(self)) - 32
|
|
|
|
|
} - 1,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let position = ViewportPosition { x, y };
|
|
|
|
|
let modifiers = parse_mask((button >> 2) & 0x07);
|
|
|
|
|
let code = button & 0x43;
|
|
|
|
|
if code != 0 || c != b'M' || modifiers.is_some() {
|
2024-03-30 16:10:12 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
2025-01-25 16:07:15 +01:00
|
|
|
let Some(wait) = self.blocking_wait() else {
|
|
|
|
|
self.on_mouse_left_click(position);
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
match wait {
|
|
|
|
|
BlockingWait::Startup(_) => {}
|
|
|
|
|
BlockingWait::CursorPosition(_) => {
|
2025-01-04 01:21:52 +01:00
|
|
|
// TODO: re-queue it I guess.
|
|
|
|
|
FLOG!(
|
2025-01-25 16:07:15 +01:00
|
|
|
reader,
|
|
|
|
|
"Ignoring mouse left click received while still waiting for Cursor Position Report"
|
|
|
|
|
);
|
2025-01-04 01:21:52 +01:00
|
|
|
}
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
b't' => {
|
|
|
|
|
self.disable_mouse_tracking();
|
|
|
|
|
// VT200 button released in mouse highlighting mode at valid text location. 5 chars.
|
|
|
|
|
let _ = next_char(self);
|
|
|
|
|
let _ = next_char(self);
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
b'T' => {
|
|
|
|
|
self.disable_mouse_tracking();
|
|
|
|
|
// VT200 button released in mouse highlighting mode past end-of-line. 9 characters.
|
2024-10-24 11:22:52 -05:00
|
|
|
for _ in 0..6 {
|
2024-03-30 16:10:12 +01:00
|
|
|
let _ = next_char(self);
|
|
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
b'P' => masked_key(function_key(1), None),
|
|
|
|
|
b'Q' => masked_key(function_key(2), None),
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
b'R' => {
|
|
|
|
|
let y = usize::try_from(params[0][0] - 1).unwrap();
|
|
|
|
|
let x = usize::try_from(params[1][0] - 1).unwrap();
|
|
|
|
|
FLOG!(reader, "Received cursor position report y:", y, "x:", x);
|
2025-01-25 16:07:15 +01:00
|
|
|
let Some(BlockingWait::CursorPosition(wait)) = self.blocking_wait() else {
|
|
|
|
|
CURSOR_POSITION_REPORTING_SUPPORTED.store(true);
|
|
|
|
|
return None;
|
2025-01-04 01:21:52 +01:00
|
|
|
};
|
2025-01-25 16:07:15 +01:00
|
|
|
let continuation = match wait {
|
|
|
|
|
CursorPositionWait::MouseLeft(click_position) => {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
ImplicitEvent::MouseLeftClickContinuation(
|
|
|
|
|
ViewportPosition { x, y },
|
|
|
|
|
*click_position,
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-01-25 16:07:15 +01:00
|
|
|
CursorPositionWait::ScrollbackPush => {
|
2024-12-21 19:41:41 +01:00
|
|
|
ImplicitEvent::ScrollbackPushContinuation(y)
|
|
|
|
|
}
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
};
|
|
|
|
|
self.push_front(CharEvent::Implicit(continuation));
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
b'S' => masked_key(function_key(4), None),
|
|
|
|
|
b'~' => match params[0][0] {
|
|
|
|
|
1 => masked_key(key::Home, None), // VT220/tmux style
|
|
|
|
|
2 => masked_key(key::Insert, None),
|
|
|
|
|
3 => masked_key(key::Delete, None),
|
|
|
|
|
4 => masked_key(key::End, None), // VT220/tmux style
|
|
|
|
|
5 => masked_key(key::PageUp, None),
|
|
|
|
|
6 => masked_key(key::PageDown, None),
|
|
|
|
|
7 => masked_key(key::Home, None), // rxvt style
|
|
|
|
|
8 => masked_key(key::End, None), // rxvt style
|
|
|
|
|
11..=15 => masked_key(
|
|
|
|
|
char::from_u32(u32::from(function_key(1)) + params[0][0] - 11).unwrap(),
|
|
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
17..=21 => masked_key(
|
|
|
|
|
char::from_u32(u32::from(function_key(6)) + params[0][0] - 17).unwrap(),
|
|
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
23 | 24 => masked_key(
|
|
|
|
|
char::from_u32(u32::from(function_key(11)) + params[0][0] - 23).unwrap(),
|
|
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
25 | 26 => {
|
|
|
|
|
shift(char::from_u32(u32::from(function_key(3)) + params[0][0] - 25).unwrap())
|
|
|
|
|
} // rxvt style
|
2024-08-11 10:32:51 +02:00
|
|
|
27 => {
|
|
|
|
|
let key =
|
|
|
|
|
canonicalize_keyed_control_char(char::from_u32(params[2][0]).unwrap());
|
|
|
|
|
masked_key(key, None)
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
28 | 29 => {
|
|
|
|
|
shift(char::from_u32(u32::from(function_key(5)) + params[0][0] - 28).unwrap())
|
|
|
|
|
} // rxvt style
|
|
|
|
|
31 | 32 => {
|
|
|
|
|
shift(char::from_u32(u32::from(function_key(7)) + params[0][0] - 31).unwrap())
|
|
|
|
|
} // rxvt style
|
|
|
|
|
33 | 34 => {
|
|
|
|
|
shift(char::from_u32(u32::from(function_key(9)) + params[0][0] - 33).unwrap())
|
|
|
|
|
} // rxvt style
|
|
|
|
|
200 => {
|
|
|
|
|
self.paste_start_buffering();
|
2024-12-30 07:47:54 +01:00
|
|
|
return None;
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
201 => {
|
|
|
|
|
self.paste_commit();
|
2024-12-30 07:47:54 +01:00
|
|
|
return None;
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
_ => return None,
|
|
|
|
|
},
|
2025-01-25 16:07:15 +01:00
|
|
|
b'c' if private_mode == Some(b'?') => {
|
|
|
|
|
self.push_front(CharEvent::Implicit(ImplicitEvent::PrimaryDeviceAttribute));
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
b'u' => {
|
2025-01-03 22:19:41 +01:00
|
|
|
if private_mode == Some(b'?') {
|
|
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
|
|
|
|
"Received kitty progressive enhancement flags, marking as supported"
|
|
|
|
|
);
|
2025-01-26 12:08:48 +01:00
|
|
|
KITTY_KEYBOARD_SUPPORTED.store(Capability::Supported as _, Ordering::Release);
|
2025-01-03 22:19:41 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
// Treat numpad keys the same as their non-numpad counterparts. Could add a numpad modifier here.
|
|
|
|
|
let key = match params[0][0] {
|
2025-01-01 21:32:40 +01:00
|
|
|
57361 => key::PrintScreen,
|
|
|
|
|
57363 => key::Menu,
|
2024-04-02 18:19:11 +02:00
|
|
|
57399 => '0',
|
|
|
|
|
57400 => '1',
|
|
|
|
|
57401 => '2',
|
|
|
|
|
57402 => '3',
|
|
|
|
|
57403 => '4',
|
|
|
|
|
57404 => '5',
|
|
|
|
|
57405 => '6',
|
|
|
|
|
57406 => '7',
|
|
|
|
|
57407 => '8',
|
|
|
|
|
57408 => '9',
|
|
|
|
|
57409 => '.',
|
|
|
|
|
57410 => '/',
|
|
|
|
|
57411 => '*',
|
|
|
|
|
57412 => '-',
|
|
|
|
|
57413 => '+',
|
2024-03-30 16:10:12 +01:00
|
|
|
57414 => key::Enter,
|
2024-04-02 18:19:11 +02:00
|
|
|
57415 => '=',
|
2024-03-30 16:10:12 +01:00
|
|
|
57417 => key::Left,
|
|
|
|
|
57418 => key::Right,
|
|
|
|
|
57419 => key::Up,
|
|
|
|
|
57420 => key::Down,
|
|
|
|
|
57421 => key::PageUp,
|
|
|
|
|
57422 => key::PageDown,
|
|
|
|
|
57423 => key::Home,
|
|
|
|
|
57424 => key::End,
|
|
|
|
|
57425 => key::Insert,
|
|
|
|
|
57426 => key::Delete,
|
|
|
|
|
cp => canonicalize_keyed_control_char(char::from_u32(cp).unwrap()),
|
|
|
|
|
};
|
|
|
|
|
masked_key(
|
|
|
|
|
key,
|
|
|
|
|
Some(canonicalize_keyed_control_char(
|
|
|
|
|
char::from_u32(params[0][1]).unwrap(),
|
|
|
|
|
)),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
b'Z' => shift(key::Tab),
|
|
|
|
|
b'I' => {
|
2024-12-29 19:31:15 +01:00
|
|
|
self.push_front(CharEvent::Implicit(ImplicitEvent::FocusIn));
|
2024-12-30 07:47:54 +01:00
|
|
|
return None;
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
b'O' => {
|
2024-12-29 19:31:15 +01:00
|
|
|
self.push_front(CharEvent::Implicit(ImplicitEvent::FocusOut));
|
2024-12-30 07:47:54 +01:00
|
|
|
return None;
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
_ => return None,
|
|
|
|
|
};
|
|
|
|
|
Some(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn disable_mouse_tracking(&mut self) {
|
|
|
|
|
// fish recognizes but does not actually support mouse reporting. We never turn it on, and
|
|
|
|
|
// it's only ever enabled if a program we spawned enabled it and crashed or forgot to turn
|
|
|
|
|
// it off before exiting. We turn it off here to avoid wasting resources.
|
|
|
|
|
//
|
|
|
|
|
// Since this is only called when we detect an incoming mouse reporting payload, we know the
|
|
|
|
|
// terminal emulator supports mouse reporting, so no terminfo checks.
|
|
|
|
|
FLOG!(reader, "Disabling mouse tracking");
|
|
|
|
|
|
|
|
|
|
// We shouldn't directly manipulate stdout from here, so we ask the reader to do it.
|
|
|
|
|
// writembs(outputter_t::stdoutput(), "\x1B[?1000l");
|
2024-12-29 19:31:15 +01:00
|
|
|
self.push_front(CharEvent::Implicit(ImplicitEvent::DisableMouseTracking));
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_ss3(&mut self, buffer: &mut Vec<u8>) -> Option<Key> {
|
|
|
|
|
let mut raw_mask = 0;
|
2024-12-30 07:47:54 +01:00
|
|
|
let Some(mut code) = self.try_readb(buffer) else {
|
|
|
|
|
return Some(alt('O'));
|
|
|
|
|
};
|
|
|
|
|
while (b'0'..=b'9').contains(&code) {
|
2024-03-30 16:10:12 +01:00
|
|
|
raw_mask = raw_mask * 10 + u32::from(code - b'0');
|
|
|
|
|
code = self.try_readb(buffer).unwrap_or(0xff);
|
|
|
|
|
}
|
|
|
|
|
let modifiers = parse_mask(raw_mask.saturating_sub(1));
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let key = match code {
|
|
|
|
|
b' ' => Key{modifiers, codepoint: key::Space},
|
2024-08-14 15:39:53 +02:00
|
|
|
b'A' => Key{modifiers, codepoint: key::Up},
|
|
|
|
|
b'B' => Key{modifiers, codepoint: key::Down},
|
|
|
|
|
b'C' => Key{modifiers, codepoint: key::Right},
|
|
|
|
|
b'D' => Key{modifiers, codepoint: key::Left},
|
2024-03-30 16:10:12 +01:00
|
|
|
b'F' => Key{modifiers, codepoint: key::End},
|
|
|
|
|
b'H' => Key{modifiers, codepoint: key::Home},
|
|
|
|
|
b'I' => Key{modifiers, codepoint: key::Tab},
|
|
|
|
|
b'M' => Key{modifiers, codepoint: key::Enter},
|
|
|
|
|
b'P' => Key{modifiers, codepoint: function_key(1)},
|
|
|
|
|
b'Q' => Key{modifiers, codepoint: function_key(2)},
|
|
|
|
|
b'R' => Key{modifiers, codepoint: function_key(3)},
|
|
|
|
|
b'S' => Key{modifiers, codepoint: function_key(4)},
|
|
|
|
|
b'X' => Key{modifiers, codepoint: '='},
|
|
|
|
|
b'j' => Key{modifiers, codepoint: '*'},
|
|
|
|
|
b'k' => Key{modifiers, codepoint: '+'},
|
|
|
|
|
b'l' => Key{modifiers, codepoint: ','},
|
|
|
|
|
b'm' => Key{modifiers, codepoint: '-'},
|
|
|
|
|
b'n' => Key{modifiers, codepoint: '.'},
|
|
|
|
|
b'o' => Key{modifiers, codepoint: '/'},
|
|
|
|
|
b'p' => Key{modifiers, codepoint: '0'},
|
|
|
|
|
b'q' => Key{modifiers, codepoint: '1'},
|
|
|
|
|
b'r' => Key{modifiers, codepoint: '2'},
|
|
|
|
|
b's' => Key{modifiers, codepoint: '3'},
|
|
|
|
|
b't' => Key{modifiers, codepoint: '4'},
|
|
|
|
|
b'u' => Key{modifiers, codepoint: '5'},
|
|
|
|
|
b'v' => Key{modifiers, codepoint: '6'},
|
|
|
|
|
b'w' => Key{modifiers, codepoint: '7'},
|
|
|
|
|
b'x' => Key{modifiers, codepoint: '8'},
|
|
|
|
|
b'y' => Key{modifiers, codepoint: '9'},
|
|
|
|
|
_ => return None,
|
|
|
|
|
};
|
|
|
|
|
Some(key)
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2025-01-05 11:48:32 +01:00
|
|
|
fn parse_dcs(&mut self, buffer: &mut Vec<u8>) -> Option<Key> {
|
|
|
|
|
assert!(buffer.len() == 2);
|
|
|
|
|
let Some(success) = self.try_readb(buffer) else {
|
|
|
|
|
return Some(alt('P'));
|
|
|
|
|
};
|
|
|
|
|
let success = match success {
|
|
|
|
|
b'0' => false,
|
|
|
|
|
b'1' => true,
|
|
|
|
|
_ => return None,
|
|
|
|
|
};
|
|
|
|
|
if self.try_readb(buffer)? != b'+' {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
if self.try_readb(buffer)? != b'r' {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
while self.try_readb(buffer)? != b'\x1b' {}
|
|
|
|
|
if self.try_readb(buffer)? != b'\\' {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
buffer.pop();
|
|
|
|
|
buffer.pop();
|
2025-01-06 21:09:41 +01:00
|
|
|
// \e P 1 r + Pn ST
|
|
|
|
|
// \e P 0 r + msg ST
|
|
|
|
|
let buffer = &buffer[5..];
|
2025-01-05 11:48:32 +01:00
|
|
|
if !success {
|
2025-01-06 21:09:41 +01:00
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
|
|
|
|
format!(
|
|
|
|
|
"Received XTGETTCAP failure response: {}",
|
|
|
|
|
str2wcstring(&parse_hex(buffer)?),
|
|
|
|
|
)
|
|
|
|
|
);
|
2025-01-05 11:48:32 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
2025-01-06 21:09:41 +01:00
|
|
|
let mut buffer = buffer.splitn(2, |&c| c == b'=');
|
2025-01-05 11:48:32 +01:00
|
|
|
let key = buffer.next().unwrap();
|
|
|
|
|
let value = buffer.next()?;
|
|
|
|
|
let key = parse_hex(key)?;
|
|
|
|
|
let value = parse_hex(value)?;
|
|
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
|
|
|
|
format!(
|
|
|
|
|
"Received XTGETTCAP response: {}={:?}",
|
|
|
|
|
str2wcstring(&key),
|
|
|
|
|
str2wcstring(&value)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
if key == b"indn" && matches!(&value[..], b"\x1b[%p1%dS" | b"\\E[%p1%dS") {
|
|
|
|
|
SCROLL_FORWARD_SUPPORTED.store(true);
|
|
|
|
|
FLOG!(reader, "Scroll forward is supported");
|
|
|
|
|
}
|
|
|
|
|
if key == b"cuu" && matches!(&value[..], b"\x1b[%p1%dA" | b"\\E[%p1%dA") {
|
|
|
|
|
CURSOR_UP_SUPPORTED.store(true);
|
|
|
|
|
FLOG!(reader, "Cursor up is supported");
|
|
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 10:28:47 +02:00
|
|
|
fn readch_timed_esc(&mut self) -> Option<CharEvent> {
|
2023-11-25 14:37:42 -08:00
|
|
|
self.readch_timed(WAIT_ON_ESCAPE_MS.load(Ordering::Relaxed))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn readch_timed_sequence_key(&mut self) -> Option<CharEvent> {
|
|
|
|
|
let wait_on_sequence_key_ms = WAIT_ON_SEQUENCE_KEY_MS.load(Ordering::Relaxed);
|
|
|
|
|
if wait_on_sequence_key_ms == WAIT_ON_SEQUENCE_KEY_INFINITE {
|
|
|
|
|
return Some(self.readch());
|
|
|
|
|
}
|
|
|
|
|
self.readch_timed(wait_on_sequence_key_ms)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Like readch(), except it will wait at most wait_time_ms milliseconds for a
|
|
|
|
|
/// character to be available for reading.
|
2024-05-06 14:58:10 -05:00
|
|
|
/// Return None on timeout, the event on success.
|
2023-11-25 14:37:42 -08:00
|
|
|
fn readch_timed(&mut self, wait_time_ms: usize) -> Option<CharEvent> {
|
|
|
|
|
if let Some(evt) = self.try_pop() {
|
|
|
|
|
return Some(evt);
|
|
|
|
|
}
|
2024-05-16 10:52:19 +02:00
|
|
|
terminal_protocols_enable_ifn();
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
// We are not prepared to handle a signal immediately; we only want to know if we get input on
|
|
|
|
|
// our fd before the timeout. Use pselect to block all signals; we will handle signals
|
|
|
|
|
// before the next call to readch().
|
|
|
|
|
let mut sigs: libc::sigset_t = unsafe { std::mem::zeroed() };
|
|
|
|
|
unsafe { libc::sigfillset(&mut sigs) };
|
|
|
|
|
|
|
|
|
|
// pselect expects timeouts in nanoseconds.
|
|
|
|
|
const NSEC_PER_MSEC: u64 = 1000 * 1000;
|
|
|
|
|
const NSEC_PER_SEC: u64 = NSEC_PER_MSEC * 1000;
|
|
|
|
|
let wait_nsec: u64 = (wait_time_ms as u64) * NSEC_PER_MSEC;
|
Revert libc time_t changes
This was based on a misunderstanding.
On musl, 64-bit time_t on 32-bit architectures was introduced in version 1.2.0,
by introducing new symbols. The old symbols still exist, to allow programs compiled against older versions
to keep running on 1.2.0+, preserving ABI-compatibility. (see musl commit 38143339646a4ccce8afe298c34467767c899f51)
Programs compiled against 1.2.0+ will get the new symbols, and will therefore think time_t is 64-bit.
Unfortunately, rust's libc crate uses its own definition of these types, and does not check for musl version.
Currently, it includes the pre-1.2.0 32-bit type.
That means:
- If you run on a 32-bit system like i686
- ... and compile against a C-library other than libc
- ... and pass it a time_t-containing struct like timespec or stat
... you need to arrange for that library to be built against musl <1.2.0.
Or, as https://github.com/ericonr/rust-time64 says:
> Therefore, for "old" 32-bit targets (riscv32 is supposed to default to time64),
> any Rust code that interacts with C code built on musl after 1.2.0,
> using types based on time_t (arguably, the main ones are struct timespec and struct stat) in their interface,
> will be completely miscompiled.
However, while fish runs on i686 and compiles against pcre2, we do not pass pcre2 a time_t.
Our only uses of time_t are confined to interactions with libc, in which case with musl we would simply use the legacy ABI.
I have compiled an i686 fish against musl to confirm and can find no issue.
This reverts commit 55196ee2a0430d920ea7a2c89a6e322615f78334.
This reverts commit 4992f8896633fb8ca8d89e09f02330cd49395485.
This reverts commit 46c8ba2c9fec77195091ddcf7ee0bb3d9a6e5f54.
This reverts commit 3a9b4149da7d44b8648702f17d9e9eef651e56f9.
This reverts commit 5f9e9cbe741025231acfb24dc900433e1c6738ac.
This reverts commit 338579b78ca2ba0aab108304bc33a53fddeb11ba.
This reverts commit d19e5508d7b406da6813edb9d0a6909094d20e5a.
This reverts commit b64045dc189ec58b6bd3dea71e1441e00876904c.
Closes #10634
2024-08-27 11:15:27 +02:00
|
|
|
let timeout = libc::timespec {
|
2023-11-25 14:37:42 -08:00
|
|
|
tv_sec: (wait_nsec / NSEC_PER_SEC).try_into().unwrap(),
|
|
|
|
|
tv_nsec: (wait_nsec % NSEC_PER_SEC).try_into().unwrap(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// We have one fd of interest.
|
|
|
|
|
let mut fdset: libc::fd_set = unsafe { std::mem::zeroed() };
|
|
|
|
|
let in_fd = self.get_in_fd();
|
|
|
|
|
unsafe {
|
|
|
|
|
libc::FD_ZERO(&mut fdset);
|
|
|
|
|
libc::FD_SET(in_fd, &mut fdset);
|
|
|
|
|
};
|
Revert libc time_t changes
This was based on a misunderstanding.
On musl, 64-bit time_t on 32-bit architectures was introduced in version 1.2.0,
by introducing new symbols. The old symbols still exist, to allow programs compiled against older versions
to keep running on 1.2.0+, preserving ABI-compatibility. (see musl commit 38143339646a4ccce8afe298c34467767c899f51)
Programs compiled against 1.2.0+ will get the new symbols, and will therefore think time_t is 64-bit.
Unfortunately, rust's libc crate uses its own definition of these types, and does not check for musl version.
Currently, it includes the pre-1.2.0 32-bit type.
That means:
- If you run on a 32-bit system like i686
- ... and compile against a C-library other than libc
- ... and pass it a time_t-containing struct like timespec or stat
... you need to arrange for that library to be built against musl <1.2.0.
Or, as https://github.com/ericonr/rust-time64 says:
> Therefore, for "old" 32-bit targets (riscv32 is supposed to default to time64),
> any Rust code that interacts with C code built on musl after 1.2.0,
> using types based on time_t (arguably, the main ones are struct timespec and struct stat) in their interface,
> will be completely miscompiled.
However, while fish runs on i686 and compiles against pcre2, we do not pass pcre2 a time_t.
Our only uses of time_t are confined to interactions with libc, in which case with musl we would simply use the legacy ABI.
I have compiled an i686 fish against musl to confirm and can find no issue.
This reverts commit 55196ee2a0430d920ea7a2c89a6e322615f78334.
This reverts commit 4992f8896633fb8ca8d89e09f02330cd49395485.
This reverts commit 46c8ba2c9fec77195091ddcf7ee0bb3d9a6e5f54.
This reverts commit 3a9b4149da7d44b8648702f17d9e9eef651e56f9.
This reverts commit 5f9e9cbe741025231acfb24dc900433e1c6738ac.
This reverts commit 338579b78ca2ba0aab108304bc33a53fddeb11ba.
This reverts commit d19e5508d7b406da6813edb9d0a6909094d20e5a.
This reverts commit b64045dc189ec58b6bd3dea71e1441e00876904c.
Closes #10634
2024-08-27 11:15:27 +02:00
|
|
|
let res = unsafe {
|
|
|
|
|
libc::pselect(
|
|
|
|
|
in_fd + 1,
|
|
|
|
|
&mut fdset,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
&timeout,
|
|
|
|
|
&sigs,
|
|
|
|
|
)
|
|
|
|
|
};
|
2023-11-25 14:37:42 -08:00
|
|
|
|
|
|
|
|
// Prevent signal starvation on WSL causing the `torn_escapes.py` test to fail
|
2024-05-20 14:06:50 -05:00
|
|
|
if is_windows_subsystem_for_linux(WSL::V1) {
|
2023-11-25 14:37:42 -08:00
|
|
|
// Merely querying the current thread's sigmask is sufficient to deliver a pending signal
|
|
|
|
|
let _ = unsafe { libc::pthread_sigmask(0, ptr::null(), &mut sigs) };
|
|
|
|
|
}
|
|
|
|
|
if res > 0 {
|
|
|
|
|
return Some(self.readch());
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 16:21:11 -07:00
|
|
|
/// Return the fd from which to read.
|
|
|
|
|
fn get_in_fd(&self) -> RawFd {
|
|
|
|
|
self.get_input_data().in_fd
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
|
2024-05-26 16:21:11 -07:00
|
|
|
/// Return the input data. This is to be implemented by the concrete type.
|
|
|
|
|
fn get_input_data(&self) -> &InputData;
|
|
|
|
|
fn get_input_data_mut(&mut self) -> &mut InputData;
|
2023-11-25 14:37:42 -08:00
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
// Support for "bracketed paste"
|
|
|
|
|
// The way it works is that we acknowledge our support by printing
|
|
|
|
|
// \e\[?2004h
|
|
|
|
|
// then the terminal will "bracket" every paste in
|
|
|
|
|
// \e\[200~ and \e\[201~
|
|
|
|
|
// Every character in between those two will be part of the paste and should not cause a binding to execute (like \n executing commands).
|
|
|
|
|
//
|
|
|
|
|
// We enable it after every command and disable it before, see the terminal protocols logic.
|
|
|
|
|
//
|
|
|
|
|
// Support for this seems to be ubiquitous - emacs enables it unconditionally (!) since 25.1
|
|
|
|
|
// (though it only supports it since then, it seems to be the last term to gain support).
|
|
|
|
|
//
|
|
|
|
|
// See http://thejh.net/misc/website-terminal-copy-paste.
|
2024-05-26 16:21:11 -07:00
|
|
|
|
|
|
|
|
fn paste_start_buffering(&mut self) {
|
|
|
|
|
self.get_input_data_mut().paste_buffer = Some(Vec::new());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn paste_is_buffering(&self) -> bool {
|
|
|
|
|
self.get_input_data().paste_buffer.is_some()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn paste_push_char(&mut self, b: u8) {
|
|
|
|
|
self.get_input_data_mut()
|
|
|
|
|
.paste_buffer
|
|
|
|
|
.as_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.push(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn paste_commit(&mut self) {
|
|
|
|
|
self.get_input_data_mut().paste_buffer = None;
|
|
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// Enqueue a character or a readline function to the queue of unread characters that
|
|
|
|
|
/// readch will return before actually reading from fd 0.
|
|
|
|
|
fn push_back(&mut self, ch: CharEvent) {
|
2024-05-26 16:21:11 -07:00
|
|
|
self.get_input_data_mut().queue.push_back(ch);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add a character or a readline function to the front of the queue of unread characters. This
|
|
|
|
|
/// will be the next character returned by readch.
|
|
|
|
|
fn push_front(&mut self, ch: CharEvent) {
|
2024-05-26 16:21:11 -07:00
|
|
|
self.get_input_data_mut().queue.push_front(ch);
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Find the first sequence of non-char events, and promote them to the front.
|
|
|
|
|
fn promote_interruptions_to_front(&mut self) {
|
|
|
|
|
// Find the first sequence of non-char events.
|
|
|
|
|
// EOF is considered a char: we don't want to pull EOF in front of real chars.
|
2024-05-26 16:21:11 -07:00
|
|
|
let queue = &mut self.get_input_data_mut().queue;
|
2024-12-29 19:31:15 +01:00
|
|
|
let is_char = |evt: &CharEvent| {
|
|
|
|
|
evt.is_char() || matches!(evt, CharEvent::Implicit(ImplicitEvent::Eof))
|
|
|
|
|
};
|
2023-11-25 14:37:42 -08:00
|
|
|
// Find the index of the first non-char event.
|
|
|
|
|
// If there's none, we're done.
|
|
|
|
|
let Some(first): Option<usize> = queue.iter().position(|e| !is_char(e)) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let last = queue
|
|
|
|
|
.range(first..)
|
|
|
|
|
.position(is_char)
|
|
|
|
|
.map_or(queue.len(), |x| x + first);
|
|
|
|
|
// Move the non-char events to the front, retaining their order.
|
|
|
|
|
let elems: Vec<CharEvent> = queue.drain(first..last).collect();
|
|
|
|
|
for elem in elems.into_iter().rev() {
|
|
|
|
|
queue.push_front(elem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add multiple readline events to the front of the queue of unread characters.
|
|
|
|
|
/// The order of the provided events is not changed, i.e. they are not inserted in reverse
|
|
|
|
|
/// order. That is, the first element in evts will be the first element returned.
|
|
|
|
|
fn insert_front<I>(&mut self, evts: I)
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = CharEvent>,
|
|
|
|
|
I::IntoIter: DoubleEndedIterator,
|
|
|
|
|
{
|
2024-05-26 16:21:11 -07:00
|
|
|
let queue = &mut self.get_input_data_mut().queue;
|
2023-11-25 14:37:42 -08:00
|
|
|
let iter = evts.into_iter().rev();
|
|
|
|
|
queue.reserve(iter.size_hint().0);
|
|
|
|
|
for evt in iter {
|
|
|
|
|
queue.push_front(evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Forget all enqueued readline events in the front of the queue.
|
|
|
|
|
fn drop_leading_readline_events(&mut self) {
|
2024-05-26 16:21:11 -07:00
|
|
|
let queue = &mut self.get_input_data_mut().queue;
|
2023-11-25 14:37:42 -08:00
|
|
|
while let Some(evt) = queue.front() {
|
2024-03-02 07:58:01 +01:00
|
|
|
if evt.is_readline_or_command() {
|
2023-11-25 14:37:42 -08:00
|
|
|
queue.pop_front();
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 16:07:15 +01:00
|
|
|
fn blocking_wait(&self) -> Option<&BlockingWait> {
|
|
|
|
|
None
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
}
|
2025-01-25 16:07:15 +01:00
|
|
|
fn is_blocked(&self) -> bool {
|
2025-01-04 01:21:52 +01:00
|
|
|
false
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
}
|
2025-01-25 16:07:15 +01:00
|
|
|
fn unblock_input(&mut self) -> bool {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
false
|
|
|
|
|
}
|
2025-01-25 16:07:15 +01:00
|
|
|
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
fn on_mouse_left_click(&mut self, _position: ViewportPosition) {}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// Override point for when we are about to (potentially) block in select(). The default does
|
|
|
|
|
/// nothing.
|
|
|
|
|
fn prepare_to_select(&mut self) {}
|
|
|
|
|
|
2024-03-30 16:10:12 +01:00
|
|
|
/// Called when select() is interrupted by a signal.
|
2024-05-26 16:21:11 -07:00
|
|
|
fn select_interrupted(&mut self) {}
|
2023-11-25 14:37:42 -08:00
|
|
|
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
fn enqueue_interrupt_key(&mut self) {
|
|
|
|
|
let vintr = shell_modes().c_cc[libc::VINTR];
|
|
|
|
|
if vintr != 0 {
|
|
|
|
|
let interrupt_evt = CharEvent::from_key(Key::from_single_byte(vintr));
|
2025-01-25 16:07:15 +01:00
|
|
|
if self.unblock_input() {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
FLOG!(
|
|
|
|
|
reader,
|
2025-01-25 16:07:15 +01:00
|
|
|
"Received interrupt, giving up on waiting for terminal response"
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
);
|
|
|
|
|
self.push_back(interrupt_evt);
|
|
|
|
|
} else {
|
|
|
|
|
self.push_front(interrupt_evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 14:37:42 -08:00
|
|
|
/// Override point for when when select() is interrupted by the universal variable notifier.
|
|
|
|
|
/// The default does nothing.
|
|
|
|
|
fn uvar_change_notified(&mut self) {}
|
|
|
|
|
|
2024-05-27 12:04:08 -07:00
|
|
|
/// Override point for when the ioport is ready.
|
|
|
|
|
/// The default does nothing.
|
|
|
|
|
fn ioport_notified(&mut self) {}
|
|
|
|
|
|
2024-05-26 16:21:11 -07:00
|
|
|
/// Reset the function status.
|
|
|
|
|
fn get_function_status(&self) -> bool {
|
|
|
|
|
self.get_input_data().function_status
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:58:10 -05:00
|
|
|
/// Return if we have any lookahead.
|
2023-11-25 14:37:42 -08:00
|
|
|
fn has_lookahead(&self) -> bool {
|
2024-05-26 16:21:11 -07:00
|
|
|
!self.get_input_data().queue.is_empty()
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A simple, concrete implementation of InputEventQueuer.
|
|
|
|
|
pub struct InputEventQueue {
|
2024-05-26 16:21:11 -07:00
|
|
|
data: InputData,
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InputEventQueue {
|
|
|
|
|
pub fn new(in_fd: RawFd) -> InputEventQueue {
|
|
|
|
|
InputEventQueue {
|
2024-05-26 16:21:11 -07:00
|
|
|
data: InputData::new(in_fd),
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InputEventQueuer for InputEventQueue {
|
2024-05-26 16:21:11 -07:00
|
|
|
fn get_input_data(&self) -> &InputData {
|
|
|
|
|
&self.data
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 16:21:11 -07:00
|
|
|
fn get_input_data_mut(&mut self) -> &mut InputData {
|
|
|
|
|
&mut self.data
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2024-03-30 16:10:12 +01:00
|
|
|
|
|
|
|
|
fn select_interrupted(&mut self) {
|
|
|
|
|
if reader_test_and_clear_interrupted() != 0 {
|
Move cursor on mouse click via kitty's OSC 133 click_events=1
When the user clicks somewhere in the prompt, kitty asks the shell
to move the cursor there (since there is not much else to do).
This is currently implemented by sending an array of
forward-char-passive commands. This has problems, for example it
is really slow on large command lines (probably because we repaint
everytime).
Implement kitty's `click_events=1` flag to set the
position directly. To convert from terminal-coordinates
to fish-coordinates, query [CSI 6 n Report Cursor
Position](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)
and use it to compute the left prompt's terminal-coordinates (which
are (0, 0) in fish-coordinates).
Unfortunately this doesn't yet work correctly while the terminal
is scrolled. This is probably because the cursor position is wrong
if off-screen. To fix that we could probably record the cursor
position while not scrolled, but it doesn't seem terribly important
(the existing implementation also doesn't get it right).
We still turn off mouse reporting. If we turned it on, it
would be harder to select text in the terminal itself (not fish).
This would typically mean that mouse-drag will alter fish's
selection and shift+mouse-drag or alt+mouse-drag can be used.
To improve this, we could try to synchronize the selection: if parts
of the fish commandline are selected in the terminal's selection,
copy that to fish's selection and vice versa.
Or maybe there is an intuitive criteria, like: whenever we receive a
mouse event outside fish, turn off mouse reporting, and turn it back on
whenver we receive new keyboard input. One problem is that we lose
one event (though we could send it back to the terminal). Another
problem is we would turn it back on too late in some scenarios.
Closes #10932
2024-12-21 19:41:41 +01:00
|
|
|
self.enqueue_interrupt_key();
|
2024-03-30 16:10:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-25 14:37:42 -08:00
|
|
|
}
|
2025-01-05 11:48:32 +01:00
|
|
|
|
|
|
|
|
fn parse_hex(hex: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
|
if hex.len() % 2 != 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let mut result = vec![0; hex.len() / 2];
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
while i < hex.len() {
|
|
|
|
|
let d1 = char::from(hex[i]).to_digit(16)?;
|
|
|
|
|
let d2 = char::from(hex[i + 1]).to_digit(16)?;
|
|
|
|
|
let decoded = u8::try_from(16 * d1 + d2).unwrap();
|
|
|
|
|
result[i / 2] = decoded;
|
|
|
|
|
i += 2;
|
|
|
|
|
}
|
|
|
|
|
Some(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_hex() {
|
|
|
|
|
assert_eq!(parse_hex(&[b'3', b'd']), Some(vec![61]));
|
|
|
|
|
}
|