wcwidth

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: BSD-3-Clause Imports: 2 Imported by: 1

Documentation

Overview

Package wcwidth provides functions to determine the number of columns required when printing to a terminal.

http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParagraphLineBreak

func ParagraphLineBreak(maxlen int, text string) (string, int, string)

ParagraphLineBreak splits the text such that the first component will occupy no more than maxlen columns. Leading spaces are trimmed from lines.

Example
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr/wcwidth"
)

const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer feugiat scelerisque est non feugiat. Donec at ligula tristique neque dignissim pellentesque sit amet dictum magna. Phasellus dictum cursus quam, sit amet iaculis magna mollis vitae. Sed libero justo, tempus vitae ullamcorper et, aliquam sit amet augue. Vivamus erat libero, tincidunt tincidunt est et, pellentesque semper lacus. Donec elementum non odio.`

func main() {
	const width = 60 /* columns */

	line, _, rest := wcwidth.ParagraphLineBreak(width, lorem)
	fmt.Println(line)
	for rest != "" {
		line, _, rest = wcwidth.ParagraphLineBreak(width, rest)
		fmt.Println(line)
	}

}
Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Integer feugiat scelerisque est non feugiat. Donec at ligula
tristique neque dignissim pellentesque sit amet dictum
magna. Phasellus dictum cursus quam, sit amet iaculis magna
mollis vitae. Sed libero justo, tempus vitae ullamcorper et,
aliquam sit amet augue. Vivamus erat libero, tincidunt
tincidunt est et, pellentesque semper lacus. Donec elementum
non odio.

func RuneWidth

func RuneWidth(r rune) int

RuneWidth return the number of columns required when printing the rune to a terminal.

If the rune is the null character, this functionos returns zero.

If the rune is a control character, this functions a non-specified value.

func StringWidth

func StringWidth(s string) int

StringWidth returns the number of columns required when printing the string to a terminal. The string should not contain the null character, or any control character.

Example
package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~rj/sgr/wcwidth"
)

func main() {
	const line = "Hello, 世界!"

	fmt.Println("01234567899876543210")

	width := wcwidth.StringWidth(line)
	fmt.Printf("%s%s\n", strings.Repeat(" ", (20-width)/2), line)

}
Output:

01234567899876543210
    Hello, 世界!

Types

This section is empty.

Jump to

Keyboard shortcuts

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