Skip to content

Commit

Permalink
better default colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew O'Meara committed Oct 1, 2020
1 parent 5427f40 commit bedc073
Showing 1 changed file with 48 additions and 36 deletions.
84 changes: 48 additions & 36 deletions klog_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ type FmtConstWidth struct {

// FormatHeader -- see interface Formatter
func (f *FmtConstWidth) FormatHeader(sevChar byte, file string, line int, buf *bytes.Buffer) {
var (
var (
tmp [64]byte
)
sz := 0

usingColor := f.UseColor
if usingColor {
var fg byte
var fg byte
switch sevChar {
case 'W':
fg = yellow
case 'W':
fg = StdWarnColor
case 'E', 'F':
fg = lightRed
fg = StdErrColor
default:
fg = dim
fg = StdInfoColor
}
sz += AppendColorCode(fg, tmp[sz:])
sz += AppendColorCode(fg, tmp[sz:])
}

tmp[sz] = sevChar
Expand Down Expand Up @@ -100,15 +100,14 @@ func (f *FmtConstWidth) FormatHeader(sevChar byte, file string, line int, buf *b
sz += AppendDigits(line, tmp[sz:])
}
}
tmp[sz] = '|'
sz++

tmp[sz] = '|'
sz++

if usingColor {
sz += AppendColorCode(resetColor, tmp[sz:])
}
tmp[sz] = ' '
sz++
}
tmp[sz] = ' '
sz++

buf.Write(tmp[:sz])
}
Expand Down Expand Up @@ -183,39 +182,45 @@ func AppendNDigits(inNumDigits int, inValue int, buf []byte, inPad byte) int {
return inNumDigits
}



// AppendColorCode appends the console color code to the given slice,
// returning how many bytes were appended.
// AppendColorCode appends the console color code to the given slice.
// Returns how many bytes were appended.
// https://misc.flogisoft.com/bash/tip_colors_and_formatting
func AppendColorCode(code byte, buf []byte) int {
buf[0] = '\x1b'
buf[1] = '['
sz := 2

{
if code >= 100 {
digit := '0' + code/100
buf[sz] = digit
sz++
code -= digit * 100
}
if code >= 10 {
digit := code/10
buf[sz] = '0' + digit
sz++
code -= digit * 10
}
buf[sz] = '0' + code
sz++
}
buf[2] = '3'
buf[3] = '8'
buf[4] = ';'
buf[5] = '5'
buf[6] = ';'
sz := 7

{
var digits [4]byte
numDigits := 0
for {
digit := code % 10
digits[numDigits] = '0' + digit
numDigits++
code -= digit
if code == 0 {
break
}
code /= 10
}
for i := int32(numDigits - 1); i >= 0; i-- {
buf[sz] = digits[i]
sz++
}
}

buf[sz] = 'm'
sz++

return sz
}

// https://misc.flogisoft.com/bash/tip_colors_and_formatting
// ANSI color codes
const (
resetColor = byte(0)
bold = 1
Expand All @@ -238,3 +243,10 @@ const (
lightMagenta = 95
lightCyan = 96
)

// 256 color palette mode
const (
StdInfoColor byte = 139
StdErrColor = 196
StdWarnColor = 166
)

0 comments on commit bedc073

Please sign in to comment.