1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Terminal capability queries -- no extra dependencies needed.
//!
//! These helpers answer the three questions every CLI asks before emitting
//! ANSI escapes: *Am I a terminal? How big? Should I use color?* They use
//! only `libc` (already a dependency) and standard environment variables, so
//! consumers don't need to pull in `atty`, `terminal_size`, or
//! `supports-color`.
//!
//! For the terminal dimensions while an editor is active, prefer
//! [`EditLine::terminal_size`](crate::EditLine::terminal_size) -- it uses the
//! same underlying syscall but is documented alongside the other editor
//! methods.
/// Returns `true` if file descriptor `fd` is connected to a terminal (TTY).
///
/// Common usage: `is_tty(1)` for stdout, `is_tty(0)` for stdin.
///
/// This is a thin wrapper over `libc::isatty`. Rust's
/// [`std::io::IsTerminal`] trait provides the same check on `Stdout` /
/// `Stdin` handles; this function is useful when you have a raw fd (e.g.
/// from libedit's stream) or want to avoid importing `std::io`.
/// Returns `true` if the terminal likely supports ANSI color output.
///
/// Checks, in order:
/// 1. **`$NO_COLOR`** -- if set (to any value), returns `false`.
/// See <https://no-color.org/>.
/// 2. **stdout is not a TTY** -- returns `false` (piped output shouldn't
/// contain escapes).
/// 3. **`$TERM` is `"dumb"`** -- returns `false`.
/// 4. Otherwise returns `true`.
///
/// This covers the standard conventions and is sufficient for nearly all
/// real terminals (iTerm2, Terminal.app, xterm, Alacritty, Windows Terminal,
/// etc.). It does not query termcap/terminfo for the `Co` capability -- that
/// would require additional FFI and is rarely needed in practice.
/// Query the terminal dimensions `(columns, rows)` for the given fd.
///
/// Returns `None` if the fd is not a terminal or the ioctl fails. For a
/// convenient fallback-included version, see
/// [`EditLine::terminal_size`](crate::EditLine::terminal_size).