fmtutil

package
v12.73.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: Apache-2.0 Imports: 10 Imported by: 16

Documentation

Overview

Package fmtutil provides methods for output formatting

Index

Examples

Constants

This section is empty.

Variables

View Source
var OrderSeparator = ","

OrderSeparator is a default order separator

View Source
var SeparatorColorTag = "{s}"

SeparatorColorTag is fmtc color tag used for separator (light gray by default)

View Source
var SeparatorFullscreen = false

SeparatorFullscreen allow enabling full-screen separator

View Source
var SeparatorSize = 88

SeparatorSize contains size of separator

View Source
var SeparatorSymbol = "-"

SeparatorSymbol used for separator generation

View Source
var SeparatorTitleColorTag = "{s}"

SeparatorTitleColorTag is fmtc color tag used for separator title (light gray by default)

View Source
var SizeSeparator = ""

SizeSeparator is a default size separator

Functions

func Align added in v12.65.0

func Align(text string, alignment Alignment, size int) string

Align can align text with ANSI control sequences (for example colors)

func ColorizePassword

func ColorizePassword(password, letterTag, numTag, specialTag string) string

ColorizePassword adds different fmtc color tags for numbers and letters

Example
password := ">+XY!b3Rog"

fmt.Println(ColorizePassword(password, "{r}", "{y}", "{c}"))
Output:

{c}>+{r}XY{c}!{r}b{y}3{r}Rog{!}

func CountDigits

func CountDigits(i int) int

CountDigits returns number of digits in integer

func Float

func Float(f float64) float64

Float formats float numbers more nicely

Example
f1 := 0.3145
f2 := 3.452
f3 := 135.5215

fmt.Printf("%f → %g\n", f1, Float(f1))
fmt.Printf("%f → %g\n", f2, Float(f2))
fmt.Printf("%f → %g\n", f3, Float(f3))
Output:

0.314500 → 0.31
3.452000 → 3.45
135.521500 → 135.5

func ParseSize

func ParseSize(size string) uint64

ParseSize parses size and return it in bytes (e.g 1.34 Mb -> 1478182)

Example
s1 := "160"
s2 := "34Mb"
s3 := "2.2 GB"

fmt.Printf("%s → %d\n", s1, ParseSize(s1))
fmt.Printf("%s → %d\n", s2, ParseSize(s2))
fmt.Printf("%s → %d\n", s3, ParseSize(s3))
Output:

160 → 160
34Mb → 35651584
2.2 GB → 2362232012

func PrettyBool

func PrettyBool(b bool, vals ...string) string

PrettyBool formats boolean to "pretty" form (e.g true/false -> Y/N)

Example
fmt.Printf("%t → %s\n", true, PrettyBool(true))
fmt.Printf("%t → %s\n", false, PrettyBool(false))
fmt.Printf("%t → %s\n", true, PrettyBool(true, "Yep", "Nope"))
fmt.Printf("%t → %s\n", false, PrettyBool(false, "Yep", "Nope"))
Output:

true → Y
false → N
true → Yep
false → Nope

func PrettyDiff

func PrettyDiff(i int, separator ...string) string

PrettyDiff formats number to "pretty" form with + or - symbol at the beginning

func PrettyNum

func PrettyNum(i any, separator ...string) string

PrettyNum formats number to "pretty" form (e.g 1234567 -> 1,234,567)

Example
var (
	n1 int     = 10
	n2 uint    = 5000
	n3 float64 = 6128750.26
)

fmt.Printf("%d → %s\n", n1, PrettyNum(n1))
fmt.Printf("%d → %s\n", n2, PrettyNum(n2))
fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3))

// Set default order separator
OrderSeparator = " "

fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3))

// Use custom order separator
fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3, "|"))
Output:

10 → 10
5000 → 5,000
6128750.26 → 6,128,750.26
6128750.26 → 6 128 750.26
6128750.26 → 6|128|750.26

func PrettyPerc

func PrettyPerc(i float64) string

PrettyNum formats float value to "pretty" percent form (e.g 12.3423 -> 12.3%)

Example
var (
	n1 float64 = 0.123
	n2 float64 = 10.24
	n3 float64 = 1294.193
)

OrderSeparator = ","

fmt.Printf("%f → %s\n", n1, PrettyPerc(n1))
fmt.Printf("%f → %s\n", n2, PrettyPerc(n2))
fmt.Printf("%f → %s\n", n3, PrettyPerc(n3))
Output:

0.123000 → 0.12%
10.240000 → 10.2%
1294.193000 → 1,294.2%

func PrettySize

func PrettySize(i any, separator ...string) string

PrettySize formats value to "pretty" size (e.g 1478182 -> 1.34 Mb)

Example
s1 := 193
s2 := 184713
s3 := int64(46361936461)

fmt.Printf("%d → %s\n", s1, PrettySize(s1))
fmt.Printf("%d → %s\n", s2, PrettySize(s2))
fmt.Printf("%d → %s\n", s3, PrettySize(s3))

// Set size separator
SizeSeparator = " "

fmt.Printf("%d → %s\n", s3, PrettySize(s3))

// Use custom order separator
fmt.Printf("%d → %s\n", s3, PrettySize(s3, "|"))
Output:

193 → 193B
184713 → 180.4KB
46361936461 → 43.2GB
46361936461 → 43.2 GB
46361936461 → 43.2|GB

func Separator

func Separator(tiny bool, args ...string)

Separator renders separator to console

Example
// You can change color of separator symbols and title using fmtc color tags

SeparatorColorTag = "{r}"       // Set color to red
SeparatorTitleColorTag = "{r*}" // Set color to red with bold weight

// Or you can remove colors
SeparatorColorTag = ""
SeparatorTitleColorTag = ""

// This is tiny separator (just 1 line)
Separator(true)

// This is wide separator with newlines before and after separator
Separator(false)

// You can set title of separator
Separator(true, "MY SEPARATOR")
Output:

func Wrap

func Wrap(text, indent string, maxLineLength int) string

Wrap wraps text using max line length

Example
text := "Aenean tincidunt metus a tortor aramus, ut bibendum magna fringilla."

fmt.Println(
	Wrap(text, "", 36),
)
Output:

Types

type Alignment added in v12.65.0

type Alignment uint8
const (
	LEFT   Alignment = 0
	CENTER Alignment = 1
	RIGHT  Alignment = 2
)

Directories

Path Synopsis
Package panel provides methods for rendering panels with text
Package panel provides methods for rendering panels with text
Package table contains methods and structs for rendering data in tabular format
Package table contains methods and structs for rendering data in tabular format

Jump to

Keyboard shortcuts

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