Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ windows = { version = "0.52.0", features = [
"Win32_Foundation",
"Win32_Media_Audio",
"Win32_Media_Audio_Endpoints",
"Win32_System_Console",
"Win32_UI_WindowsAndMessaging",
] }
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ use timers::AudibleTimers;
mod options;
#[cfg(target_os = "windows")]
mod windows_volume;
#[cfg(target_os = "windows")]
mod windows_focus;
use tracing::{debug, error, info, instrument, warn};
use tracing_appender::rolling::{RollingFileAppender, Rotation};

Expand Down Expand Up @@ -611,6 +613,18 @@ fn call_fn(
Some(format!("Current voice is {}", name))
}

"focus_terminal" => {
#[cfg(target_os = "windows")]
{
windows_focus::bring_terminal_to_front();
None
}
#[cfg(not(target_os = "windows"))]
{
Some("focus_terminal is only supported on Windows".to_string())
}
}

"list_output_devices" => {
let default = get_default_output_device().unwrap_or_else(|| "Unknown".to_string());
let mut devices = list_audio_output_devices();
Expand Down Expand Up @@ -1576,6 +1590,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
}))
.build().unwrap(),

ChatCompletionFunctionsArgs::default()
.name("focus_terminal")
.description("Brings the console window to the foreground on Windows.")
.parameters(json!({
"type": "object",
"properties": {},
"required": [],
}))
.build().unwrap(),

ChatCompletionFunctionsArgs::default()
.name("get_location")
.description("Returns an approximate location based on the machine's IP address.")
Expand Down
43 changes: 43 additions & 0 deletions src/windows_focus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[cfg(target_os = "windows")]
use windows::Win32::Foundation::HWND;
#[cfg(target_os = "windows")]
use windows::Win32::System::Console::GetConsoleWindow;
#[cfg(target_os = "windows")]
use windows::Win32::UI::WindowsAndMessaging::{SetForegroundWindow, ShowWindow, SW_RESTORE};

#[cfg(target_os = "windows")]
/// Brings the console window to the foreground on Windows.
pub fn bring_terminal_to_front() {
// Skip if running inside VS Code's integrated terminal to avoid spawning a new window
if let Ok(term) = std::env::var("TERM_PROGRAM") {
if term.to_lowercase().contains("vscode") {
return;
}
}

unsafe {
let hwnd: HWND = GetConsoleWindow();
if hwnd.0 != 0 {
// Restore the window in case it is minimized
ShowWindow(hwnd, SW_RESTORE);
// Attempt to bring the window to the foreground
let _ = SetForegroundWindow(hwnd);
}
}
}

#[cfg(not(target_os = "windows"))]
/// Stub for non-Windows platforms.
pub fn bring_terminal_to_front() {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn call_bring_terminal_to_front() {
// The function should simply run without panicking on all platforms.
bring_terminal_to_front();
}
}