Giter Site home page Giter Site logo

codefmt's Introduction

GitHub Workflow Action Status   Go Report codefmt (this repo)

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.

codefmt's People

Contributors

lcaballero avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.