Skip to content

Commit

Permalink
fix(query): windows: explicitly open CONIN/CONOUT when I/O is not a t…
Browse files Browse the repository at this point in the history
…erminal

This change explicitly opens CONIN/CONOUT when either in/out files
is not a terminal. Otherwise, the background color query fails because
the streams are not console devices.

Fixes: charmbracelet/sequin#28
  • Loading branch information
aymanbagabas committed Nov 22, 2024
1 parent 8448a9b commit 8f4aab7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ func backgroundColor(in *os.File, out *os.File) (color.Color, error) {
// Bubble Tea, listen for tea.BackgroundColorMsg in your update function.
func BackgroundColor(in *os.File, out *os.File) (bg color.Color, err error) {
if runtime.GOOS == "windows" {

Check failure on line 35 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

`if runtime.GOOS == "windows"` has complex nested blocks (complexity: 6) (nestif)

Check failure on line 35 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

`if runtime.GOOS == "windows"` has complex nested blocks (complexity: 6) (nestif)

Check failure on line 35 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

`if runtime.GOOS == "windows"` has complex nested blocks (complexity: 6) (nestif)
// On Windows, when the input/output is redirected or piped, we need to
// open the console explicitly.
// See https://learn.microsoft.com/en-us/windows/console/getstdhandle#remarks
if !term.IsTerminal(in.Fd()) {
f, err := os.OpenFile("CONIN$", os.O_RDWR, 0o644)

Check failure on line 40 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 0o644, in <argument> detected (mnd)

Check failure on line 40 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 0o644, in <argument> detected (mnd)

Check failure on line 40 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 0o644, in <argument> detected (mnd)
if err != nil {
return nil, fmt.Errorf("error opening CONIN$: %w", err)
}
in = f
}
if !term.IsTerminal(out.Fd()) {
f, err := os.OpenFile("CONOUT$", os.O_RDWR, 0o644)

Check failure on line 47 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 0o644, in <argument> detected (mnd)

Check failure on line 47 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 0o644, in <argument> detected (mnd)

Check failure on line 47 in query.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 0o644, in <argument> detected (mnd)
if err != nil {
return nil, fmt.Errorf("error opening CONOUT$: %w", err)
}
out = f
}
return backgroundColor(in, out)
}

Expand Down

0 comments on commit 8f4aab7

Please sign in to comment.