Skip to main content

talon_cli/
platform.rs

1//! Platform initialization and terminal detection.
2
3/// Initializes platform-specific terminal behavior.
4#[cfg(not(windows))]
5pub const fn start() {}
6
7/// Initializes platform-specific terminal behavior.
8#[cfg(windows)]
9pub fn start() {
10    start_windows();
11}
12
13#[cfg(windows)]
14#[expect(
15    unsafe_code,
16    reason = "configuring the Windows console codepage requires a Win32 API call"
17)]
18fn start_windows() {
19    use windows_sys::Win32::System::Console::SetConsoleOutputCP;
20    // SAFETY: `SetConsoleOutputCP` takes a codepage id and does not dereference pointers.
21    let result = unsafe { SetConsoleOutputCP(65_001) };
22    if result == 0 {
23        tracing::warn!("failed to configure Windows console output as UTF-8");
24    }
25}
26
27/// Returns whether stdout is connected to an interactive terminal.
28#[must_use]
29pub fn stdout_is_tty() -> bool {
30    use std::io::IsTerminal;
31    std::io::stdout().is_terminal()
32}
33
34/// Returns whether stderr is connected to an interactive terminal.
35#[must_use]
36pub fn stderr_is_tty() -> bool {
37    use std::io::IsTerminal;
38    std::io::stderr().is_terminal()
39}
40
41/// Returns whether the user permits ANSI color.
42#[must_use]
43pub fn user_accepts_ansi_color() -> bool {
44    if std::env::var_os("NO_COLOR").is_some() {
45        return false;
46    }
47    std::env::var("CLICOLOR").as_deref() != Ok("0")
48}