Skip to content

Commit

Permalink
fix(go-runtime): use fatih/color instead of ANSI Escape Code (#4109)
Browse files Browse the repository at this point in the history
ANSI Escape Codes don't work in Windows so use [fatih/color](https://github.com/fatih/color) .

Debug code(ANSI):

```go
package main


import (
	"fmt"
	"os"
)

func main() {
	fmt.Fprintf(os.Stderr, "\u001b[31m\u001b[1m")
        fmt.Fprintln(os.Stderr, "###########################################################")
        fmt.Fprintf(os.Stderr, "\u001b[0m")
	fmt.Fprintln(os.Stderr, "###########################################################")
}
```

Result(Amazon Linux 2023):
![スクリーンショット 2023-05-21 18 08 16](https://github.com/aws/jsii/assets/50798936/2318d7a3-8cb5-4cd6-8275-682b9b307563)

Result(Windows_Server-2022):
<img width="554" alt="スクリーンショット 2023-05-21 18 08 08" src="https://github.com/aws/jsii/assets/50798936/9d1615ac-825e-4039-b9c6-d65a69298d89">


Debug code(fatih/color):

```go
package main


import (
	"fmt"
	"os"

	"github.com/fatih/color"
	"github.com/mattn/go-isatty"
)

func main() {
	fmt.Printf("res: %v\n", isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()))
	color.Set(color.FgRed)
        fmt.Fprintln(os.Stderr, "###########################################################")
	color.Unset()
	fmt.Fprintln(os.Stderr, "###########################################################")
}
```

Result(Amazon Linux 2023):
![スクリーンショット 2023-05-21 18 24 38](https://github.com/aws/jsii/assets/50798936/4c6b76b0-5ce5-48f7-810d-a640de8e0167)

Result(Windows_Server-2022):
<img width="484" alt="スクリーンショット 2023-05-21 18 24 13" src="https://github.com/aws/jsii/assets/50798936/182984c5-eedd-447e-88d7-33c46939ace8">


---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
wafuwafu13 authored Jul 3, 2023
1 parent 6b7eb02 commit 28b192a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/aws/jsii-runtime-go/internal/compiler"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)

Expand All @@ -18,7 +19,7 @@ func init() {

if tty {
// Set terminal to bold red
fmt.Fprintf(os.Stderr, "\u001b[31m\u001b[1m")
color.Set(color.FgRed, color.Bold)
}

fmt.Fprintln(os.Stderr, "###########################################################")
Expand All @@ -32,6 +33,6 @@ func init() {

if tty {
// Reset terminal back to normal
fmt.Fprintf(os.Stderr, "\u001b[0m")
color.Unset()
}
}
2 changes: 2 additions & 0 deletions packages/@jsii/go-runtime/jsii-runtime-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/fatih/color v1.15.0
github.com/mattn/go-isatty v0.0.19
github.com/stretchr/testify v1.8.4
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
Expand All @@ -12,6 +13,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/mod v0.11.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions packages/@jsii/go-runtime/jsii-runtime-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -23,6 +28,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 28b192a

Please sign in to comment.