Files
esctl/pkg/printer/color.go

67 lines
1.5 KiB
Go
Raw Normal View History

/*
Copyright © 2026 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package printer
import (
2026-07-17 10:09:05 +02:00
"os"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/fatih/color"
2026-07-17 10:09:05 +02:00
"github.com/mattn/go-isatty"
)
var (
green = color.New(color.BgGreen, color.FgHiWhite).SprintFunc()
2026-05-27 09:59:56 +02:00
yellow = color.New(color.BgYellow, color.FgHiWhite).SprintFunc()
orange = color.BgRGB(255, 128, 0).AddRGB(0, 0, 0).SprintFunc()
red = color.New(color.BgRed, color.FgHiWhite).SprintFunc()
bold = color.New(color.Bold).SprintFunc()
)
func Colorize(conf *cfg.Config, col, what string) string {
2026-07-17 10:09:05 +02:00
if !isatty.IsTerminal(os.Stdout.Fd()) {
return what
}
switch conf.Output {
case "json", "yaml":
return what
}
switch col {
case "green":
what = green(what)
case "orange":
what = orange(what)
case "red":
what = red(what)
2026-05-27 09:59:56 +02:00
case "yellow":
what = yellow(what)
}
return what
}
2026-07-17 10:09:05 +02:00
func Bold(what string) string {
if !isatty.IsTerminal(os.Stdout.Fd()) {
return what
}
return bold(what)
}