Skip to content

Commit

Permalink
Add enable signals config option
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Stan <[email protected]>
  • Loading branch information
andreistan26 committed Jan 13, 2024
1 parent c01855a commit d5d51ee
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct Config {
check_cursor_position: bool,
/// Bracketed paste on unix platform
enable_bracketed_paste: bool,
/// Whether to disable or not the signals in termios
enable_signals: bool,
}

impl Config {
Expand Down Expand Up @@ -193,6 +195,18 @@ impl Config {
pub fn enable_bracketed_paste(&self) -> bool {
self.enable_bracketed_paste
}

/// Enable or disable signals in termios
///
/// By default, it's disabled.
#[must_use]
pub fn enable_signals(&self) -> bool {
self.enable_signals
}

pub(crate) fn set_enable_signals(&mut self, enable_signals: bool) {
self.enable_signals = enable_signals;
}
}

impl Default for Config {
Expand All @@ -213,6 +227,7 @@ impl Default for Config {
indent_size: 2,
check_cursor_position: false,
enable_bracketed_paste: true,
enable_signals: false,
}
}
}
Expand Down Expand Up @@ -450,6 +465,15 @@ impl Builder {
self
}

/// Enable or disable signals in termios
///
/// By default, it's disabled.
#[must_use]
pub fn enable_signals(mut self, enable_signals: bool) -> Self {
self.p.set_enable_signals(enable_signals);
self
}

/// Builds a `Config` with the settings specified so far.
#[must_use]
pub fn build(self) -> Config {
Expand Down Expand Up @@ -567,4 +591,11 @@ pub trait Configurer {
fn enable_bracketed_paste(&mut self, enabled: bool) {
self.config_mut().enable_bracketed_paste = enabled;
}

/// Enable or disable signals in termios
///
/// By default, it's disabled.
fn set_enable_signals(&mut self, enable_signals: bool) {
self.config_mut().set_enable_signals(enable_signals);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ impl<H: Helper, I: History> Editor<H, I> {
config.tab_stop(),
config.bell_style(),
config.enable_bracketed_paste(),
config.enable_signals(),
)?;
Ok(Self {
term,
Expand Down
1 change: 1 addition & 0 deletions src/tty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub trait Term {
tab_stop: usize,
bell_style: BellStyle,
enable_bracketed_paste: bool,
enable_signals: bool,
) -> Result<Self>
where
Self: Sized;
Expand Down
1 change: 1 addition & 0 deletions src/tty/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl Term for DummyTerminal {
_tab_stop: usize,
bell_style: BellStyle,
_enable_bracketed_paste: bool,
_enable_signals: bool,
) -> Result<DummyTerminal> {
Ok(DummyTerminal {
keys: Vec::new(),
Expand Down
19 changes: 16 additions & 3 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ pub struct PosixTerminal {
// external print writer
pipe_writer: Option<PipeWriter>,
sigwinch: Option<SigWinCh>,
enable_signals: bool,
}

impl PosixTerminal {
Expand All @@ -1266,6 +1267,7 @@ impl Term for PosixTerminal {
tab_stop: usize,
bell_style: BellStyle,
enable_bracketed_paste: bool,
enable_signals: bool,
) -> Result<Self> {
let (tty_in, is_in_a_tty, tty_out, is_out_a_tty, close_on_drop) =
if behavior == Behavior::PreferTerm {
Expand Down Expand Up @@ -1314,6 +1316,7 @@ impl Term for PosixTerminal {
pipe_reader: None,
pipe_writer: None,
sigwinch,
enable_signals,
})
}

Expand All @@ -1340,7 +1343,7 @@ impl Term for PosixTerminal {
if !self.is_in_a_tty {
return Err(ENOTTY.into());
}
let (original_mode, key_map) = termios_::enable_raw_mode(self.tty_in)?;
let (original_mode, key_map) = termios_::enable_raw_mode(self.tty_in, self.enable_signals)?;

self.raw_mode.store(true, Ordering::SeqCst);
// enable bracketed paste
Expand Down Expand Up @@ -1493,7 +1496,7 @@ mod termios_ {
let fd = unsafe { BorrowedFd::borrow_raw(tty_in) };
Ok(termios::tcsetattr(fd, SetArg::TCSADRAIN, termios)?)
}
pub fn enable_raw_mode(tty_in: RawFd) -> Result<(Termios, PosixKeyMap)> {
pub fn enable_raw_mode(tty_in: RawFd, enable_signals: bool) -> Result<(Termios, PosixKeyMap)> {
use nix::sys::termios::{ControlFlags, InputFlags, LocalFlags};

let fd = unsafe { BorrowedFd::borrow_raw(tty_in) };
Expand All @@ -1515,6 +1518,11 @@ mod termios_ {
// disable echoing, canonical mode, extended input processing and signals
raw.local_flags &=
!(LocalFlags::ECHO | LocalFlags::ICANON | LocalFlags::IEXTEN | LocalFlags::ISIG);

if enable_signals {
raw.local_flags |= LocalFlags::ISIG;
}

raw.control_chars[SCI::VMIN as usize] = 1; // One character-at-a-time input
raw.control_chars[SCI::VTIME as usize] = 0; // with blocking read

Expand Down Expand Up @@ -1551,7 +1559,7 @@ mod termios_ {
pub fn disable_raw_mode(tty_in: RawFd, termios: &Termios) -> Result<()> {
Ok(termios::tcsetattr(tty_in, termios::TCSADRAIN, termios)?)
}
pub fn enable_raw_mode(tty_in: RawFd) -> Result<(Termios, PosixKeyMap)> {
pub fn enable_raw_mode(tty_in: RawFd, enable_signals: bool) -> Result<(Termios, PosixKeyMap)> {
let original_mode = Termios::from_fd(tty_in)?;
let mut raw = original_mode;
// disable BREAK interrupt, CR to NL conversion on input,
Expand All @@ -1566,6 +1574,11 @@ mod termios_ {
raw.c_cflag |= termios::CS8;
// disable echoing, canonical mode, extended input processing and signals
raw.c_lflag &= !(termios::ECHO | termios::ICANON | termios::IEXTEN | termios::ISIG);

if enable_signals {
raw.c_lflag |= termios::ISIG;
}

raw.c_cc[termios::VMIN] = 1; // One character-at-a-time input
raw.c_cc[termios::VTIME] = 0; // with blocking read

Expand Down

0 comments on commit d5d51ee

Please sign in to comment.