Skip to content

Commit

Permalink
color types
Browse files Browse the repository at this point in the history
  • Loading branch information
david-littlefarmer committed Aug 23, 2023
1 parent eb6157d commit 92140a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (a attributes) Less(i, j int) bool {
return a[i].Key < a[j].Key
}

func (a attributes) padding(c color) int {
func (a attributes) padding(c foregroundColor) int {
var padding int
for _, e := range a {
color := len(cs(e.Key, c))
Expand Down
50 changes: 27 additions & 23 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@ package devslog

import "fmt"

type color string
type (
foregroundColor string
backgroundColor string
commonValuesColor string
)

const (
// Foreground colors
fgBlack color = "\x1b[30m"
fgRed color = "\x1b[31m"
fgGreen color = "\x1b[32m"
fgYellow color = "\x1b[33m"
fgBlue color = "\x1b[34m"
fgMagenta color = "\x1b[35m"
fgCyan color = "\x1b[36m"
fgWhite color = "\x1b[37m"
fgBlack foregroundColor = "\x1b[30m"
fgRed foregroundColor = "\x1b[31m"
fgGreen foregroundColor = "\x1b[32m"
fgYellow foregroundColor = "\x1b[33m"
fgBlue foregroundColor = "\x1b[34m"
fgMagenta foregroundColor = "\x1b[35m"
fgCyan foregroundColor = "\x1b[36m"
fgWhite foregroundColor = "\x1b[37m"

// Background colors
bgBlack color = "\x1b[40m"
bgRed color = "\x1b[41m"
bgGreen color = "\x1b[42m"
bgYellow color = "\x1b[43m"
bgBlue color = "\x1b[44m"
bgMagenta color = "\x1b[45m"
bgCyan color = "\x1b[46m"
bgWhite color = "\x1b[47m"
bgBlack backgroundColor = "\x1b[40m"
bgRed backgroundColor = "\x1b[41m"
bgGreen backgroundColor = "\x1b[42m"
bgYellow backgroundColor = "\x1b[43m"
bgBlue backgroundColor = "\x1b[44m"
bgMagenta backgroundColor = "\x1b[45m"
bgCyan backgroundColor = "\x1b[46m"
bgWhite backgroundColor = "\x1b[47m"

// Common consts
resetColor color = "\x1b[0m"
faintColor color = "\x1b[2m"
underlineColor color = "\x1b[4m"
resetColor commonValuesColor = "\x1b[0m"
faintColor commonValuesColor = "\x1b[2m"
underlineColor commonValuesColor = "\x1b[4m"
)

// Color string foreground
func cs(text string, fgColor color) string {
func cs(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v", fgColor, text, resetColor)
}

// Color string fainted
func csf(text string, fgColor color) string {
func csf(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, faintColor, text, resetColor)
}

// Color string background
func csb(text string, fgColor color, bgColor color) string {
func csb(text string, fgColor foregroundColor, bgColor backgroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, bgColor, text, resetColor)
}

Expand Down
4 changes: 2 additions & 2 deletions devslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (h *developHandler) formatSourceInfo(buf []byte, r *slog.Record) []byte {
}

func (h *developHandler) levelMessage(buf []byte, r *slog.Record) []byte {
var bgColor color
var fgColor color
var bgColor backgroundColor
var fgColor foregroundColor
var lvlStr string
if h.opts.ReplaceAttr != nil {
a := h.opts.ReplaceAttr(nil, slog.Any(slog.LevelKey, r.Level))
Expand Down

0 comments on commit 92140a7

Please sign in to comment.