Overview
This is a library based around convenience functions for formatting and buffering strings in the Go programming language.
There is NO GO TEMPLATING used by this library.
Think Sprintf
as the default. And that the output is collected in a
buffer rather than sent to stdout. Which means the f
at the end of
Sprintf
is redundant, and since all method calls are string oriented
the S
is also redundant, because this library is not geared for
low-level IO and strings are good enough, in this case.
API
func NewBuf() *Buf
func (b *Buf) Bytes() []byte
func (b *Buf) String() string
func (b *Buf) Stdout() *Buf
func (b *Buf) Stderr() *Buf
func (b *Buf) Write(format string, args ...any) *Buf
func (b *Buf) Sub(format string, subs map[string]any) *Buf
func (b *Buf) Expand(format string, args ...any) *Buf
func (b *Buf) Writeln(format string, args ...any) *Buf
func (b *Buf) NL() *Buf
func (b *Buf) Preln(format string, args ...any) *Buf
func (b *Buf) Both(format string, args ...any) *Buf
func NewIndent() Indent
func (n Indent) String() string
func (n Indent) HasIndent() bool
func (n Indent) WriteTo(w io.Writer) error
func (n Indent) Next() Indent
func (n Indent) Prev() Indent
func (r Replacer) Replace(pairs ...any) string
func (b BraceTemplate) Replace(pairs ...any) string
func (b BraceTemplate) MapPair(pairs ...string) string
func MapReplacer(s string) Replacer
func ToCamel(s string) string
func ToPascal(s string) string
func ToPairs(args ...any) map[string]any
Examples
The examples directory includes small programs.
cat ./examples/ex1/main.go
package main
import (
"fmt"
"time"
"github.com/lcaballero/codefmt"
)
func main() {
var example uses
example.braceTemplate()
example.mapDirectly()
example.usingBuf()
}
type uses int
func (u uses) usingBuf() {
codefmt.NewBuf().
Write(
"I'll have %d hamburgers with %s, %s, and %s.",
4, "pickle", "ketchup", "mustard").
NL().Stdout()
codefmt.NewBuf().NL().
Writeln(
"Your order number is: %d, should be ready at: %v",
42, time.Now().Add(time.Minute*15).Format(time.Kitchen)).
Stdout()
codefmt.NewBuf().NL().
Expand(`Order for ${num}, "${item}" is ready!!!`,
"num", 42,
"item", "deluxe hamburger",
).
NL().Stdout()
}
func (u uses) mapDirectly() {
works := map[string]string{
"Robert Frost": "The Road Not Taken",
"Maya Angelou": "Stil I Rise",
"Dylan Thomas": "Do Not Go Gentle into that That Good Night",
}
buf := codefmt.NewBuf()
buf.Write("Author/Poems").NL()
for author, poem := range works {
buf.Expand(
"Author: ${author}, Poem: ${poem}",
"author", author, "poem", poem,
).NL()
}
fmt.Println(buf)
}
func (u uses) braceTemplate() {
hw1 := codefmt.BraceTemplate("${greeting}, ${name}!").Replace(
"greeting", "Hello",
"name", "World",
)
fmt.Println(hw1)
fmt.Println()
}
Which can be ran like so:
go run ./examples/ex1/main.go
And outputs the following text:
Hello, World! Author/Poems Author: Robert Frost, Poem: The Road Not Taken Author: Maya Angelou, Poem: Stil I Rise Author: Dylan Thomas, Poem: Do Not Go Gentle into that That Good Night I'll have 4 hamburgers with pickle, ketchup, and mustard. Your order number is: 42, should be ready at: 2:38PM Order for 42, "deluxe hamburger" is ready!!!
Contriubting
See CONTRIBUTING.md. However, this project (at the moment) isn’t following those guidelines simply becasue the level of interest isn’t that high and this lib is quite simple. It is provided for formality’s sake. Just make issues and open PRs for the time being. Keeping it simple for now.
License
MIT License, LICENSE.