Skip to content

Commit

Permalink
Merge pull request #759 from andreistan26/enable-signal
Browse files Browse the repository at this point in the history
Add enable signals config option
  • Loading branch information
gwenn committed Feb 17, 2024
2 parents 485eef8 + a4962f7 commit 3284f43
Show file tree
Hide file tree
Showing 6 changed files with 51 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 @@ -619,6 +619,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 @@ -232,6 +232,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 @@ -187,6 +187,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 @@ -1274,6 +1274,7 @@ pub struct PosixTerminal {
// external print writer
pipe_writer: Option<PipeWriter>,
sigwinch: Option<SigWinCh>,
enable_signals: bool,
}

impl PosixTerminal {
Expand Down Expand Up @@ -1301,6 +1302,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 @@ -1349,6 +1351,7 @@ impl Term for PosixTerminal {
pipe_reader: None,
pipe_writer: None,
sigwinch,
enable_signals,
})
}

Expand All @@ -1375,7 +1378,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 @@ -1534,7 +1537,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 @@ -1556,6 +1559,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 @@ -1592,7 +1600,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 @@ -1607,6 +1615,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
1 change: 1 addition & 0 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ impl Term for Console {
_tab_stop: usize,
bell_style: BellStyle,
_enable_bracketed_paste: bool,
_enable_signals: bool,
) -> Result<Console> {
let (conin, conout, close_on_drop) = if behavior == Behavior::PreferTerm {
if let (Ok(conin), Ok(conout)) = (
Expand Down

0 comments on commit 3284f43

Please sign in to comment.