Skip to content

Commit

Permalink
fix documetation for golint, split (Color).appendNos method to reduce…
Browse files Browse the repository at this point in the history
… cyclomatic complexity of the code
  • Loading branch information
logrusorgru committed Apr 17, 2019
1 parent c5c7da7 commit 6ec33c2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 88 deletions.
166 changes: 89 additions & 77 deletions color.go
Expand Up @@ -216,6 +216,92 @@ func itoa(t byte) string {
return string(a[j:])
}

func (c Color) appendFg(bs []byte, zero bool) []byte {

if zero || c&maskFm != 0 {
bs = append(bs, ';')
}

// 0- 7 : 30-37
// 8-15 : 90-97
// > 15 : 38;5;val

switch fg := (c & maskFg) >> shiftFg; {
case fg <= 7:
// '3' and the value itself
bs = append(bs, '3', '0'+byte(fg))
case fg <= 15:
// '9' and the value itself
bs = append(bs, '9', '0'+byte(fg&^0x08)) // clear bright flag
default:
bs = append(bs, '3', '8', ';', '5', ';')
bs = append(bs, itoa(byte(fg))...)
}
return bs
}

func (c Color) appendBg(bs []byte, zero bool) []byte {

if zero || c&(maskFm|maskFg) != 0 {
bs = append(bs, ';')
}

// 0- 7 : 40- 47
// 8-15 : 100-107
// > 15 : 48;5;val

switch fg := (c & maskBg) >> shiftBg; {
case fg <= 7:
// '3' and the value itself
bs = append(bs, '4', '0'+byte(fg))
case fg <= 15:
// '1', '0' and the value itself
bs = append(bs, '1', '0', '0'+byte(fg&^0x08)) // clear bright flag
default:
bs = append(bs, '4', '8', ';', '5', ';')
bs = append(bs, itoa(byte(fg))...)
}
return bs
}

func (c Color) appendFm9(bs []byte, zero bool) []byte {

bs = appendCond(bs, c&ItalicFm != 0,
zero || c&(BoldFm|FaintFm) != 0,
'3')
bs = appendCond(bs, c&UnderlineFm != 0,
zero || c&(BoldFm|FaintFm|ItalicFm) != 0,
'4')
// don't combine slow and rapid blink using only
// on of them, preferring slow blink
if c&SlowBlinkFm != 0 {
bs = appendSemi(bs,
zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
'5')
} else if c&RapidBlinkFm != 0 {
bs = appendSemi(bs,
zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
'6')
}

// including 1-2
const mask6i = BoldFm | FaintFm |
ItalicFm | UnderlineFm |
SlowBlinkFm | RapidBlinkFm

bs = appendCond(bs, c&ReverseFm != 0,
zero || c&(mask6i) != 0,
'7')
bs = appendCond(bs, c&ConcealFm != 0,
zero || c&(mask6i|ReverseFm) != 0,
'8')
bs = appendCond(bs, c&CrossedOutFm != 0,
zero || c&(mask6i|ReverseFm|ConcealFm) != 0,
'9')

return bs
}

// append 1;3;38;5;216 like string that represents ANSI
// color of the Color; the zero argument requires
// appending of '0' before to reset previous format
Expand Down Expand Up @@ -248,38 +334,7 @@ func (c Color) appendNos(bs []byte, zero bool) []byte {
ReverseFm | ConcealFm | CrossedOutFm

if c&mask9 != 0 {
bs = appendCond(bs, c&ItalicFm != 0,
zero || c&(BoldFm|FaintFm) != 0,
'3')
bs = appendCond(bs, c&UnderlineFm != 0,
zero || c&(BoldFm|FaintFm|ItalicFm) != 0,
'4')
// don't combine slow and rapid blink using only
// on of them, preferring slow blink
if c&SlowBlinkFm != 0 {
bs = appendSemi(bs,
zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
'5')
} else if c&RapidBlinkFm != 0 {
bs = appendSemi(bs,
zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
'6')
}

// including 1-2
const mask6i = BoldFm | FaintFm |
ItalicFm | UnderlineFm |
SlowBlinkFm | RapidBlinkFm

bs = appendCond(bs, c&ReverseFm != 0,
zero || c&(mask6i) != 0,
'7')
bs = appendCond(bs, c&ConcealFm != 0,
zero || c&(mask6i|ReverseFm) != 0,
'8')
bs = appendCond(bs, c&CrossedOutFm != 0,
zero || c&(mask6i|ReverseFm|ConcealFm) != 0,
'9')
bs = c.appendFm9(bs, zero)
}

// 20-21
Expand Down Expand Up @@ -320,56 +375,13 @@ func (c Color) appendNos(bs []byte, zero bool) []byte {
}

// foreground

if c&maskFg != 0 {

if zero || c&maskFm != 0 {
bs = append(bs, ';')
}

// 0- 7 : 30-37
// 8-15 : 90-97
// > 15 : 38;5;val

switch fg := (c & maskFg) >> shiftFg; {
case fg <= 7:
// '3' and the value itself
bs = append(bs, '3', '0'+byte(fg))
case fg <= 15:
// '9' and the value itself
bs = append(bs, '9', '0'+byte(fg&^0x08)) // clear bright flag
default:
bs = append(bs, '3', '8', ';', '5', ';')
bs = append(bs, itoa(byte(fg))...)
}

bs = c.appendFg(bs, zero)
}

// background

if c&maskBg != 0 {

// excluding foreground bright flag
if zero || c&(maskFm|maskFg) != 0 {
bs = append(bs, ';')
}

// 0- 7 : 40- 47
// 8-15 : 100-107
// > 15 : 48;5;val

switch fg := (c & maskBg) >> shiftBg; {
case fg <= 7:
// '3' and the value itself
bs = append(bs, '4', '0'+byte(fg))
case fg <= 15:
// '1', '0' and the value itself
bs = append(bs, '1', '0', '0'+byte(fg&^0x08)) // clear bright flag
default:
bs = append(bs, '4', '8', ';', '5', ';')
bs = append(bs, itoa(byte(fg))...)
}

bs = c.appendBg(bs, zero)
}

return bs
Expand Down
4 changes: 2 additions & 2 deletions value.go
Expand Up @@ -311,7 +311,7 @@ func (vc valueClear) BgIndex(uint8) Value { return vc }
func (vc valueClear) BgGray(uint8) Value { return vc }
func (vc valueClear) Colorize(Color) Value { return vc }

func (v valueClear) Format(s fmt.State, verb rune) {
func (vc valueClear) Format(s fmt.State, verb rune) {
// it's enough for many cases (%-+020.10f)
// % - 1
// availFlags - 3 (5)
Expand Down Expand Up @@ -342,7 +342,7 @@ func (v valueClear) Format(s fmt.State, verb rune) {
} else {
format = append(format, byte(verb))
}
fmt.Fprintf(s, string(format), v.value)
fmt.Fprintf(s, string(format), vc.value)
}

// Value within colors
Expand Down
19 changes: 10 additions & 9 deletions wrap.go
Expand Up @@ -67,7 +67,7 @@ func Bold(arg interface{}) Value {
return value{value: arg, color: BoldFm}
}

// Faint, decreased intensity (2).
// Faint decreases intensity (2).
// The Faint rejects the Bold.
func Faint(arg interface{}) Value {
if val, ok := arg.(Value); ok {
Expand All @@ -85,15 +85,15 @@ func DoublyUnderline(arg interface{}) Value {
return value{value: arg, color: DoublyUnderlineFm}
}

// Fraktur, rarely supported (20).
// Fraktur is rarely supported (20).
func Fraktur(arg interface{}) Value {
if val, ok := arg.(Value); ok {
return val.Fraktur()
}
return value{value: arg, color: FrakturFm}
}

// Italic, not widely supported, sometimes
// Italic is not widely supported, sometimes
// treated as inverse (3).
func Italic(arg interface{}) Value {
if val, ok := arg.(Value); ok {
Expand All @@ -110,17 +110,17 @@ func Underline(arg interface{}) Value {
return value{value: arg, color: UnderlineFm}
}

// SlowBlink, blinking less than 150
// per minute (5).
// SlowBlink makes text blink less than
// 150 per minute (5).
func SlowBlink(arg interface{}) Value {
if val, ok := arg.(Value); ok {
return val.SlowBlink()
}
return value{value: arg, color: SlowBlinkFm}
}

// RapidBlink, blinking 150+ per minute,
// not widely supported (6).
// RapidBlink makes text blink 150+ per
// minute. It is not widely supported (6).
func RapidBlink(arg interface{}) Value {
if val, ok := arg.(Value); ok {
return val.RapidBlink()
Expand All @@ -147,7 +147,8 @@ func Inverse(arg interface{}) Value {
return Reverse(arg)
}

// Conceal, hidden, not widely supported (8).
// Conceal hides text, preserving an ability to select
// the text and copy it. It is not widely supported (8).
func Conceal(arg interface{}) Value {
if val, ok := arg.(Value); ok {
return val.Conceal()
Expand All @@ -160,7 +161,7 @@ func Hidden(arg interface{}) Value {
return Conceal(arg)
}

// CrossedOut, characters legible, but
// CrossedOut makes characters legible, but
// marked for deletion (9).
func CrossedOut(arg interface{}) Value {
if val, ok := arg.(Value); ok {
Expand Down

0 comments on commit 6ec33c2

Please sign in to comment.