util.string.width

Shared

Calculates the display width of a string or codepoint


Syntax

local width = util.string.width(
    input,
    ambiguous_as_double = false,
    default_width = 0
)

Parameters

TypeNameDescription
string | intinputString to measure, or a single codepoint
boolambiguous_as_doubleWhen true - East Asian "ambiguous width" characters count as double-width instead of single-width
intdefault_widthWidth to use for codepoints with no defined display width

Returns

TypeNameDescription
intwidthTotal display width in terminal columns

Examples

Measure the width of a plain ASCII string
local width = util.string.width("hello")

core.engine.print("info", width) -- 5
Wide characters count as 2 columns
local width = util.string.width("中文")

core.engine.print("info", width) -- 4
Measure the width of a single codepoint
local width = util.string.width(util.string.byte("中"))

core.engine.print("info", width) -- 2
Treat ambiguous-width characters as double-width
local width = util.string.width("±", true)

core.engine.print("info", width) -- 2

On this page