Skip to content

Commit

Permalink
feat: force colors when using ansifmt, add tests for ansifmt
Browse files Browse the repository at this point in the history
  • Loading branch information
srevinsaju committed Apr 6, 2024
1 parent 32bbbd7 commit 54334bd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
34 changes: 34 additions & 0 deletions examples/colors/togomak.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
togomak {
version = 2
}

stage "colors" {
for_each = toset([
"error",
"success",
"warn",
"warning",
"info",
"red",
"green",
"blue",
"yellow",
"bold",
"italic",
"cyan",
"grey",
"white",
"magenta",
"orange",
"hi-green",
"hi-blue",
"hi-magenta",
"hi-black",
"hi-white",
"hi-red",
"hi-cyan",
"hi-yellow",
])

script = "echo ${ansifmt(each.key, "hello world")}"
}
2 changes: 1 addition & 1 deletion examples/module-phases-inheritance/calculator/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ stage "paths" {
script = <<-EOT
echo path.module: ${path.module}
echo path.cwd: ${path.cwd}
echo path.root: ${path.root}
echo path.root: ${ansifmt("green", path.root)}
EOT
}
75 changes: 61 additions & 14 deletions internal/ui/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,32 @@ import (
"os"
)

var Green = color.New(color.FgGreen).SprintFunc()
var HiCyan = color.New(color.FgHiCyan).SprintFunc()
var Red = color.New(color.FgRed).SprintFunc()
var Bold = color.New(color.Bold).SprintFunc()
var Blue = color.New(color.FgBlue).SprintFunc()
var Grey = color.New(color.FgHiBlack).SprintFunc()
var Yellow = color.New(color.FgYellow).SprintFunc()
var HiYellow = color.New(color.FgHiYellow).SprintFunc()
var HiRed = color.New(color.FgHiRed).SprintFunc()
var Italic = color.New(color.Italic).SprintFunc()
var Plus = color.New(color.FgHiWhite).SprintFunc()("+")
func NewColor(value ...color.Attribute) *color.Color {
c := color.New(value...)
c.EnableColor()
return c
}

var Green = NewColor(color.FgGreen).SprintFunc()
var Red = NewColor(color.FgRed).SprintFunc()
var Bold = NewColor(color.Bold).SprintFunc()
var Blue = NewColor(color.FgBlue).SprintFunc()
var Grey = NewColor(color.FgHiBlack).SprintFunc()
var Cyan = NewColor(color.FgCyan).SprintFunc()
var White = NewColor(color.FgWhite).SprintFunc()
var Yellow = NewColor(color.FgYellow).SprintFunc()
var Magenta = NewColor(color.FgMagenta).SprintFunc()
var Orange = NewColor(color.FgHiYellow).SprintFunc()
var HiGreen = NewColor(color.FgHiGreen).SprintFunc()
var HiBlue = NewColor(color.FgHiBlue).SprintFunc()
var HiMagenta = NewColor(color.FgHiMagenta).SprintFunc()
var HiBlack = NewColor(color.FgHiBlack).SprintFunc()
var HiCyan = NewColor(color.FgHiCyan).SprintFunc()
var HiWhite = NewColor(color.FgHiWhite).SprintFunc()
var HiYellow = NewColor(color.FgHiYellow).SprintFunc()
var HiRed = NewColor(color.FgHiRed).SprintFunc()
var Italic = NewColor(color.Italic).SprintFunc()
var Plus = NewColor(color.FgHiWhite).SprintFunc()("+")
var SubStage = Grey("==>")
var SubSubStage = Grey("-->")
var Matrix = Blue("matrix")
Expand Down Expand Up @@ -91,8 +106,19 @@ var ShellEscapeFunc = function.New(&function.Spec{
},
})

func Color(color string, message string) string {
switch color {
func Color(c string, message string) string {
switch c {
case "error":
return Red(Bold(message))
case "success":
return Green(Bold(message))
case "warn":
return Yellow(Bold(message))
case "warning":
return Yellow(Bold(message))
case "info":
return Blue(Bold(message))

case "green":
return Green(message)
case "red":
Expand All @@ -106,9 +132,30 @@ func Color(color string, message string) string {
case "italic":
return Italic(message)
case "cyan":
return HiCyan(message)
return Cyan(message)
case "grey":
return Grey(message)
case "white":
return White(message)
case "magenta":
return Magenta(message)
case "orange":
return Orange(message)
case "hi-green":
return HiGreen(message)
case "hi-blue":
return HiBlue(message)
case "hi-magenta":
return HiMagenta(message)
case "hi-black":
return HiBlack(message)
case "hi-white":
return HiWhite(message)
case "hi-red":
return HiRed(message)
case "hi-cyan":
return HiCyan(message)

case "hi-yellow":
return HiYellow(message)
default:
Expand Down

0 comments on commit 54334bd

Please sign in to comment.