width

package module
v0.0.0-...-1452e9b Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 1 Imported by: 2

README

width

The width package helps you manage string widths

When writing code that wants to print things to a terminal while formatting those things using escape sequences, often you want to keep track of the width of the thing you've printed, so you can do things like word breaking, line wrapping, or alignment.

The width package aims to be the basic block for solving that family of problems.

At its core there's just an interface,

type StringWidther interface {
	String() string
	Width() int
}

and two implementations of that thing: a bare String type that just uses go-runewidth, and an explicit StringAndWidth that lets you set both things by hand. This is enough for most things.


At some point this library might go beyond that looking at actual widths for some currently poorly-supported corner cases (i.e. cases where runewidth and your terminal disagree). So far just ingoring that issue has solved most problems (e.g. VTE in Ubuntu 16.04 mangles emoji widths, but nobody cares about it any more).

Documentation

Overview

Package width defines an interface, `StringWidther`, and a couple of trivial implementations of that interface, to allow you to build things where the length of a string and its width on screen are independent of one another. This is useful for embedding escape sequences, for example, as well as for accounting for glyphs of varying on-screen width.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type String

type String string

A String is just a string that uses runewidth to get its width.

func (String) String

func (s String) String() string

func (String) Width

func (s String) Width() int

Width of a String is just its runewidth.StringWidth (ie the sum of the widths of its runes).

type StringAndWidth

type StringAndWidth struct {
	S string
	W int
}

StringAndWidth keeps track of its string and its width independent and explicitly.

func (StringAndWidth) String

func (sw StringAndWidth) String() string

func (StringAndWidth) Width

func (sw StringAndWidth) Width() int

Width of a StringAndWidth is whatever it was set to.

type StringWidther

type StringWidther interface {
	String() string
	Width() int
}

The StringWidther interface is the basic interface this package builds on.

Jump to

Keyboard shortcuts

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