An open API service indexing awesome lists of open source software.

https://github.com/lcaballero/codefmt

A library for buffer with a string format interface.
https://github.com/lcaballero/codefmt

code-generation string-formatting string-interpolation

Last synced: 3 months ago
JSON representation

A library for buffer with a string format interface.

Awesome Lists containing this project

README

        

#+PROPERTY: header-args:sh :prologue "exec 2>&1" :epilogue ":"

#+begin_src shell :results verbatim raw :exports none
# This code block is used to create the html for badges and so is
# hidden on export. Running the block creates a and +begin_html block
# inside of the +RESULT block. Remove the +RESULT before commiting
# updates of the readme.org
./run.sh badges
#+end_src

#+begin_html
GitHub Workflow Action Status   Go Report codefmt (this repo)
#+end_html

* 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

#+begin_src shell :results verbatim raw :exports none
./run.sh funcs
#+end_src

#+begin_src go
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
#+end_src

* Examples

The examples directory includes small programs.

#+begin_src shell :results output
cat ./examples/ex1/main.go
#+end_src

#+begin_src 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()
}
#+end_src

Which can be ran like so:

#+begin_src shell :results output
go run ./examples/ex1/main.go
#+end_src

And outputs the following text:

#+begin_example
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!!!
#+end_example

* Contriubting

See [[CONTRIBUTING.md][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][LICENSE]].