Skip to content

Commit 0942b01

Browse files
committed
feat(chat): Add terminal bell notifications
Add terminal bell notifications when responses are ready or spinner drops. 🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
1 parent 3e61cd8 commit 0942b01

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

crates/q_cli/src/cli/chat/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ use crate::cli::chat::parse::{
102102
};
103103
use crate::util::region_check;
104104
use crate::util::token_counter::TokenCounter;
105+
use crate::util::spinner::play_notification_bell;
105106

106107
const WELCOME_TEXT: &str = color_print::cstr! {"
107108
@@ -1686,7 +1687,10 @@ where
16861687
)
16871688
.await;
16881689
}
1690+
16891691
if self.interactive {
1692+
// Play notification bell when response is complete
1693+
play_notification_bell();
16901694
queue!(self.output, style::ResetColor, style::SetAttribute(Attribute::Reset))?;
16911695
execute!(self.output, style::Print("\n"))?;
16921696

crates/q_cli/src/util/spinner.rs

+51-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use std::sync::mpsc::{
77
TryRecvError,
88
channel,
99
};
10-
use std::thread;
1110
use std::thread::JoinHandle;
1211
use std::time::Duration;
12+
use std::{
13+
env,
14+
thread,
15+
};
1316

1417
use anstream::{
1518
print,
@@ -49,6 +52,53 @@ pub enum SpinnerComponent {
4952
Spinner,
5053
}
5154

55+
/// Play the terminal bell notification sound
56+
/// This is a separate function to make it easier to call from multiple places
57+
pub fn play_notification_bell() {
58+
// Check if we should play the bell based on terminal type
59+
if should_play_bell() {
60+
print!("\x07"); // ASCII bell character
61+
stdout().flush().unwrap();
62+
}
63+
}
64+
65+
/// Determine if we should play the bell based on terminal type
66+
fn should_play_bell() -> bool {
67+
// Get the TERM environment variable
68+
if let Ok(term) = env::var("TERM") {
69+
// List of terminals known to handle bell character well
70+
let bell_compatible_terms = [
71+
"xterm",
72+
"xterm-256color",
73+
"screen",
74+
"screen-256color",
75+
"tmux",
76+
"tmux-256color",
77+
"rxvt",
78+
"rxvt-unicode",
79+
"linux",
80+
"konsole",
81+
"gnome",
82+
"gnome-256color",
83+
"alacritty",
84+
"iterm2",
85+
];
86+
87+
// Check if the current terminal is in the compatible list
88+
for compatible_term in bell_compatible_terms.iter() {
89+
if term.starts_with(compatible_term) {
90+
return true;
91+
}
92+
}
93+
94+
// For other terminals, don't play the bell
95+
return false;
96+
}
97+
98+
// If TERM is not set, default to not playing the bell
99+
false
100+
}
101+
52102
impl Spinner {
53103
pub fn new(components: Vec<SpinnerComponent>) -> Self {
54104
let (sender, recv) = channel::<Option<String>>();

0 commit comments

Comments
 (0)