formatalign

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: Apache-2.0 Imports: 2 Imported by: 2

README

format-align

Golang string format align implement

Installation

go get -u github.com/coolstina/format-align

Feature

  • Alignment: Support Right/Left align
  • Placeholder: Support specify placeholder length
  • Separator: Support specify separator

Example

Use default argument.

Use default argument.

package main

import (
	"bytes"
	"fmt"

	formatalign "github.com/coolstina/format-align"
)

func main() {
	align := formatalign.NewFormatAlign()
	buffer := bytes.Buffer{}

	buffer.Write(append([]byte(align.Format("Username", "helloshaohua")), '\n'))
	buffer.Write(append([]byte(align.Format("Sex", "male")), '\n'))
	buffer.Write(append([]byte(align.Format("IsStudent", false)), '\n'))

	fmt.Println(buffer.String())
}
Username                  : helloshaohua
Sex                       : male
IsStudent                 : false
Alignment of right.
package main

import (
	"bytes"
	"fmt"

	formatalign "github.com/coolstina/format-align"
)

func main() {
	align := formatalign.NewFormatAlign(formatalign.WithAlignment(formatalign.AlignmentOfRight))
	buffer := bytes.Buffer{}

	buffer.Write(append([]byte(align.Format("Username", "helloshaohua")), '\n'))
	buffer.Write(append([]byte(align.Format("Sex", "male")), '\n'))
	buffer.Write(append([]byte(align.Format("IsStudent", false)), '\n'))

	fmt.Println(buffer.String())
}
                  Username: helloshaohua
                       Sex: male
                 IsStudent: false
Specify placeholder.
package main

import (
	"bytes"
	"fmt"

	formatalign "github.com/coolstina/format-align"
)

func main() {
	align := formatalign.NewFormatAlign(
		formatalign.WithAlignment(formatalign.AlignmentOfRight),
		formatalign.WithPlaceholder(12),
	)
	buffer := bytes.Buffer{}

	buffer.Write(append([]byte(align.Format("Username", "helloshaohua")), '\n'))
	buffer.Write(append([]byte(align.Format("Sex", "male")), '\n'))
	buffer.Write(append([]byte(align.Format("IsStudent", false)), '\n'))

	fmt.Println(buffer.String())
}
    Username: helloshaohua
         Sex: male
   IsStudent: false
Specify separator.
package main

import (
	"bytes"
	"fmt"

	formatalign "github.com/coolstina/format-align"
)

func main() {
	align := formatalign.NewFormatAlign(
		formatalign.WithAlignment(formatalign.AlignmentOfRight),
		formatalign.WithPlaceholder(12),
		formatalign.WithSeparator("-"),
	)
	buffer := bytes.Buffer{}

	buffer.Write(append([]byte(align.Format("Username", "helloshaohua")), '\n'))
	buffer.Write(append([]byte(align.Format("Sex", "male")), '\n'))
	buffer.Write(append([]byte(align.Format("IsStudent", false)), '\n'))

	fmt.Println(buffer.String())
}
    Username- helloshaohua
         Sex- male
   IsStudent- false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alignment

type Alignment string
const (
	AlignmentOfRight Alignment = "right"
	AlignmentOfLeft  Alignment = "left"
)

func (Alignment) Is

func (alignment Alignment) Is(align Alignment) bool

type FormatAlign

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

func NewFormatAlign

func NewFormatAlign(options ...Option) *FormatAlign

func (*FormatAlign) Format

func (align *FormatAlign) Format(name string, value interface{}) string

func (*FormatAlign) SetPlaceholder

func (align *FormatAlign) SetPlaceholder(placeholder int) *FormatAlign

func (*FormatAlign) SetSeparator

func (align *FormatAlign) SetSeparator(separator string) *FormatAlign

type Option

type Option interface {
	// contains filtered or unexported methods
}

type OptionFunc

type OptionFunc func(formatAlign *FormatAlign)

func WithAlignment

func WithAlignment(alignment Alignment) OptionFunc

func WithPlaceholder

func WithPlaceholder(placeholder int) OptionFunc

func WithSeparator

func WithSeparator(separator string) OptionFunc

Directories

Path Synopsis
example
section1 command
section2 command
section3 command
section4 command

Jump to

Keyboard shortcuts

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