Skip to content

Conversation

caarlos0
Copy link
Member

@caarlos0 caarlos0 commented Jul 1, 2025

fixes #50

Copilot

This comment was marked as outdated.

help.go Outdated
Comment on lines 113 to 115
fmt.Fprintln(w, err.Error())
return
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return implies usage errors won't be displayed to user.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me rephrase: it might be OK, but then an inline comment expanding the reason would be present

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's on purpose, it'll still show the error, just not the try --help thing

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tge confirmation

Then an inline comment would be appreciated

@caarlos0 caarlos0 requested a review from Copilot July 1, 2025 20:17
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes #50 by detecting when stderr is not a terminal (TTY) and falling back to plain error output instead of styled output.

  • Imported github.com/charmbracelet/x/term and updated the ErrorHandler doc to note terminal-only styling.
  • Added a TTY check around styled error handling in Execute.
  • Prints unstyled errors when stderr is redirected or piped (non-TTY).
Comments suppressed due to low confidence (1)

fang.go:163

  • Add unit tests covering both branches (TTY vs. non-TTY) to ensure plain error output is used when stderr is redirected and styled output when running in a terminal.
			if !term.IsTerminal(w.Fd()) {

fang.go Outdated
Comment on lines 160 to 166
if w, ok := root.ErrOrStderr().(term.File); ok {
// if stderr is not a tty, simply print the error without any
// styling or going through an [ErrorHandler]:
if !term.IsTerminal(w.Fd()) {
fmt.Fprintln(w, err.Error())
return err
}
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type assertion to term.File is too strict and may miss other io.Writer implementations wrapping *os.File. Consider asserting an interface with Fd() (e.g., interface{ Fd() uintptr }) or checking for *os.File directly to reliably detect a terminal descriptor.

Suggested change
if w, ok := root.ErrOrStderr().(term.File); ok {
// if stderr is not a tty, simply print the error without any
// styling or going through an [ErrorHandler]:
if !term.IsTerminal(w.Fd()) {
fmt.Fprintln(w, err.Error())
return err
}
if w, ok := root.ErrOrStderr().(interface{ Fd() uintptr }); ok {
// if stderr is not a tty, simply print the error without any
// styling or going through an [ErrorHandler]:
if !term.IsTerminal(w.Fd()) {
fmt.Fprintln(w, err.Error())
return err
}
} else if f, ok := root.ErrOrStderr().(*os.File); ok {
// fallback check for *os.File
if !term.IsTerminal(f.Fd()) {
fmt.Fprintln(f, err.Error())
return err
}

Copilot uses AI. Check for mistakes.

Comment on lines 22 to 26
// ErrorHandler handles an error, printing them to the given [io.Writer].
//
// Note that this will only be used if the STDERR is a terminal, and should
// be used for styling only.
type ErrorHandler = func(w io.Writer, styles Styles, err error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two remarks

  • Is there a need for this to be exported?

  • you are not using it in your PR, right? Except in a comment I don't see it.

Why having it then?

@caarlos0 caarlos0 merged commit 4d07c80 into main Jul 2, 2025
18 checks passed
@caarlos0 caarlos0 deleted the tty branch July 2, 2025 00:58
@FalcoSuessgott
Copy link

What an elegant solution thanks for implementing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

consider adding an env var or global persistent flag for parsable error output
4 participants