-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
colors: detect (and try to enable) ANSI codes support (#56)
- Loading branch information
Showing
47 changed files
with
6,982 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
package table | ||
|
||
import "reflect" | ||
|
||
// AutoIndexColumnID returns a unique Column ID/Name for the given Column Number. | ||
// The functionality is similar to what you get in an Excel spreadsheet w.r.t. | ||
// the Column ID/Name. | ||
func AutoIndexColumnID(colIdx int) string { | ||
charIdx := colIdx % 26 | ||
out := string(65 + charIdx) | ||
colIdx = colIdx / 26 | ||
if colIdx > 0 { | ||
return AutoIndexColumnID(colIdx-1) + out | ||
} | ||
return out | ||
} | ||
|
||
// IsNumber returns true if the argument is a numeric type; false otherwise. | ||
func IsNumber(x interface{}) bool { | ||
switch reflect.TypeOf(x).Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, | ||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, | ||
reflect.Float32, reflect.Float64: | ||
return true | ||
} | ||
return false | ||
} | ||
|
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,33 @@ | ||
package table | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAutoIndexColumnID(t *testing.T) { | ||
assert.Equal(t, "A", AutoIndexColumnID(0)) | ||
assert.Equal(t, "Z", AutoIndexColumnID(25)) | ||
assert.Equal(t, "AA", AutoIndexColumnID(26)) | ||
assert.Equal(t, "ZZ", AutoIndexColumnID(701)) | ||
assert.Equal(t, "AAA", AutoIndexColumnID(702)) | ||
assert.Equal(t, "ZZZ", AutoIndexColumnID(18277)) | ||
assert.Equal(t, "AAAA", AutoIndexColumnID(18278)) | ||
} | ||
|
||
func TestIsNumber(t *testing.T) { | ||
assert.True(t, IsNumber(int(1))) | ||
assert.True(t, IsNumber(int8(1))) | ||
assert.True(t, IsNumber(int16(1))) | ||
assert.True(t, IsNumber(int32(1))) | ||
assert.True(t, IsNumber(int64(1))) | ||
assert.True(t, IsNumber(uint(1))) | ||
assert.True(t, IsNumber(uint8(1))) | ||
assert.True(t, IsNumber(uint16(1))) | ||
assert.True(t, IsNumber(uint32(1))) | ||
assert.True(t, IsNumber(uint64(1))) | ||
assert.True(t, IsNumber(float32(1))) | ||
assert.True(t, IsNumber(float64(1))) | ||
assert.False(t, IsNumber("1")) | ||
} |
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,5 @@ | ||
package text | ||
|
||
// ANSICodesSupported will be true on consoles where ANSI Escape Codes/Sequences | ||
// are supported. | ||
var ANSICodesSupported = areANSICodesSupported() |
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,7 @@ | ||
// +build !windows | ||
|
||
package text | ||
|
||
func areANSICodesSupported() bool { | ||
return true | ||
} |
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,23 @@ | ||
// +build windows | ||
|
||
package text | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func areANSICodesSupported() bool { | ||
outHandle := windows.Handle(os.Stdout.Fd()) | ||
var outMode uint32 | ||
if err := windows.GetConsoleMode(outHandle, &outMode); err == nil { | ||
if outMode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 { | ||
return true | ||
} | ||
if err := windows.SetConsoleMode(outHandle, outMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.