-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make the help output pretty * Add another sub command to the simple example
- Loading branch information
1 parent
09564f4
commit 3cd05c8
Showing
10 changed files
with
183 additions
and
13 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
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,40 @@ | ||
// Package colour implements basic text colouring for cli's limited needs. | ||
// | ||
// In particular, it's not expected to provide every ANSI code, just the ones we need. The codes have also been padded so that they are | ||
// the same length, which means [text/tabwriter] will correctly calculate alignment as long as styles are not mixed within a table. | ||
package colour | ||
|
||
import "os" | ||
|
||
// ANSI codes for coloured output, they are all the same length so as not to throw off | ||
// alignment of [text/tabwriter]. | ||
const ( | ||
CodeReset = "\x1b[000000m" // Reset all attributes | ||
CodeTitle = "\x1b[1;37;4m" // Bold, white & underlined | ||
CodeBold = "\x1b[1;0037m" // Bold & white | ||
) | ||
|
||
// Title returns the given text in a title style, bold white and underlined. | ||
// | ||
// If $NO_COLOR is set, text will be returned unmodified. | ||
func Title(text string) string { | ||
if noColour() { | ||
return text | ||
} | ||
return CodeTitle + text + CodeReset | ||
} | ||
|
||
// Bold returns the given text in bold white. | ||
// | ||
// If $NO_COLOR is set, text will be returned unmodified. | ||
func Bold(text string) string { | ||
if noColour() { | ||
return text | ||
} | ||
return CodeBold + text + CodeReset | ||
} | ||
|
||
// noColour returns whether the $NO_COLOR env var was set. | ||
func noColour() bool { | ||
return os.Getenv("NO_COLOR") != "" | ||
} |
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,73 @@ | ||
package colour_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/FollowTheProcess/cli/internal/colour" | ||
"github.com/FollowTheProcess/test" | ||
) | ||
|
||
func TestColour(t *testing.T) { | ||
tests := []struct { | ||
name string // Name of the test case | ||
text string // Text to colour | ||
fn func(text string) string // Printer function to return the coloured version of text | ||
want string // Expected result containing ANSI escape codes | ||
noColor bool // Whether to set the $NO_COLOR env var | ||
}{ | ||
{ | ||
name: "bold", | ||
text: "hello bold", | ||
fn: colour.Bold, | ||
want: colour.CodeBold + "hello bold" + colour.CodeReset, | ||
}, | ||
{ | ||
name: "bold no color", | ||
text: "hello bold", | ||
fn: colour.Bold, | ||
noColor: true, | ||
want: "hello bold", | ||
}, | ||
{ | ||
name: "title", | ||
text: "Section", | ||
fn: colour.Title, | ||
want: colour.CodeTitle + "Section" + colour.CodeReset, | ||
}, | ||
{ | ||
name: "title no color", | ||
text: "Section", | ||
fn: colour.Title, | ||
noColor: true, | ||
want: "Section", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.noColor { | ||
t.Setenv("NO_COLOR", "true") | ||
} | ||
got := tt.fn(tt.text) | ||
test.Equal(t, got, tt.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestCodesAllSameLength(t *testing.T) { | ||
test.True(t, len(colour.CodeBold) == len(colour.CodeReset)) | ||
test.True(t, len(colour.CodeBold) == len(colour.CodeTitle)) | ||
test.True(t, len(colour.CodeReset) == len(colour.CodeTitle)) | ||
} | ||
|
||
func BenchmarkBold(b *testing.B) { | ||
for range b.N { | ||
colour.Bold("Some bold text here") | ||
} | ||
} | ||
|
||
func BenchmarkTitle(b *testing.B) { | ||
for range b.N { | ||
colour.Title("Some title here") | ||
} | ||
} |
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