Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 13.14.0 #524

Merged
merged 9 commits into from
Dec 2, 2024
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Changelog

### [13.14.0](https://kaos.sh/ek/13.14.0)

- `[fmtutil/filetree]` Added experimental package for printing file tree
- `[fmtc]` Added methods `Printfn`, `Fprintfn`, `Sprintfn`, and `LPrintfn`
- `[req]` Added custom encoder for arrays in `Query`
- `[fmtutil/table]` Improved support of data with ANSI escape sequences
- `[fmtc]` `NameColor` marked as deprecated (_use method `AddColor` instead_)

### [13.13.1](https://kaos.sh/ek/13.13.1)

- `[spellcheck]` Distance calculation method now public
Expand Down
41 changes: 41 additions & 0 deletions fmtc/cond_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func (cw CondWrapper) Printf(f string, a ...any) (int, error) {
return Printf(f, a...)
}

// Printfn formats according to a format specifier and writes to standard output with
// the new line at the end. It returns the number of bytes written and any write
// error encountered.
func (cw CondWrapper) Printfn(f string, a ...any) (int, error) {
if !cw {
return 0, nil
}

return Printfn(f, a...)
}

// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string. It returns the
// number of bytes written and any write error encountered.
Expand Down Expand Up @@ -85,6 +96,16 @@ func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error) {
return Fprintf(w, f, a...)
}

// Fprintfn formats according to a format specifier and writes to w with the newline
// at the end. It returns the number of bytes written and any write error encountered.
func (cw CondWrapper) Fprintfn(w io.Writer, f string, a ...any) (int, error) {
if !cw {
return 0, nil
}

return Fprintfn(w, f, a...)
}

// Sprint formats using the default formats for its operands and returns the
// resulting string. Spaces are added between operands when neither is a string.
func (cw CondWrapper) Sprint(a ...any) string {
Expand All @@ -105,6 +126,16 @@ func (cw CondWrapper) Sprintf(f string, a ...any) string {
return Sprintf(f, a...)
}

// Sprintfn formats according to a format specifier and returns the resulting
// string with the newline at the end.
func (cw CondWrapper) Sprintfn(f string, a ...any) string {
if !cw {
return ""
}

return Sprintfn(f, a...)
}

// Sprintln formats using the default formats for its operands and returns the
// resulting string. Spaces are always added between operands and a newline is
// appended.
Expand Down Expand Up @@ -164,6 +195,16 @@ func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error) {
return LPrintf(maxSize, f, a...)
}

// LPrintfn formats according to a format specifier and writes to standard output
// limited by the text size and with the newline at the end
func (cw CondWrapper) LPrintfn(maxSize int, f string, a ...any) (int, error) {
if !cw {
return 0, nil
}

return LPrintfn(maxSize, f, a...)
}

// LPrintln formats using the default formats for its operands and writes to standard
// output limited by the text size
func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error) {
Expand Down
81 changes: 80 additions & 1 deletion fmtc/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,70 @@ func ExamplePrintf() {
Printf("{%6a5acd}%s{!}\n", "slateblue background")
}

func ExamplePrintfn() {
// print colored text
// {!} is tag for style reset
Printfn("{d}%s{!}", "black")
Printfn("{r}%s{!}", "red")
Printfn("{y}%s{!}", "yellow")
Printfn("{b}%s{!}", "blue")
Printfn("{c}%s{!}", "cyan")
Printfn("{m}%s{!}", "magenta")
Printfn("{g}%s{!}", "green")
Printfn("{s}%s{!}", "light grey")

// use text modificators

// light colors
Printfn("{r-}%s{!}", "light red")
Printfn("{r-}%s{!}", "dark grey")

// bold + color
Printfn("{r*}%s{!}", "red")
Printfn("{g*}%s{!}", "green")

// dim
Printfn("{r^}%s{!}", "red")
Printfn("{g^}%s{!}", "green")

// underline
Printfn("{r_}%s{!}", "red")
Printfn("{g_}%s{!}", "green")

// blink
Printfn("{r~}%s{!}", "red")
Printfn("{g~}%s{!}", "green")

// reverse
Printfn("{r@}%s{!}", "red")
Printfn("{g@}%s{!}", "green")

// background color
Printfn("{D}%s{!}", "black")
Printfn("{R}%s{!}", "red")
Printfn("{Y}%s{!}", "yellow")
Printfn("{B}%s{!}", "blue")
Printfn("{C}%s{!}", "cyan")
Printfn("{M}%s{!}", "magenta")
Printfn("{G}%s{!}", "green")
Printfn("{S}%s{!}", "light grey")

// many tags at once
// underline, cyan text with the red background
Printfn("{cR_}%s{!}", "text")

// many tags in once
Printfn("{r}{*}%s{!}", "red and bold")

// 256 colors (# for foreground, % for background)
Printfn("{#201}%s{!}", "pink text")
Printfn("{%201}%s{!}", "pink background")

// 24-bit colors (# for foreground, % for background)
Printfn("{#7cfc00}%s{!}", "lawngreen text")
Printfn("{%6a5acd}%s{!}", "slateblue background")
}

func ExamplePrintln() {
// print colored text
// {!} is tag for style reset
Expand Down Expand Up @@ -239,6 +303,11 @@ func ExampleFprintf() {
Fprintf(os.Stdout, "{g}%s{!}\n", "This is normal message")
}

func ExampleFprintfn() {
Fprintfn(os.Stderr, "{r}%s{!}", "This is error message")
Fprintfn(os.Stdout, "{g}%s{!}", "This is normal message")
}

func ExampleFprintln() {
Fprintln(os.Stderr, "{r}This is error message{!}")
Fprintln(os.Stdout, "{g}This is normal message{!}")
Expand All @@ -254,6 +323,11 @@ func ExampleSprintf() {
fmt.Print(msg)
}

func ExampleSprintfn() {
msg := Sprintfn("{r}%s{!}", "This is error message")
fmt.Print(msg)
}

func ExampleSprintln() {
msg := Sprintln("{r}This is error message{!}")
fmt.Print(msg)
Expand Down Expand Up @@ -289,7 +363,12 @@ func ExampleLPrint() {

func ExampleLPrintf() {
// Only "This is text" will be shown
LPrintf(12, "{r}This is %s {g} with colors{!}", "text")
LPrintf(12, "{r}This is %s {g} with colors{!}\n", "text")
}

func ExampleLPrintfn() {
// Only "This is text" will be shown
LPrintfn(12, "{r}This is %s {g} with colors{!}", "text")
}

func ExampleLPrintln() {
Expand Down
33 changes: 33 additions & 0 deletions fmtc/fmtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ var colorTermEnvVar = env.Var("COLORTERM")
// ////////////////////////////////////////////////////////////////////////////////// //

// NameColor defines or redifines named color
//
// Deprecated: Use method AddColor instead
func NameColor(name, tag string) error {
return AddColor(name, tag)
}

// AddColor defines or redifines named color
func AddColor(name, tag string) error {
if colorsMap == nil {
colorsMap = &sync.Map{}
}
Expand Down Expand Up @@ -192,6 +199,13 @@ func Printf(f string, a ...any) (int, error) {
return fmt.Printf(searchColors(f, -1, DisableColors, true), a...)
}

// Printfn formats according to a format specifier and writes to standard output with
// the new line at the end. It returns the number of bytes written and any write
// error encountered.
func Printfn(f string, a ...any) (int, error) {
return fmt.Printf(searchColors(f, -1, DisableColors, true)+"\n", a...)
}

// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string. It returns the
// number of bytes written and any write error encountered.
Expand All @@ -214,6 +228,12 @@ func Fprintf(w io.Writer, f string, a ...any) (int, error) {
return fmt.Fprintf(w, searchColors(f, -1, DisableColors, true), a...)
}

// Fprintfn formats according to a format specifier and writes to w with the newline
// at the end. It returns the number of bytes written and any write error encountered.
func Fprintfn(w io.Writer, f string, a ...any) (int, error) {
return fmt.Fprintf(w, searchColors(f, -1, DisableColors, true)+"\n", a...)
}

// Sprint formats using the default formats for its operands and returns the
// resulting string. Spaces are added between operands when neither is a string.
func Sprint(a ...any) string {
Expand All @@ -227,6 +247,12 @@ func Sprintf(f string, a ...any) string {
return fmt.Sprintf(searchColors(f, -1, DisableColors, true), a...)
}

// Sprintfn formats according to a format specifier and returns the resulting
// string with the newline at the end.
func Sprintfn(f string, a ...any) string {
return fmt.Sprintf(searchColors(f, -1, DisableColors, true)+"\n", a...)
}

// Sprintln formats using the default formats for its operands and returns the
// resulting string. Spaces are always added between operands and a newline is
// appended.
Expand Down Expand Up @@ -274,6 +300,13 @@ func LPrintf(maxSize int, f string, a ...any) (int, error) {
return fmt.Print(searchColors(s, maxSize, DisableColors, true))
}

// LPrintfn formats according to a format specifier and writes to standard output
// limited by the text size and with the newline at the end
func LPrintfn(maxSize int, f string, a ...any) (int, error) {
s := fmt.Sprintf(f, a...)
return fmt.Print(searchColors(s, maxSize, DisableColors, true) + "\n")
}

// LPrintln formats using the default formats for its operands and writes to standard
// output limited by the text size
func LPrintln(maxSize int, a ...any) (int, error) {
Expand Down
48 changes: 28 additions & 20 deletions fmtc/fmtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,42 +343,50 @@ func (s *FormatSuite) TestAux(c *C) {
func (s *FormatSuite) TestIfHelper(c *C) {
w := bytes.NewBufferString("")

If(false).Print("Print: OK\n")
If(false).Println("Println: OK")
If(false).Printf("Printf: %s\n", "OK")
If(false).Fprint(w, "Fprint\n")
If(false).Fprintln(w, "Fprintln")
If(false).Fprintf(w, "Fprintf: %s\n", "OK")
If(false).Sprint("Sprint: OK\n")
If(false).Sprintln("Sprintln: OK")
If(false).Sprintf("Sprintf: %s\n", "OK")
If(false).TPrint("TPrint: OK\n")
If(false).TPrintln("TPrintln: OK")
If(false).TPrintf("TPrintf: %s\n", "OK")
If(false).LPrint(100, "LPrint: OK\n")
If(false).LPrintln(100, "LPrintln: OK")
If(false).LPrintf(100, "LPrintf: %s\n", "OK")
If(false).TLPrint(100, "TLPrint: OK\n")
If(false).TLPrintln(100, "TLPrintln: OK")
If(false).TLPrintf(100, "TLPrintf: %s\n", "OK")
If(false).Print("Print: NOT OK\n")
If(false).Println("Println: NOT OK")
If(false).Printf("Printf: %s\n", "NOT OK")
If(false).Printfn("Printfn: %s\n", "NOT OK")
If(false).Fprint(w, "Fprint: NOT OK\n")
If(false).Fprintln(w, "Fprintln: NOT OK")
If(false).Fprintf(w, "Fprintf: %s\n", "NOT OK")
If(false).Fprintfn(w, "Fprintfn: %s", "NOT OK")
If(false).Sprint("Sprint: NOT OK\n")
If(false).Sprintln("Sprintln: NOT OK")
If(false).Sprintf("Sprintf: %s\n", "NOT OK")
If(false).Sprintfn("Sprintfn: %s", "NOT OK")
If(false).TPrint("TPrint: NOT OK\n")
If(false).TPrintln("TPrintln: NOT OK")
If(false).TPrintf("TPrintf: %s\n", "NOT OK")
If(false).LPrint(100, "LPrint: NOT OK\n")
If(false).LPrintln(100, "LPrintln: NOT OK")
If(false).LPrintf(100, "LPrintf: %s\n", "NOT OK")
If(false).LPrintfn(100, "LPrintfn: %s", "NOT OK")
If(false).TLPrint(100, "TLPrint: NOT OK\n")
If(false).TLPrintln(100, "TLPrintln: NOT OK")
If(false).TLPrintf(100, "TLPrintf: %s\n", "NOT OK")
If(false).NewLine()
If(false).Bell()

If(true).Print("Print: OK\n")
If(true).Println("Println: OK")
If(true).Printf("Printf: %s\n", "OK")
If(true).Fprint(w, "Fprint\n")
If(true).Fprintln(w, "Fprintln")
If(true).Printfn("Printfn: %s\n", "OK")
If(true).Fprint(w, "Fprint: OK\n")
If(true).Fprintln(w, "Fprintln: OK")
If(true).Fprintf(w, "Fprintf: %s\n", "OK")
If(true).Fprintfn(w, "Fprintfn: %s", "OK")
If(true).Sprint("Sprint: OK\n")
If(true).Sprintln("Sprintln: OK")
If(true).Sprintf("Sprintf: %s\n", "OK")
If(true).Sprintfn("Sprintf: %s", "OK")
If(true).TPrint("TPrint: OK\n")
If(true).TPrintln("TPrintln: OK")
If(true).TPrintf("TPrintf: %s\n", "OK")
If(true).LPrint(100, "LPrint: OK\n")
If(true).LPrintln(100, "LPrintln: OK")
If(true).LPrintf(100, "LPrintf: %s\n", "OK")
If(true).LPrintfn(100, "LPrintfn: %s", "OK")
If(true).TLPrint(100, "TLPrint: OK\n")
If(true).TLPrintln(100, "TLPrintln: OK")
If(true).TLPrintf(100, "TLPrintf: %s\n", "OK")
Expand Down
Loading
Loading