-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tiup & playground: Tidy output (#2335)
- Loading branch information
1 parent
ced2d6d
commit 93560a1
Showing
9 changed files
with
270 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package colorstr | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mitchellh/colorstring" | ||
) | ||
|
||
func init() { | ||
// Register tiup specific color tokens | ||
DefaultTokens.Colors["tiup_command"] = fmt.Sprintf("%s;%s", | ||
colorstring.DefaultColors["light_cyan"], | ||
colorstring.DefaultColors["bold"], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package colorstr interprets the input format containing color tokens like `[blue]hello [red]world` | ||
// as the text "hello world" in two colors. | ||
// | ||
// Just like tokens in the fmt package (e.g. '%s'), color tokens will only be effective when specified | ||
// as the format parameter. Tokens not in the format parameter will not be interpreted. | ||
// | ||
// colorstr.DefaultTokens.Printf("[blue]hello") ==> (a blue hello) | ||
// colorstr.DefaultTokens.Printf("[ahh]") ==> "[ahh]" | ||
// | ||
// Color tokens in the Print arguments will never be interpreted. It can be useful to pass user inputs there. | ||
package colorstr | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/mitchellh/colorstring" | ||
) | ||
|
||
type colorTokens struct { | ||
colorstring.Colorize | ||
} | ||
|
||
// Note: Print, Println, Fprint, Fprintln are intentionally not implemented, as we would like to | ||
// limit the usage of color token to be only placed in the "format" part. | ||
|
||
// Printf is a convenience wrapper for fmt.Printf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func (c colorTokens) Printf(format string, a ...any) (n int, err error) { | ||
return fmt.Printf(c.Color(format), a...) | ||
} | ||
|
||
// Fprintf is a convenience wrapper for fmt.Fprintf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func (c colorTokens) Fprintf(w io.Writer, format string, a ...any) (n int, err error) { | ||
return fmt.Fprintf(w, c.Color(format), a...) | ||
} | ||
|
||
// Sprintf is a convenience wrapper for fmt.Sprintf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func (c colorTokens) Sprintf(format string, a ...any) string { | ||
return fmt.Sprintf(c.Color(format), a...) | ||
} | ||
|
||
// DefaultTokens uses default color tokens. | ||
var DefaultTokens = (func() colorTokens { | ||
// TODO: Respect NO_COLOR env | ||
// TODO: Add more color tokens here | ||
colors := make(map[string]string) | ||
for k, v := range colorstring.DefaultColors { | ||
colors[k] = v | ||
} | ||
return colorTokens{ | ||
Colorize: colorstring.Colorize{ | ||
Colors: colors, | ||
Disable: false, | ||
Reset: true, | ||
}, | ||
} | ||
})() | ||
|
||
// Printf is a convenience wrapper for fmt.Printf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func Printf(format string, a ...any) (n int, err error) { | ||
return DefaultTokens.Printf(format, a...) | ||
} | ||
|
||
// Fprintf is a convenience wrapper for fmt.Fprintf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func Fprintf(w io.Writer, format string, a ...any) (n int, err error) { | ||
return DefaultTokens.Fprintf(w, format, a...) | ||
} | ||
|
||
// Sprintf is a convenience wrapper for fmt.Sprintf with support for color codes. | ||
// Only color codes in the format param will be respected. | ||
func Sprintf(format string, a ...any) string { | ||
return DefaultTokens.Sprintf(format, a...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package colorstr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDefaultTokens(t *testing.T) { | ||
require.Equal(t, "[blue", DefaultTokens.Sprintf("[blue")) | ||
require.Equal(t, "hello", DefaultTokens.Sprintf("hello")) | ||
require.Equal(t, "\x1B[34mhello\x1B[0m", DefaultTokens.Sprintf("[blue]hello")) | ||
require.Equal(t, "\x1B[34mhello\x1B[0m\x1B[0m", DefaultTokens.Sprintf("[blue]hello[reset]")) | ||
require.Equal(t, "\x1B[34mhello\x1B[0mfoo\x1B[0m", DefaultTokens.Sprintf("[blue]hello[reset]foo")) | ||
require.Equal(t, "\x1B[34mhello\x1B[31mfoo\x1B[0m", DefaultTokens.Sprintf("[blue]hello[red]foo")) | ||
require.Equal(t, "\x1B[34mhello [blue]\x1B[0m", DefaultTokens.Sprintf("[blue]hello %s", "[blue]")) | ||
require.Equal(t, "[ahh]hello", DefaultTokens.Sprintf("[ahh]hello")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package colorstr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// RequireEqualColorToken compares whether the actual string is equal to the expected string after color processing. | ||
func RequireEqualColorToken(t *testing.T, expectColorTokens string, actualString string) { | ||
require.Equal(t, DefaultTokens.Color(expectColorTokens), actualString) | ||
} | ||
|
||
// RequireNotEqualColorToken compares whether the actual string is not equal to the expected string after color processing. | ||
func RequireNotEqualColorToken(t *testing.T, expectColorTokens string, actualString string) { | ||
require.NotEqual(t, DefaultTokens.Color(expectColorTokens), actualString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package colorstr | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRequireEqualColorToken(t *testing.T) { | ||
RequireEqualColorToken(t, "[red]hello", DefaultTokens.Color("[red]hello")) | ||
RequireNotEqualColorToken(t, "[yellow]hello", DefaultTokens.Color("[red]hello")) | ||
RequireEqualColorToken(t, "[red]hello[reset]", DefaultTokens.Color("[red]hello[reset]")) | ||
RequireNotEqualColorToken(t, "[red]hello", DefaultTokens.Color("[red]hello[reset]")) | ||
} |