Skip to content

Commit 28b192a

Browse files
authored
fix(go-runtime): use fatih/color instead of ANSI Escape Code (#4109)
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
1 parent 6b7eb02 commit 28b192a

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

packages/@jsii/go-runtime/jsii-runtime-go/deprecationwarning.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010

1111
"github.com/aws/jsii-runtime-go/internal/compiler"
12+
"github.com/fatih/color"
1213
"github.com/mattn/go-isatty"
1314
)
1415

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

1920
if tty {
2021
// Set terminal to bold red
21-
fmt.Fprintf(os.Stderr, "\u001b[31m\u001b[1m")
22+
color.Set(color.FgRed, color.Bold)
2223
}
2324

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

3334
if tty {
3435
// Reset terminal back to normal
35-
fmt.Fprintf(os.Stderr, "\u001b[0m")
36+
color.Unset()
3637
}
3738
}

packages/@jsii/go-runtime/jsii-runtime-go/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

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

1314
require (
1415
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/mattn/go-colorable v0.1.13 // indirect
1517
github.com/pmezard/go-difflib v1.0.0 // indirect
1618
github.com/yuin/goldmark v1.4.13 // indirect
1719
golang.org/x/mod v0.11.0 // indirect

packages/@jsii/go-runtime/jsii-runtime-go/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0
22
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
6+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
7+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
8+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
9+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
510
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
611
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
712
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -23,6 +28,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
2328
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
2429
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2530
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2632
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2733
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
2834
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)