-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Fatih Arslan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,71 @@ | ||
// Package fastcolor is an extreme subset of the fatih/color package to get | ||
// ANSI colors on standard output. | ||
// | ||
// Modified to output the color string to a StringWriter with fixed-width | ||
// formatting (spaces for padding). Minimal color and attribute support. | ||
package fastcolor | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"github.com/mattn/go-isatty" | ||
) | ||
|
||
type Color string | ||
|
||
const ( | ||
Reset Color = "0" | ||
Bold Color = "1" | ||
FgBlack Color = "30" | ||
FgRed Color = "31" | ||
FgGreen Color = "32" | ||
FgYellow Color = "33" | ||
FgBlue Color = "34" | ||
FgMagenta Color = "35" | ||
FgCyan Color = "36" | ||
FgWhite Color = "37" | ||
) | ||
|
||
var spaceStr string = strings.Repeat(" ", 132) | ||
|
||
// NoColor defines if the output is colorized or not. It's dynamically set to | ||
// false or true based on the stdout's file descriptor referring to a terminal | ||
// or not. It's also set to true if the NO_COLOR environment variable is | ||
// set (regardless of its value). This is a global option and affects all | ||
// colors. | ||
var NoColor = noColorIsSet() || os.Getenv("TERM") == "dumb" || | ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) | ||
|
||
// noColorIsSet returns true if the environment variable NO_COLOR is set to a non-empty string. | ||
func noColorIsSet() bool { | ||
return os.Getenv("NO_COLOR") != "" | ||
} | ||
|
||
func (c Color) WriteStringFixed(w io.StringWriter, s string, width int, leftpad bool) { | ||
if !NoColor { | ||
w.WriteString("\x1b[") | ||
w.WriteString(string(c)) | ||
w.WriteString("m") | ||
} | ||
|
||
l := utf8.RuneCountInString(s) | ||
spaces := width - l | ||
if spaces > 0 { | ||
if leftpad { | ||
w.WriteString(spaceStr[:spaces]) | ||
w.WriteString(s) | ||
} else { | ||
w.WriteString(s) | ||
w.WriteString(spaceStr[:spaces]) | ||
} | ||
} else { | ||
w.WriteString(s[:width]) | ||
} | ||
|
||
if !NoColor { | ||
w.WriteString("\x1b[0m") | ||
} | ||
} |