Skip to content

Commit

Permalink
Fix broken colorized verbose output
Browse files Browse the repository at this point in the history
From #660 (comment), the colorized verbose output is broken. The old implementation tries to use ‘\e’ which is not recognized in a Go string. Let’s Use ‘\033’ instead.
  • Loading branch information
owenthereal committed Oct 28, 2014
1 parent d26b4e9 commit b377107
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions github/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type verboseTransport struct {
Transport *http.Transport
Verbose bool
OverrideURL *url.URL
Out io.Writer
Colorized bool
}

func (t *verboseTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
Expand Down Expand Up @@ -107,11 +109,11 @@ func (t *verboseTransport) dumpBody(body io.ReadCloser) io.ReadCloser {
}

func (t *verboseTransport) verbosePrintln(msg string) {
if isTerminal(os.Stderr.Fd()) {
msg = fmt.Sprintf("\\e[36m%s\\e[m", msg)
if t.Colorized {
msg = fmt.Sprintf("\033[36m%s\033[0m", msg)
}

fmt.Fprintln(os.Stderr, msg)
fmt.Fprintln(t.Out, msg)
}

func newHttpClient(testHost string, verbose bool) *http.Client {
Expand All @@ -123,6 +125,8 @@ func newHttpClient(testHost string, verbose bool) *http.Client {
Transport: &http.Transport{Proxy: proxyFromEnvironment},
Verbose: verbose,
OverrideURL: testURL,
Out: os.Stderr,
Colorized: isTerminal(os.Stderr.Fd()),
}
return &http.Client{Transport: tr}
}
Expand Down
12 changes: 12 additions & 0 deletions github/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -52,3 +53,14 @@ func TestNewHttpClient_OverrideURL(t *testing.T) {
c = newHttpClient("", false)
c.Get(fmt.Sprintf("%s/not-override", s.URL.String()))
}

func TestVerboseTransport_VerbosePrintln(t *testing.T) {
var b bytes.Buffer
tr := &verboseTransport{
Out: &b,
Colorized: true,
}

tr.verbosePrintln("foo")
assert.Equal(t, "\033[36mfoo\033[0m\n", b.String())
}

0 comments on commit b377107

Please sign in to comment.