termstatus: centralize OutputIsTerminal checks

This commit is contained in:
Michael Eischer 2025-09-14 17:58:52 +02:00
parent c745e4221e
commit 1ae2d08d1b
14 changed files with 85 additions and 68 deletions

View file

@ -10,18 +10,11 @@ func StdinIsTerminal() bool {
return term.IsTerminal(int(os.Stdin.Fd()))
}
func StdoutIsTerminal() bool {
func OutputIsTerminal(fd uintptr) bool {
// mintty on windows can use pipes which behave like a posix terminal,
// but which are not a terminal handle
return term.IsTerminal(int(os.Stdout.Fd())) || StdoutCanUpdateStatus()
}
func StdoutCanUpdateStatus() bool {
return CanUpdateStatus(os.Stdout.Fd())
}
func StdoutWidth() int {
return Width(os.Stdout.Fd())
// but which are not a terminal handle. Thus also check `CanUpdateStatus`,
// which is able to detect such pipes.
return term.IsTerminal(int(fd)) || CanUpdateStatus(fd)
}
func Width(fd uintptr) int {