Commit d0f93565 authored by Miles Whittaker's avatar Miles Whittaker 🏘
Browse files

Added support for Fprint family for all ANSI codes

parent c4587c2e
......@@ -11,15 +11,15 @@ import (
// Helpers begin
func err(msg string) { hl.PrintlnRedf("[!] %s", msg) }
func err(msg string) { hl.PrintfRed("[!] %s\n", msg) }
func errx(status int, msg string) {
err(msg)
os.Exit(status)
}
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) }
func good(msg string) { hl.PrintfGreen("[+] %s\n", msg) }
func info(msg string) { hl.PrintfWhite("[*] %s\n", msg) }
func subinfo(msg string) { hl.PrintfCyan("[=] %s\n", msg) }
func warn(msg string) { hl.PrintfYellow("[-] %s\n", msg) }
// Helpers end
......
......@@ -39,101 +39,11 @@ func Printf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
// PrintHex will take a hex color code and print a string with ANSI
// escape codes.
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) {
Print(Hilight(code, str))
}
// PrintHilights will print a string with ANSI escape codes.
func PrintHilights(codes []string, str string) {
// Apply all specified color codes
for _, code := range codes {
str = Hilight(code, str)
}
// Print the result
Print(str)
}
// Println wraps fmt.Println().
func Println(args ...interface{}) {
fmt.Println(args...)
}
// PrintlnHex will take a hex color code and print a line with ANSI
// escape codes.
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) {
Println(Hilight(code, str))
}
// PrintlnHilights will print a line with ANSI escape codes.
func PrintlnHilights(codes []string, str string) {
// Apply all specified color codes
for _, code := range codes {
str = Hilight(code, str)
}
// Print the result
Println(str)
}
// PrintlnOnHex will take a hex color code and print a line with ANSI
// escape codes.
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) {
Println(OnRainbow(str))
}
// PrintlnRainbow will print a line rotating through ANSI color codes
// for a rainbow effect.
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) {
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) {
Print(OnHex(hex, str))
}
// PrintOnRainbow will print a string rotating through ANSI color
// codes for a rainbow effect.
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) {
Print(Rainbow(str))
}
// PrintWrap will wrap a string to the specified width and print it.
func PrintWrap(width int, str string) {
Print(Wrap(width, str))
}
// Sprint wraps fmt.Sprint().
func Sprint(args ...interface{}) string {
return fmt.Sprint(args...)
......
This diff is collapsed.
......@@ -90,7 +90,7 @@ var Modes = map[string]string{
}
// Version is the package version
const Version = "1.7.0"
const Version = "1.8.0"
// Various regular expressions
var allCodes = regexp.MustCompile(`\x1b\[([0-9;]*m|K)`)
......
......@@ -347,7 +347,7 @@ func Table() {
for i := 0; i < 16; i++ {
bg = Sprintf("on_color_%03d", i)
PrintHilightf(
PrintfHilight(
bg,
" %s %s ",
Blackf("%03d", i),
......@@ -360,7 +360,7 @@ func Table() {
for i := 16; i < 256; i++ {
bg = Sprintf("on_color_%03d", i)
PrintHilightf(
PrintfHilight(
bg,
" %s %s ",
Blackf("%03d", i),
......
......@@ -23,23 +23,7 @@ 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() {
generate_ansi_funcs() {
local code="$1"
local func="$(echo "${code^}" | sed -r "s/_(.)/\u\1/g")"
......@@ -50,24 +34,51 @@ 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) {
Print(Hilight("$code", str))
$(generate_print_funcs "$func" "")
EOF
}
// Println$func will Hilight() the provided string with the
// specified ANSI code and call fmt.Println(args ...interface{}).
func Println$func(str string) {
Println(Hilight("$code", str))
generate_print_funcs() {
local func="$1"
local var="${2:+${2%% *}, }"
local vardef="${2:+$2, }"
cat <<EOF
// ${func}f wraps fmt.Sprintf() and $func.
func ${func}f(${vardef}format string, args ...interface{}) string {
return $func(${var}Sprintf(format, args...))
}
$(
generate_format_func "$func" "" "string"
generate_format_func "Print$func" ""
generate_format_func "Println$func" ""
)
// Fprint$func wraps $func() and fmt.Fprint().
func Fprint$func(w io.Writer, ${vardef}str string) {
Fprint(w, $func(${var}str))
}
// Fprintf$func wraps $func and fmt.Fprintf().
func Fprintf$func(w io.Writer, ${vardef}format string, args ...interface{}) {
Fprint$func(w, ${var}Sprintf(format, args...))
}
// Fprintln$func wraps $func() and fmt.Fprintln().
func Fprintln$func(w io.Writer, ${vardef}str string) {
Fprintln(w, $func(${var}str))
}
// Print$func wraps $func() and fmt.Print().
func Print$func(${vardef}str string) {
Print($func(${var}str))
}
// Printf$func wraps $func() and fmt.Printf().
func Printf$func(${vardef}format string, args ...interface{}) {
Print$func(${var}Sprintf(format, args...))
}
// Println$func wraps $func() and fmt.Println().
func Println$func(${vardef}str string) {
Println($func(${var}str))
}
EOF
}
......@@ -116,66 +127,53 @@ cat >"$file" <<EOF
// Code generated by ${0#./}; DO NOT EDIT.
package hilighter
import "io"
$(
for color in black red green yellow blue magenta cyan white; do
generate_funcs "$color"
generate_funcs "on_$color"
generate_funcs "light_$color"
generate_funcs "on_light_$color"
generate_ansi_funcs "$color"
generate_ansi_funcs "on_$color"
generate_ansi_funcs "light_$color"
generate_ansi_funcs "on_light_$color"
done; unset color
generate_funcs default
generate_funcs on_default
generate_ansi_funcs default
generate_ansi_funcs on_default
for i in $(seq -w 0 255); do
generate_funcs "color_$i"
generate_funcs "on_color_$i"
generate_ansi_funcs "color_$i"
generate_ansi_funcs "on_color_$i"
done; unset i
)
$(
generate_funcs reset
generate_funcs normal
generate_ansi_funcs reset
generate_ansi_funcs normal
for mode in \
bold dim faint italic underline blink blink_slow blink_rapid \
inverse negative swap hide conceal crossed_out strikethrough \
fraktur
do
generate_funcs "$mode"
generate_funcs "no_$mode"
generate_ansi_funcs "$mode"
generate_ansi_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"
generate_print_funcs "Hex" "hex string"
generate_print_funcs "Hilight" "code string"
generate_print_funcs "Hilights" "codes []string"
generate_print_funcs "OnHex" "hex string"
generate_print_funcs "OnRainbow" ""
generate_print_funcs "Plain" ""
generate_print_funcs "Rainbow" ""
generate_print_funcs "Wrap" "width int"
)
EOF
#$(
# generate_ansi_funcs "green"
# generate_print_funcs "Hex" "hex string"
#)
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