From 5d827c76546a08000296a541a325cd26675054bd Mon Sep 17 00:00:00 2001 From: pagedmov Date: Fri, 6 Mar 2026 00:46:43 -0500 Subject: [PATCH] added a slight debounce to bell bytes sent to the terminal --- src/main.rs | 6 +++--- src/parse/execute.rs | 2 +- src/readline/term.rs | 30 +++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index bee2e16..55f0c60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,9 +223,9 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> { readline.reset_active_widget(false)?; } ShErrKind::CleanExit(code) => { - QUIT_CODE.store(*code, Ordering::SeqCst); - return Ok(()); - } + QUIT_CODE.store(*code, Ordering::SeqCst); + return Ok(()); + } _ => e.print_error(), } } diff --git a/src/parse/execute.rs b/src/parse/execute.rs index 37dd8e8..ed8dd31 100644 --- a/src/parse/execute.rs +++ b/src/parse/execute.rs @@ -313,7 +313,7 @@ impl Dispatcher { Ok(()) } fn exec_subsh(&mut self, subsh: Node) -> ShResult<()> { - let blame = subsh.get_span().clone(); + let _blame = subsh.get_span().clone(); let NdRule::Command { assignments, argv } = subsh.class else { unreachable!() }; diff --git a/src/readline/term.rs b/src/readline/term.rs index 37dc9dc..9943622 100644 --- a/src/readline/term.rs +++ b/src/readline/term.rs @@ -4,7 +4,7 @@ use std::{ fmt::{Debug, Write}, io::{BufRead, BufReader, Read}, os::fd::{AsFd, BorrowedFd, RawFd}, - sync::Arc, + sync::Arc, time::Instant, }; use nix::{ @@ -822,6 +822,7 @@ impl Default for Layout { } pub struct TermWriter { + last_bell: Option, out: RawFd, pub t_cols: Col, // terminal width buffer: String, @@ -831,6 +832,7 @@ impl TermWriter { pub fn new(out: RawFd) -> Self { let (t_cols, _) = get_win_size(out); Self { + last_bell: None, out, t_cols, buffer: String::new(), @@ -1067,10 +1069,24 @@ impl LineWriter for TermWriter { Ok(()) } - fn send_bell(&mut self) -> ShResult<()> { - if read_shopts(|o| o.core.bell_enabled) { - self.flush_write("\x07")?; - } - Ok(()) - } + fn send_bell(&mut self) -> ShResult<()> { + if read_shopts(|o| o.core.bell_enabled) { + // we use a cooldown because I don't like having my ears assaulted by 1 million bells + // whenever i finish clearing the line using backspace. + let now = Instant::now(); + + // surprisingly, a fixed cooldown like '100' is actually more annoying than 1 million bells. + // I've found this range of 50-150 to be the best balance + let cooldown = rand::random_range(50..150); + let should_send = match self.last_bell { + None => true, + Some(time) => now.duration_since(time).as_millis() > cooldown, + }; + if should_send { + self.flush_write("\x07")?; + self.last_bell = Some(now); + } + } + Ok(()) + } }