elign

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2018 License: MIT Imports: 2 Imported by: 1

README

Elign

Align the string with east asian character set.

Usage

package main

import (
    "fmt"
    "gitlab.com/visig/elign"
)

func main() {
	data := []string{
		"世界上",
		"只有 10 種人",
		"懂二進位和不懂二進位的",
	}
	e := elign.Default(0).AdjustWidth(data...)

	for _, d := range data {
		fmt.Printf("|%v|\n", e.Right(d))
	}

	// Output:
	// |                世界上|
	// |          只有 10 種人|
	// |懂二進位和不懂二進位的|
}

Please check godoc for more information.

Download

go get gitlab.com/visig/elign

License

MIT

Documentation

Overview

Package elign align the string with east asian character set.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Left

func Left(wfn WidthFn, cw int, s string) string

Left padding whitespace to the right of string if cw wider than the string size.

cw is column width.

func Right(wfn WidthFn, cw int, s string) string

Right padding whitespace to the left of string if cw wider than the string size.

cw is column width.

func Width

func Width(s string) (w int)

Width is build-in WidthFn can retrieve the width of a string.

Types

type Elign

type Elign struct {
	// WidthFn offer an algorithm can retrive the width of a string.
	WidthFn WidthFn
	// ColumnWidth offer the filed size of align operations.
	ColumnWidth int
}

Elign is using for align text with east character set.

Example
data := []string{
	"世界上",
	"只有 10 種人",
	"懂二進位和不懂二進位的",
}
e := Default(0).AdjustWidth(data...)

for _, d := range data {
	fmt.Printf("|%v|\n", e.Right(d))
}
Output:

|                世界上|
|          只有 10 種人|
|懂二進位和不懂二進位的|

func Default

func Default(cw int) *Elign

Default return a default Elign instance (using Width as WidthFn). This function only for convenient.

cw is initial column width.

func (*Elign) AdjustWidth

func (e *Elign) AdjustWidth(strs ...string) *Elign

AdjustWidth enlarge the ColumnWidth if it's smaller than input string.

It will return itself for chaining call.

func (*Elign) Left

func (e *Elign) Left(s string) string

Left padding whitespace to the right of string if cw wider than the string size.

func (*Elign) Right

func (e *Elign) Right(s string) string

Right padding whitespace to the left of string if cw wider than the string size.

type WidthFn

type WidthFn func(s string) (width int)

WidthFn is function can calculate the width size of a string.

Jump to

Keyboard shortcuts

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