Commit 2355c767 authored by Miles Whittaker's avatar Miles Whittaker 🏘
Browse files

Much better handling of format strings

parent 8d100d05
......@@ -11,15 +11,15 @@ import (
// Helpers begin
func err(msg string) { hl.PrintlnRed("[!] %s", msg) }
func err(msg string) { hl.PrintlnRedf("[!] %s", msg) }
func errx(status int, msg string) {
err(msg)
os.Exit(status)
}
func good(msg string) { hl.PrintlnGreen("[+] %s", msg) }
func info(msg string) { hl.PrintlnWhite("[*] %s", msg) }
func subinfo(msg string) { hl.PrintlnCyan("[=] %s", msg) }
func warn(msg string) { hl.PrintlnYellow("[-] %s", msg) }
func good(msg string) { hl.PrintlnGreenf("[+] %s", msg) }
func info(msg string) { hl.PrintlnWhitef("[*] %s", msg) }
func subinfo(msg string) { hl.PrintlnCyanf("[=] %s", msg) }
func warn(msg string) { hl.PrintlnYellowf("[-] %s", msg) }
// Helpers end
......
......@@ -14,26 +14,24 @@ func Printf(str string, args ...interface{}) {
// PrintHex will take a hex color code and print a string with ANSI
// escape codes.
func PrintHex(hex string, str string, args ...interface{}) {
fmt.Print(Hex(hex, str, args...))
func PrintHex(hex string, str string) {
Print(Hex(hex, str))
}
// PrintHilight will print a string with an ANSI escape code.
func PrintHilight(code string, str string, args ...interface{}) {
fmt.Print(Hilight(code, str, args...))
func PrintHilight(code string, str string) {
Print(Hilight(code, str))
}
// PrintHilights will print a string with ANSI escape codes.
func PrintHilights(codes []string, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)
func PrintHilights(codes []string, str string) {
// Apply all specified color codes
for _, code := range codes {
str = Hilight(code, str)
}
// Print the result
fmt.Print(str)
Print(str)
}
// Println wraps fmt.Println(args ...interface{}).
......@@ -43,76 +41,70 @@ func Println(args ...interface{}) {
// PrintlnHex will take a hex color code and print a line with ANSI
// escape codes.
func PrintlnHex(hex string, str string, args ...interface{}) {
fmt.Println(Hex(hex, str, args...))
func PrintlnHex(hex string, str string) {
Println(Hex(hex, str))
}
// PrintlnHilight will print a line with an ANSI escape code.
func PrintlnHilight(code string, str string, args ...interface{}) {
fmt.Println(Hilight(code, str, args...))
func PrintlnHilight(code string, str string) {
Println(Hilight(code, str))
}
// PrintlnHilights will print a line with ANSI escape codes.
func PrintlnHilights(
codes []string,
str string,
args ...interface{},
) {
str = fmt.Sprintf(str, args...)
func PrintlnHilights(codes []string, str string) {
// Apply all specified color codes
for _, code := range codes {
str = Hilight(code, str)
}
// Print the result
fmt.Println(str)
Println(str)
}
// PrintlnOnHex will take a hex color code and print a line with ANSI
// escape codes.
func PrintlnOnHex(hex string, str string, args ...interface{}) {
fmt.Println(OnHex(hex, str, args...))
func PrintlnOnHex(hex string, str string) {
Println(OnHex(hex, str))
}
// PrintlnOnRainbow will print a line rotating through ANSI color
// codes for a rainbow effect.
func PrintlnOnRainbow(str string, args ...interface{}) {
fmt.Println(OnRainbow(str, args...))
func PrintlnOnRainbow(str string) {
Println(OnRainbow(str))
}
// PrintlnRainbow will print a line rotating through ANSI color codes
// for a rainbow effect.
func PrintlnRainbow(str string, args ...interface{}) {
fmt.Println(Rainbow(str, args...))
func PrintlnRainbow(str string) {
Println(Rainbow(str))
}
// PrintlnWrap will wrap a line to the specified width and print it.
func PrintlnWrap(width int, str string, args ...interface{}) {
fmt.Println(Wrap(width, str, args...))
func PrintlnWrap(width int, str string) {
Println(Wrap(width, str))
}
// PrintOnHex will take a hex color code and print a line with ANSI
// escape codes.
func PrintOnHex(hex string, str string, args ...interface{}) {
fmt.Print(OnHex(hex, str, args...))
func PrintOnHex(hex string, str string) {
Print(OnHex(hex, str))
}
// PrintOnRainbow will print a string rotating through ANSI color
// codes for a rainbow effect.
func PrintOnRainbow(str string, args ...interface{}) {
fmt.Print(OnRainbow(str, args...))
func PrintOnRainbow(str string) {
Print(OnRainbow(str))
}
// PrintRainbow will print a string rotating through ANSI color codes
// for a rainbow effect.
func PrintRainbow(str string, args ...interface{}) {
fmt.Print(Rainbow(str, args...))
func PrintRainbow(str string) {
Print(Rainbow(str))
}
// PrintWrap will wrap a string to the specified width and print it.
func PrintWrap(width int, str string, args ...interface{}) {
fmt.Print(Wrap(width, str, args...))
func PrintWrap(width int, str string) {
Print(Wrap(width, str))
}
// Sprint wraps fmt.Sprint(args ...interface{}).
......
This diff is collapsed.
......@@ -90,7 +90,7 @@ var Modes = map[string]string{
}
// Version is the package version
const Version = "1.5.13"
const Version = "1.6.0"
// Various regular expressions
var allCodes = regexp.MustCompile(`\x1b\[([0-9;]*m|K)`)
......
......@@ -8,13 +8,13 @@ import (
"strings"
)
func bgColor(code string, str string, args ...interface{}) string {
func bgColor(code string, str string) string {
var colorized string
// Strip all other bg color codes and don't extend bg color over
// newlines
str = newline.ReplaceAllString(
plainBg(str, args...),
plainBg(str),
"\x1b["+Colors["on_default"]+"m\n\x1b["+Colors[code]+"m",
)
......@@ -26,26 +26,26 @@ func bgColor(code string, str string, args ...interface{}) string {
return onlyCodes.ReplaceAllString(colorized, "$1$4")
}
func colorize(clr string, str string, args ...interface{}) string {
func colorize(clr string, str string) string {
if Disable {
// Return the string w/o any color codes
return Plain(str, args...)
return Plain(str)
}
// Call the appropriate function
if strings.HasPrefix(clr, "on_") {
return bgColor(clr, str, args...)
return bgColor(clr, str)
}
return fgColor(clr, str, args...)
return fgColor(clr, str)
}
func fgColor(code string, str string, args ...interface{}) string {
func fgColor(code string, str string) string {
var colorized string
// Strip all other fg color codes and don't extend fg color over
// newlines
str = newline.ReplaceAllString(
plainFg(str, args...),
plainFg(str),
"\x1b["+Colors["default"]+"m\n\x1b["+Colors[code]+"m",
)
......@@ -59,8 +59,8 @@ func fgColor(code string, str string, args ...interface{}) string {
// Hex will take a hex color code and add the appropriate ANSI color
// codes to the provided string.
func Hex(hex string, str string, args ...interface{}) string {
return colorize(hexToXterm256(hex), str, args...)
func Hex(hex string, str string) string {
return colorize(hexToXterm256(hex), str)
}
// Convert hex to xterm-256 8-bit value
......@@ -167,7 +167,7 @@ func hexToXterm256(hex string) string {
// Hilight will add the appropriate ANSI code to the specified
// string.
func Hilight(code string, str string, args ...interface{}) string {
func Hilight(code string, str string) string {
var clr string
var hasKey bool
var matches [][]string
......@@ -175,17 +175,17 @@ func Hilight(code string, str string, args ...interface{}) string {
// Call the appropriate function
if _, hasKey = Colors[code]; hasKey {
return colorize(code, str, args...)
return colorize(code, str)
} else if _, hasKey = Modes[code]; hasKey {
return modify(code, str, args...)
return modify(code, str)
} else {
switch code {
case "on_rainbow":
return OnRainbow(str, args...)
return OnRainbow(str)
case "plain":
return Plain(str, args...)
return Plain(str)
case "rainbow":
return Rainbow(str, args...)
return Rainbow(str)
default:
// Check if hex color code
matches = hexCode.FindAllStringSubmatch(code, -1)
......@@ -194,7 +194,7 @@ func Hilight(code string, str string, args ...interface{}) string {
if strings.HasPrefix(code, "on_") {
clr = "on_" + clr
}
return colorize(clr, str, args...)
return colorize(clr, str)
}
// Check if wrap
......@@ -205,7 +205,7 @@ func Hilight(code string, str string, args ...interface{}) string {
if len(match) == 3 && len(match[2]) > 0 {
width, _ = strconv.Atoi(match[2])
}
return Wrap(width, str, args...)
return Wrap(width, str)
}
// Otherwise panic
......@@ -216,13 +216,7 @@ func Hilight(code string, str string, args ...interface{}) string {
// Hilights will add the appropriate ANSI codes to the specified
// string.
func Hilights(
codes []string,
str string,
args ...interface{},
) string {
str = Sprintf(str, args...)
func Hilights(codes []string, str string) string {
// Apply all specified color codes
for _, code := range codes {
str = Hilight(code, str)
......@@ -231,10 +225,10 @@ func Hilights(
return str
}
func modify(mode string, str string, args ...interface{}) string {
func modify(mode string, str string) string {
if Disable {
// Return the string w/o any color codes
return Plain(str, args...)
return Plain(str)
}
var hasKey bool
......@@ -267,7 +261,7 @@ func modify(mode string, str string, args ...interface{}) string {
// Remove other occurrences of specified mode and opposite
r = regexp.MustCompile(`\x1b\[(` + rm + ")m")
str = r.ReplaceAllString(Sprintf(str, args...), "")
str = r.ReplaceAllString(str, "")
// Wrap the whole thing with specified color code
modified = "\x1b[" + Modes[mode] + "m" + str + off
......@@ -278,16 +272,16 @@ func modify(mode string, str string, args ...interface{}) string {
// OnHex will take a hex color code and add the appropriate ANSI color
// codes to the provided string.
func OnHex(hex string, str string, args ...interface{}) string {
return colorize("on_"+hexToXterm256(hex), str, args...)
func OnHex(hex string, str string) string {
return colorize("on_"+hexToXterm256(hex), str)
}
// OnRainbow will add multiple ANSI color codes to a string for a
// rainbow effect.
func OnRainbow(str string, args ...interface{}) string {
func OnRainbow(str string) string {
if Disable {
// Return the string w/o any color codes
return Plain(str, args...)
return Plain(str)
}
var chars []string
......@@ -299,7 +293,7 @@ func OnRainbow(str string, args ...interface{}) string {
var out []string
// Strip all other bg color codes and split on newline
lines = strings.Split(plainBg(str, args...), "\n")
lines = strings.Split(plainBg(str), "\n")
// Loop thru lines and apply bg color codes
colors = rainbowColors()
......@@ -324,25 +318,25 @@ func OnRainbow(str string, args ...interface{}) string {
}
// Plain will strip all ANSI color codes from a string.
func Plain(str string, args ...interface{}) string {
func Plain(str string) string {
// Strip all color codes
return allCodes.ReplaceAllString(Sprintf(str, args...), "")
return allCodes.ReplaceAllString(str, "")
}
func plainBg(str string, args ...interface{}) string {
return bgCodes.ReplaceAllString(Sprintf(str, args...), "")
func plainBg(str string) string {
return bgCodes.ReplaceAllString(str, "")
}
func plainFg(str string, args ...interface{}) string {
return fgCodes.ReplaceAllString(Sprintf(str, args...), "")
func plainFg(str string) string {
return fgCodes.ReplaceAllString(str, "")
}
// Rainbow will add multiple ANSI color codes to a string for a
// rainbow effect.
func Rainbow(str string, args ...interface{}) string {
func Rainbow(str string) string {
if Disable {
// Return the string w/o any color codes
return Plain(str, args...)
return Plain(str)
}
var chars []string
......@@ -353,7 +347,7 @@ func Rainbow(str string, args ...interface{}) string {
var out []string
// Strip all other fg color codes and split on newline
lines = strings.Split(plainFg(str, args...), "\n")
lines = strings.Split(plainFg(str), "\n")
// Loop thru lines and apply fg color codes
colors = rainbowColors()
......@@ -402,13 +396,11 @@ func Table() {
for i := 0; i < 16; i++ {
bg = Sprintf("on_color_%03d", i)
Print(
colorize(
bg,
" %s %s ",
Black("%03d", i),
White("%03d", i),
),
PrintHilightf(
bg,
" %s %s ",
Blackf("%03d", i),
Whitef("%03d", i),
)
if (i+1)%8 == 0 {
Print("\n")
......@@ -417,13 +409,11 @@ func Table() {
for i := 16; i < 256; i++ {
bg = Sprintf("on_color_%03d", i)
Print(
colorize(
bg,
" %s %s ",
Black("%03d", i),
White("%03d", i),
),
PrintHilightf(
bg,
" %s %s ",
Blackf("%03d", i),
Whitef("%03d", i),
)
if (i-15)%6 == 0 {
Print("\n")
......@@ -432,14 +422,14 @@ func Table() {
}
// Wrap will wrap a string to the specified width.
func Wrap(width int, str string, args ...interface{}) string {
func Wrap(width int, str string) string {
var lc int
var line = ""
var lines []string
var wc int
var words = strings.Fields(str)
str = Sprintf(str, args...)
str = Sprintf(str)
// Loop thru words
for _, word := range words {
......
......@@ -23,6 +23,22 @@ subinfo() { echo -e "${color:+\e[36m}[=] $*\e[0m"; }
warn() { echo -e "${color:+\e[33m}[-] $*\e[0m"; }
### Helpers end
generate_format_func() {
local func="${1}f"
local var="$2"
local ret
[[ $# -eq 2 ]] || ret="true"
cat <<EOF
// $func wraps $1 and works with format
// strings.
func $func(${var:+$var, }str string, args ...interface{}) ${ret:+string }{
${ret:+return }$1(${var:+${var%% *}, }Sprintf(str, args...))
}
EOF
}
generate_funcs() {
local code="$1"
local func="$(echo "${code^}" | sed -r "s/_(.)/\u\1/g")"
......@@ -30,22 +46,28 @@ generate_funcs() {
cat <<EOF
// $func will Hilight() the provided string with the
// specified ANSI code.
func $func(str string, args ...interface{}) string {
return Hilight("$code", str, args...)
func $func(str string) string {
return Hilight("$code", str)
}
// Print$func will Hilight() the provided string with the
// specified ANSI code and call fmt.Print(args ...interface{}).
func Print$func(str string, args ...interface{}) {
Print(Hilight("$code", str, args...))
func Print$func(str string) {
Print(Hilight("$code", str))
}
// Println$func will Hilight() the provided string with the
// specified ANSI code and call fmt.Println(args ...interface{}).
func Println$func(str string, args ...interface{}) {
Println(Hilight("$code", str, args...))
func Println$func(str string) {
Println(Hilight("$code", str))
}
$(
generate_format_func "$func" "" "string"
generate_format_func "Print$func" ""
generate_format_func "Println$func" ""
)
EOF
}
......@@ -124,6 +146,36 @@ $(
generate_funcs "no_$mode"
done; unset mode
)
$(
generate_format_func "Hex" "hex string" "string"
generate_format_func "Hilight" "code string" "string"
generate_format_func "Hilights" "codes []string" "string"
generate_format_func "OnHex" "hex string" "string"
generate_format_func "OnRainbow" "" "string"
generate_format_func "Plain" "" "string"
generate_format_func "Rainbow" "" "string"
generate_format_func "Wrap" "width int" "string"
generate_format_func "PrintHex" "hex string"
generate_format_func "PrintOnHex" "hex string"
generate_format_func "PrintlnHex" "hex string"
generate_format_func "PrintlnOnHex" "hex string"
generate_format_func "PrintHilight" "code string"
generate_format_func "PrintlnHilight" "code string"
generate_format_func "PrintHilights" "codes []string"
generate_format_func "PrintlnHilights" "codes []string"
generate_format_func "PrintRainbow" ""
generate_format_func "PrintOnRainbow" ""
generate_format_func "PrintlnRainbow" ""
generate_format_func "PrintlnOnRainbow" ""
generate_format_func "PrintWrap" "width int"
generate_format_func "PrintlnWrap" "width int"
)
EOF
go fmt "$file"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment