Skip to main content
Module

x/enzastdlib/vendor/@deno-std-console.ts>unicodeWidth

enzastdlib is a set of TypeScript modules that follow a common design API philosophy aiming at sane defaults and ease-of-use targeting the Deno TypeScript runtime.
Latest
function unicodeWidth
import { unicodeWidth } from "https://deno.land/x/enzastdlib@v0.0.4/vendor/@deno-std-console.ts";

Get the width of a string's constituent characters in columns in TTY-like environments.

Combine with stripColor from fmt/colors.ts to get the expected physical width of a string in the console.

Examples

Example 1

import { unicodeWidth } from "https://deno.land/std@0.224.0/console/unicode_width.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
import { stripColor } from "https://deno.land/std@0.224.0/fmt/colors.ts";

assertEquals(unicodeWidth("hello world"), 11);
assertEquals(unicodeWidth("天地玄黃宇宙洪荒"), 16);
assertEquals(unicodeWidth("fullwidth"), 18);
assertEquals(unicodeWidth(stripColor("\x1b[36mголубой\x1b[39m")), 7);
assertEquals(unicodeWidth(stripColor("\x1b[31m紅色\x1b[39m")), 4);
assertEquals(unicodeWidth(stripColor("\x1B]8;;https://deno.land\x07🦕\x1B]8;;\x07")), 2);

Parameters

str: string