Skip to content

Commit

Permalink
Add functions to print or sprint a colour string
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 28, 2024
1 parent 63432e0 commit fcf285e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
43 changes: 40 additions & 3 deletions cmd/color/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package color

import "fmt"

const escape = "\x1b"

type Color int

const (
Reset int = iota
Reset Color = iota
Bold
Dim
Italic
Underline
)

const (
FgBlack int = iota + 30
FgBlack Color = iota + 30
FgRed
FgGreen
FgYellow
Expand All @@ -19,7 +25,7 @@ const (
)

const (
FgDarkGray int = iota + 90
FgDarkGray Color = iota + 90
FgLightRed
FgLightGreen
FgLightYellow
Expand All @@ -28,3 +34,34 @@ const (
FgLightCyan
FgWhite
)

const (
BgDefault Color = iota + 49
BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgLightGray
)

const (
BgDarkGray Color = iota + 100
BgLightRed
BgLightGreen
BgLightYellow
BgLightBlue
BgLightMagenta
BgLightCyan
BgWhite
)

func SprintColor(fg Color, bg Color, message string) string {
return fmt.Sprintf("%s[%d;%dm%s%s[0;0m", escape, fg, bg, message, escape)
}

func PrintColor(fg Color, bg Color, message string) {
fmt.Println(SprintColor(fg, bg, message))
}
14 changes: 7 additions & 7 deletions cmd/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"time"

"github.com/fatih/color"
"github.com/Dobefu/csb/cmd/color"
)

var (
Expand All @@ -30,20 +30,20 @@ func logMessage(level byte, format string, a ...any) string {

switch level {
case LOG_VERBOSE:
str = fmt.Sprintf(color.HiBlackString(" %s"), format)
str = fmt.Sprintf(color.SprintColor(color.FgDarkGray, color.BgDefault, " %s"), format)
case LOG_INFO:
str = fmt.Sprintf(color.HiCyanString("ℹ %s"), format)
str = fmt.Sprintf(color.SprintColor(color.FgLightCyan, color.BgDefault, "ℹ %s"), format)
case LOG_WARNING:
str = fmt.Sprintf(color.YellowString("⚠ %s"), format)
str = fmt.Sprintf(color.SprintColor(color.FgYellow, color.BgDefault, "⚠ %s"), format)
case LOG_ERROR:
str = fmt.Sprintf(color.HiRedString("‼ %s"), format)
str = fmt.Sprintf(color.SprintColor(color.FgLightRed, color.BgDefault, "‼ %s"), format)
case LOG_FATAL:
str = fmt.Sprintf(color.RedString("‼ %s"), format)
str = fmt.Sprintf(color.SprintColor(color.FgRed, color.BgDefault, "‼ %s"), format)
default:
str = format
}

str = fmt.Sprintf("%s %s\n", color.HiBlackString(timestamp), str)
str = fmt.Sprintf("%s %s\n", color.SprintColor(color.FgDarkGray, color.BgDefault, timestamp), str)
output := fmt.Sprintf(str, a...)

fmt.Print(output)
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/Dobefu/csb
go 1.23.0

require (
github.com/fatih/color v1.18.0
github.com/go-sql-driver/mysql v1.8.1
github.com/joho/godotenv v1.5.1
github.com/mattn/go-sqlite3 v1.14.24
Expand All @@ -13,9 +12,6 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit fcf285e

Please sign in to comment.