Skip to content

Commit

Permalink
Fixed junk output in windows console. (#29) (#44)
Browse files Browse the repository at this point in the history
For windows runtime, added originalMode or
ENABLE_VIRTUAL_TERMINAL_PROCESSING in SetConsoleMode, and if case of any
error, disabled color print.
  • Loading branch information
frzam committed Aug 9, 2021
1 parent 3330736 commit f759ec3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
9 changes: 8 additions & 1 deletion formatter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ func (j *JSON) Write(p []byte) (n int, err error) {
cp = append(append(append(append(cp, '\n'), bytes.Repeat(indent, j.level)...), cs.Default...), b)
if (p[0] != '}' && p[0] != ']') && j.level == 0 {
// Add a return after the outer closing brace.
cp = append(append(cp, '\n'), cs.Color(ResetColor)...)
// If cs is zero that means color is disabled, so only append '\n'
// else append '\n' and ResetColor.
if cs.IsZero() {
cp = append(cp, '\n')
} else {
cp = append(append(cp, '\n'), cs.Color(ResetColor)...)
}

}
case ':':
j.isValue = true
Expand Down
25 changes: 21 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"syscall"

"github.com/rs/curlie/args"
"github.com/rs/curlie/formatter"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/windows"
)

var (
Expand All @@ -23,7 +25,7 @@ var (

func main() {
// handle `curlie version` separately from `curl --version`
if 2 == len(os.Args) && "version" == os.Args[1] {
if len(os.Args) == 2 && os.Args[1] == "version" {
fmt.Printf("curlie %s (%s)\n", version, date)
os.Exit(0)
return
Expand All @@ -35,6 +37,18 @@ func main() {
stdoutFd := int(os.Stdout.Fd())
stderrFd := int(os.Stderr.Fd())

// Setting Console mode on windows to allow color output, By default scheme is DefaultColorScheme
// But in case of any error, it is set to ColorScheme{}.
scheme := formatter.DefaultColorScheme
if runtime.GOOS == "windows" {
console := windows.Handle(stdoutFd)
var originalMode uint32
windows.GetConsoleMode(console, &originalMode)
err := windows.SetConsoleMode(console, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
if err != nil {
scheme = formatter.ColorScheme{}
}
}
var stdout io.Writer = os.Stdout
var stderr io.Writer = os.Stderr
var stdin io.Reader = os.Stdin
Expand Down Expand Up @@ -74,13 +88,14 @@ func main() {
if pretty || terminal.IsTerminal(stdoutFd) {
inputWriter = &formatter.JSON{
Out: inputWriter,
Scheme: formatter.DefaultColorScheme,
Scheme: scheme,
}
// Format/colorize JSON output if stdout is to the terminal.
stdout = &formatter.JSON{
Out: stdout,
Scheme: formatter.DefaultColorScheme,
Scheme: scheme,
}

// Filter out binary output.
stdout = &formatter.BinaryFilter{Out: stdout}
}
Expand All @@ -89,10 +104,12 @@ func main() {
if !quiet {
opts = append(opts, "-v")
}

stderr = &formatter.HeaderColorizer{
Out: stderr,
Scheme: formatter.DefaultColorScheme,
Scheme: scheme,
}

}
hasInput := true
if data := opts.Val("d"); data != "" {
Expand Down

0 comments on commit f759ec3

Please sign in to comment.