MCPcopy Create free account
hub / github.com/coder/slog / isTTY

Function isTTY

internal/entryhuman/entry.go:366–390  ·  view source on GitHub ↗

isTTY checks whether the given writer is a *os.File TTY.

(w io.Writer)

Source from the content-addressed store, hash-verified

364
365// isTTY checks whether the given writer is a *os.File TTY.
366func isTTY(w io.Writer) bool {
367 if w == forceColorWriter {
368 return true
369 }
370 // SyscallConn is safe during file close.
371 if sc, ok := w.(interface {
372 SyscallConn() (syscall.RawConn, error)
373 }); ok {
374 conn, err := sc.SyscallConn()
375 if err != nil {
376 return false
377 }
378 var isTerm bool
379 err = conn.Control(func(fd uintptr) {
380 isTerm = term.IsTerminal(int(fd))
381 })
382 if err != nil {
383 return false
384 }
385 return isTerm
386 }
387 // Fallback to unsafe Fd.
388 f, ok := w.(interface{ Fd() uintptr })
389 return ok && term.IsTerminal(int(f.Fd()))
390}
391
392func shouldColor(w io.Writer) bool {
393 return isTTY(w) || os.Getenv("FORCE_COLOR") != ""

Callers 1

shouldColorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected