Skip to main content

sqlite_graphrag/
terminal.rs

1//! Cross-platform terminal initialization: UTF-8, ANSI colors, NO_COLOR.
2
3/// Initializes the console for correct UTF-8 output and ANSI escape
4/// support.  On non-Windows platforms this is a no-op because modern
5/// Unix terminals handle both natively.
6pub fn init_console() {
7    #[cfg(windows)]
8    init_windows_console();
9}
10
11#[cfg(windows)]
12fn init_windows_console() {
13    use windows_sys::Win32::System::Console::{
14        GetConsoleMode, GetStdHandle, SetConsoleCP, SetConsoleMode, SetConsoleOutputCP,
15        ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_ERROR_HANDLE, STD_OUTPUT_HANDLE,
16    };
17    const CP_UTF8: u32 = 65001;
18
19    // SAFETY: Win32 console functions are safe to call from a single-threaded
20    // context before any output occurs.  GetStdHandle returns
21    // INVALID_HANDLE_VALUE on failure (checked below); SetConsoleMode failure
22    // is silently tolerated so the CLI degrades to plain text.
23    unsafe {
24        SetConsoleOutputCP(CP_UTF8);
25        SetConsoleCP(CP_UTF8);
26
27        for handle_id in [STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
28            let handle = GetStdHandle(handle_id);
29            if handle != 0 && handle as isize != -1 {
30                let mut mode: u32 = 0;
31                if GetConsoleMode(handle, &mut mode) != 0 {
32                    let _ = SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
33                }
34            }
35        }
36    }
37}
38
39/// Returns whether ANSI escape codes should be emitted to stderr.
40///
41/// Precedence:
42/// 1. `NO_COLOR` set (any value) → false (<https://no-color.org> standard)
43/// 2. `CLICOLOR_FORCE=1` → true (force colors even without TTY)
44/// 3. stderr is a terminal → true
45/// 4. fallback → false
46pub fn should_use_ansi() -> bool {
47    if std::env::var_os("NO_COLOR").is_some() {
48        return false;
49    }
50    if std::env::var("CLICOLOR_FORCE").ok().as_deref() == Some("1") {
51        return true;
52    }
53    std::io::IsTerminal::is_terminal(&std::io::stderr())
54}