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::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
14 use windows_sys::Win32::System::Console::{
15 GetConsoleMode, GetStdHandle, SetConsoleCP, SetConsoleMode, SetConsoleOutputCP,
16 ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_ERROR_HANDLE, STD_OUTPUT_HANDLE,
17 };
18 const CP_UTF8: u32 = 65001;
19
20 // SAFETY: Win32 console functions are safe to call from a single-threaded
21 // context before any output occurs. GetStdHandle returns
22 // INVALID_HANDLE_VALUE on failure (checked below); SetConsoleMode failure
23 // is silently tolerated so the CLI degrades to plain text.
24 // G29 (v1.0.68): HANDLE was `isize` in windows-sys <= 0.52 and became
25 // `*mut c_void` in >= 0.59; the previous comparison `handle != 0 &&
26 // handle as isize != -1` only worked for the old type and now fails
27 // compilation. Replaced with the type-safe idiom `!handle.is_null() &&
28 // handle != INVALID_HANDLE_VALUE`, which works for both type eras and
29 // also catches the distinct INVALID_HANDLE_VALUE sentinel ((HANDLE)-1).
30 unsafe {
31 SetConsoleOutputCP(CP_UTF8);
32 SetConsoleCP(CP_UTF8);
33
34 for handle_id in [STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
35 let handle: HANDLE = GetStdHandle(handle_id);
36 if !handle.is_null() && handle != INVALID_HANDLE_VALUE {
37 let mut mode: u32 = 0;
38 if GetConsoleMode(handle, &mut mode) != 0 {
39 let _ = SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
40 }
41 }
42 }
43 }
44}
45
46/// Returns whether ANSI escape codes should be emitted to stderr.
47///
48/// Precedence:
49/// 1. `NO_COLOR` set (any value) → false (<https://no-color.org> standard)
50/// 2. `CLICOLOR_FORCE=1` → true (force colors even without TTY)
51/// 3. stderr is a terminal → true
52/// 4. fallback → false
53pub fn should_use_ansi() -> bool {
54 if std::env::var_os("NO_COLOR").is_some() {
55 return false;
56 }
57 if std::env::var("CLICOLOR_FORCE").ok().as_deref() == Some("1") {
58 return true;
59 }
60 std::io::IsTerminal::is_terminal(&std::io::stderr())
61}