mirror of
https://github.com/Jguer/yay.git
synced 2024-11-06 09:07:21 +01:00
Add common stylize
function for reduce code repeation
Also check for input length, don't create empty stylized strings.
This commit is contained in:
parent
46cffa6ba6
commit
1e4c029a2e
56
print.go
56
print.go
@ -362,60 +362,52 @@ func formatTime(i int) string {
|
||||
return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
|
||||
}
|
||||
|
||||
func red(in string) string {
|
||||
const (
|
||||
redCode = "\x1b[31m"
|
||||
greenCode = "\x1b[32m"
|
||||
yellowCode = "\x1b[33m"
|
||||
blueCode = "\x1b[34m"
|
||||
magentaCode = "\x1b[35m"
|
||||
cyanCode = "\x1b[36m"
|
||||
boldCode = "\x1b[1m"
|
||||
|
||||
resetCode = "\x1b[0m"
|
||||
)
|
||||
|
||||
func stylize(startCode, in string) string {
|
||||
if useColor {
|
||||
return "\x1b[31m" + in + "\x1b[0m"
|
||||
return startCode + in + resetCode
|
||||
}
|
||||
|
||||
return in
|
||||
}
|
||||
|
||||
func red(in string) string {
|
||||
return stylize(redCode, in)
|
||||
}
|
||||
|
||||
func green(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[32m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(greenCode, in)
|
||||
}
|
||||
|
||||
func yellow(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[33m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(yellowCode, in)
|
||||
}
|
||||
|
||||
func blue(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[34m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(blueCode, in)
|
||||
}
|
||||
|
||||
func cyan(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[36m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(cyanCode, in)
|
||||
}
|
||||
|
||||
func magenta(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[35m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(magentaCode, in)
|
||||
}
|
||||
|
||||
func bold(in string) string {
|
||||
if useColor {
|
||||
return "\x1b[1m" + in + "\x1b[0m"
|
||||
}
|
||||
|
||||
return in
|
||||
return stylize(boldCode, in)
|
||||
}
|
||||
|
||||
// Colours text using a hashing algorithm. The same text will always produce the
|
||||
|
Loading…
Reference in New Issue
Block a user