aboutsummaryrefslogtreecommitdiffstats
path: root/bug
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-14 18:06:42 +0200
committerMichael Muré <batolettre@gmail.com>2020-06-14 18:06:42 +0200
commit9a00ffb72ea3b9791a77c1f20196f9d6ba01cb54 (patch)
tree683822c186564a351dddfa38786dcca0877cc68a /bug
parent5029cc1e76de6ed08886d5cf9a06d444d6866282 (diff)
downloadgit-bug-9a00ffb72ea3b9791a77c1f20196f9d6ba01cb54.tar.gz
bug: make sure to disable label color escape when not on a terminal
Diffstat (limited to 'bug')
-rw-r--r--bug/label.go94
1 files changed, 50 insertions, 44 deletions
diff --git a/bug/label.go b/bug/label.go
index 1344d97e..75d6b012 100644
--- a/bug/label.go
+++ b/bug/label.go
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/MichaelMure/git-bug/util/text"
+ fcolor "github.com/fatih/color"
)
type Label string
@@ -15,36 +16,33 @@ func (l Label) String() string {
return string(l)
}
-type LabelColor color.RGBA
-
// RGBA from a Label computed in a deterministic way
func (l Label) Color() LabelColor {
- id := 0
- hash := sha1.Sum([]byte(l))
-
// colors from: https://material-ui.com/style/color/
colors := []LabelColor{
- LabelColor{R: 244, G: 67, B: 54, A: 255}, // red
- LabelColor{R: 233, G: 30, B: 99, A: 255}, // pink
- LabelColor{R: 156, G: 39, B: 176, A: 255}, // purple
- LabelColor{R: 103, G: 58, B: 183, A: 255}, // deepPurple
- LabelColor{R: 63, G: 81, B: 181, A: 255}, // indigo
- LabelColor{R: 33, G: 150, B: 243, A: 255}, // blue
- LabelColor{R: 3, G: 169, B: 244, A: 255}, // lightBlue
- LabelColor{R: 0, G: 188, B: 212, A: 255}, // cyan
- LabelColor{R: 0, G: 150, B: 136, A: 255}, // teal
- LabelColor{R: 76, G: 175, B: 80, A: 255}, // green
- LabelColor{R: 139, G: 195, B: 74, A: 255}, // lightGreen
- LabelColor{R: 205, G: 220, B: 57, A: 255}, // lime
- LabelColor{R: 255, G: 235, B: 59, A: 255}, // yellow
- LabelColor{R: 255, G: 193, B: 7, A: 255}, // amber
- LabelColor{R: 255, G: 152, B: 0, A: 255}, // orange
- LabelColor{R: 255, G: 87, B: 34, A: 255}, // deepOrange
- LabelColor{R: 121, G: 85, B: 72, A: 255}, // brown
- LabelColor{R: 158, G: 158, B: 158, A: 255}, // grey
- LabelColor{R: 96, G: 125, B: 139, A: 255}, // blueGrey
+ {R: 244, G: 67, B: 54, A: 255}, // red
+ {R: 233, G: 30, B: 99, A: 255}, // pink
+ {R: 156, G: 39, B: 176, A: 255}, // purple
+ {R: 103, G: 58, B: 183, A: 255}, // deepPurple
+ {R: 63, G: 81, B: 181, A: 255}, // indigo
+ {R: 33, G: 150, B: 243, A: 255}, // blue
+ {R: 3, G: 169, B: 244, A: 255}, // lightBlue
+ {R: 0, G: 188, B: 212, A: 255}, // cyan
+ {R: 0, G: 150, B: 136, A: 255}, // teal
+ {R: 76, G: 175, B: 80, A: 255}, // green
+ {R: 139, G: 195, B: 74, A: 255}, // lightGreen
+ {R: 205, G: 220, B: 57, A: 255}, // lime
+ {R: 255, G: 235, B: 59, A: 255}, // yellow
+ {R: 255, G: 193, B: 7, A: 255}, // amber
+ {R: 255, G: 152, B: 0, A: 255}, // orange
+ {R: 255, G: 87, B: 34, A: 255}, // deepOrange
+ {R: 121, G: 85, B: 72, A: 255}, // brown
+ {R: 158, G: 158, B: 158, A: 255}, // grey
+ {R: 96, G: 125, B: 139, A: 255}, // blueGrey
}
+ id := 0
+ hash := sha1.Sum([]byte(l))
for _, char := range hash {
id = (id + int(char)) % len(colors)
}
@@ -52,12 +50,30 @@ func (l Label) Color() LabelColor {
return colors[id]
}
+func (l Label) Validate() error {
+ str := string(l)
+
+ if text.Empty(str) {
+ return fmt.Errorf("empty")
+ }
+
+ if strings.Contains(str, "\n") {
+ return fmt.Errorf("should be a single line")
+ }
+
+ if !text.Safe(str) {
+ return fmt.Errorf("not fully printable")
+ }
+
+ return nil
+}
+
+type LabelColor color.RGBA
+
func (lc LabelColor) RGBA() color.RGBA {
return color.RGBA(lc)
}
-type Term256 int
-
func (lc LabelColor) Term256() Term256 {
red := Term256(lc.R) * 6 / 256
green := Term256(lc.G) * 6 / 256
@@ -66,28 +82,18 @@ func (lc LabelColor) Term256() Term256 {
return red*36 + green*6 + blue + 16
}
+type Term256 int
+
func (t Term256) Escape() string {
+ if fcolor.NoColor {
+ return ""
+ }
return fmt.Sprintf("\x1b[38;5;%dm", t)
}
func (t Term256) Unescape() string {
- return "\x1b[0m"
-}
-
-func (l Label) Validate() error {
- str := string(l)
-
- if text.Empty(str) {
- return fmt.Errorf("empty")
- }
-
- if strings.Contains(str, "\n") {
- return fmt.Errorf("should be a single line")
- }
-
- if !text.Safe(str) {
- return fmt.Errorf("not fully printable")
+ if fcolor.NoColor {
+ return ""
}
-
- return nil
+ return "\x1b[0m"
}