Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output: improve Windows console handling #274

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

All notable changes to `src-cli` are documented in this file.

## Unreleased changes

### Fixed

- `src campaigns` output has been improved in the Windows console. [#274](https://github.com/sourcegraph/src-cli/pull/274)

## 3.18.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4
gopkg.in/yaml.v2 v2.3.0
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
)
Expand Down
13 changes: 12 additions & 1 deletion internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ type OutputOpts struct {
Verbose bool
}

// newOutputPlatformQuirks provides a way for conditionally compiled code to
// hook into NewOutput to perform any required setup.
var newOutputPlatformQuirks func(o *Output) error

func NewOutput(w io.Writer, opts OutputOpts) *Output {
caps := detectCapabilities()
if opts.ForceColor {
caps.Color = true
}

return &Output{caps: caps, opts: opts, w: w}
o := &Output{caps: caps, opts: opts, w: w}
if newOutputPlatformQuirks != nil {
if err := newOutputPlatformQuirks(o); err != nil {
o.Verbosef("Error handling platform quirks: %v", err)
}
}

return o
}

func (o *Output) Verbose(s string) {
Expand Down
36 changes: 36 additions & 0 deletions internal/output/output_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package output

import (
"github.com/hashicorp/go-multierror"
"golang.org/x/sys/windows"
)

func init() {
newOutputPlatformQuirks = func(o *Output) error {
var errs *multierror.Error

if err := setConsoleMode(windows.Stdout, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil {
errs = multierror.Append(errs, err)
}
if err := setConsoleMode(windows.Stderr, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil {
errs = multierror.Append(errs, err)
}

return errs.ErrorOrNil()
}
}

func setConsoleMode(handle windows.Handle, flags uint32) error {
// This is shamelessly lifted from gitlab-runner, specifically
// https://gitlab.com/gitlab-org/gitlab-runner/blob/f8d87f1e3e3af1cc8aadcea3e40bbb069eee72ef/helpers/cli/init_cli_windows.go

// First we have to get the current console mode so we can add the desired
// flags.
var mode uint32
if err := windows.GetConsoleMode(handle, &mode); err != nil {
return err
}

// Now we can set the console mode.
return windows.SetConsoleMode(handle, mode|flags)
}