afmt

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2019 License: MIT Imports: 6 Imported by: 0

README

Advanced formatter

It is an extension of standard Golang formatter. It contains (or it will contain) some advanced features for command line print. All features are described bellow.

Features

  • Line wrap (PrintCol)
  • Tree print (PrintTree)
PrintCol

The PrintCol function (and its derivations) allow wrap text based on integer specification. It means that you can set your stop mark and start print of text on the new line.

afmt.PrintCol(80, "This is my longest text ever.")
PrintTree

The TreePrinter expect any type as argument. It use interfaces, which are analyzed and printed. So you can use string, integer, array, structure and etc. The power of TreePrinter is that you can use any structure and TreePrinter prints it for you. There are many settings, which you can use.

The easiest way is to use function PrintTree:

afmt.PrintTree(myStructure);

On the other hand you can initialize TreePrinter with your own options:

package main

import (
    "fmt"
    "os"
    "github.com/PraserX/afmt"
)

type MyStructure struct {
    Item1 string
    Item2 bool
    Item3 []string
}

func main() {
    var err error
    var result string

    str := MyStructure{}
    str.Item1 = "Lorem ipsum dolor sit amet"
    str.Item2 = false
    str.Item3 = []string{"Lorem", "ipsum"}

    tp := afmt.NewTreePrinter()

    if result, err = tp.Print(testValue); err != nil {
        fmt.Fprintf(os.Stderr, "Hmm something happened: %s", err.Error())
    }

    fmt.Printf(result)
}

So as a result you can see something like:

MyStructure:
├── Item1: Lorem ipsum dolor sit amet
├── Item2: false
└── Item3:
    ├── Lorem
    └── ipsum

Planned features

  • Text padding
  • Progress bar

Is something missing? Create issue and describe your idea!

License

This library is under MIT license.

Documentation

Overview

Package afmt (Advanced formatter or Advanced fmt) implement some extensions for fmt package. The main feature is print to tree structure.

Package afmt (Advanced formatter or Advanced fmt) implement some extensions for fmt package. The main feature is print to tree structure.

Package afmt (Advanced formatter or Advanced fmt) implement some extensions for fmt package. The main feature is print to tree structure.

Package afmt (Advanced formatter or Advanced fmt) implement some extensions for fmt package. The main feature is print to tree structure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FprintCol

func FprintCol(w io.Writer, lineLimit int, a ...interface{}) (n int, err error)

FprintCol formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. Newline is always added in order to respect column maximum characters. It returns the number of bytes written and any write error encountered.

func FprintlnCol

func FprintlnCol(w io.Writer, lineLimit int, a ...interface{}) (n int, err error)

FprintlnCol formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. Newline is always added in order to respect column maximum characters. It returns the number of bytes written and any write error encountered.

func PrintCol

func PrintCol(lineLimit int, a ...interface{}) (n int, err error)

PrintCol formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. Newline is always added in order to respect column maximum characters. It returns the number of bytes written and any write error encountered.

func PrintTree

func PrintTree(structure interface{}) error

PrintTree print input to standard output structured to tree representation.

func PrintlnCol

func PrintlnCol(lineLimit int, a ...interface{}) (n int, err error)

PrintlnCol formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. Newline is always added in order to respect column maximum characters. It returns the number of bytes written and any write error encountered.

Types

type ColPrinter

type ColPrinter struct{}

ColPrinter definition.

func NewColPrinter

func NewColPrinter() *ColPrinter

NewColPrinter allocates new column printer object.

func (*ColPrinter) Print

func (c *ColPrinter) Print(lineLimit int, a ...interface{}) (out string)

Print adding spaces between operands when neither is a string. Newline is always added in order to respect column maximum characters (lineLimit). It return string with maximum length of line based on lineLimit.

type TreePrinter

type TreePrinter struct {
	// contains filtered or unexported fields
}

TreePrinter definition.

func NewTreePrinter

func NewTreePrinter(opts ...TreePrinterOption) *TreePrinter

NewTreePrinter create new TreePrinter `instance`.

func (*TreePrinter) Print

func (p *TreePrinter) Print(inspect interface{}) (string, error)

Print method represents entry point for

type TreePrinterOption

type TreePrinterOption func(*TreePrinterOptions)

TreePrinterOption specification for Printer package.

func TreePrinterOptionIgnoreNames

func TreePrinterOptionIgnoreNames(names []string) TreePrinterOption

TreePrinterOptionIgnoreNames option specification.

func TreePrinterOptionIgnoreUnsupported

func TreePrinterOptionIgnoreUnsupported(ignoreUnsupported bool) TreePrinterOption

TreePrinterOptionIgnoreUnsupported option specification.

func TreePrinterOptionPadding

func TreePrinterOptionPadding(padding uint) TreePrinterOption

TreePrinterOptionPadding option specification.

func TreePrinterOptionPrettyNames

func TreePrinterOptionPrettyNames(names map[string]string) TreePrinterOption

TreePrinterOptionPrettyNames option specification.

type TreePrinterOptions

type TreePrinterOptions struct {
	// Ignore unsupported types, otherwise return error
	IgnoreUnsupported bool
	// Padding by spaces
	Padding uint
	// Pretty name for printer
	PrettyNames map[string]string
	// Ignore specified parts of structure (do not print these ones)
	IgnoreNames []string
}

TreePrinterOptions are used for Server construct function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL