Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecation(console): copy std/console APIs into std/cli and deprecate std/console #4547

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions _tools/check_deprecation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ for await (
/crypto(\/|\\)_wasm$/,
/encoding(\/|\\)_yaml$/,
/encoding(\/|\\)_toml$/,
/console$/,
/_tools$/,
/_util$/,
/docs$/,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
export * from "./parse_args.ts";
export * from "./prompt_secret.ts";
export * from "./spinner.ts";
export * from "./unicode_width.ts";
79 changes: 79 additions & 0 deletions cli/unicode_width.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Ported from unicode_width rust crate, Copyright (c) 2015 The Rust Project Developers. MIT license.

import data from "./_data.json" with { type: "json" };
import { runLengthDecode } from "./_run_length.ts";

let tables: Uint8Array[] | null = null;
function lookupWidth(cp: number) {
if (!tables) tables = data.tables.map(runLengthDecode);

const t1Offset = (tables[0] as Uint8Array)[(cp >> 13) & 0xff] as number;
const t2Offset =
(tables[1] as Uint8Array)[128 * t1Offset + ((cp >> 6) & 0x7f)] as number;
const packedWidths =
(tables[2] as Uint8Array)[16 * t2Offset + ((cp >> 2) & 0xf)] as number;

const width = (packedWidths >> (2 * (cp & 0b11))) & 0b11;

return width === 3 ? 1 : width;
}

const cache = new Map<string, number | null>();
function charWidth(ch: string) {
if (cache.has(ch)) return cache.get(ch)!;

const cp = ch.codePointAt(0)!;
let v: number | null = null;

if (cp < 0x7f) {
v = cp >= 0x20 ? 1 : cp === 0 ? 0 : null;
} else if (cp >= 0xa0) {
v = lookupWidth(cp);
} else {
v = null;
}

cache.set(ch, v);
return v;
}

/**
* Calculate the physical width of a string in a TTY-like environment. This is
* useful for cases such as calculating where a line-wrap will occur and
* underlining strings.
*
* The physical width is given by the number of columns required to display
* the string. The number of columns a given unicode character occupies can
* vary depending on the character itself.
*
* @param str The string to measure.
* @returns The unicode width of the string.
*
* @example Calculating the unicode width of a string
* ```ts
* import { unicodeWidth } from "https://deno.land/std@$STD_VERSION/cli/unicode_width.ts";
*
* unicodeWidth("hello world"); // 11
* unicodeWidth("天地玄黃宇宙洪荒"); // 16
* unicodeWidth("fullwidth"); // 18
* ```
*
* @example Calculating the unicode width of a color-encoded string
* ```ts
* import { unicodeWidth } from "https://deno.land/std@$STD_VERSION/cli/unicode_width.ts";
* import { stripAnsiCode } from "https://deno.land/std@$STD_VERSION/fmt/colors.ts";
*
* unicodeWidth(stripAnsiCode("\x1b[36mголубой\x1b[39m")); // 7
* unicodeWidth(stripAnsiCode("\x1b[31m紅色\x1b[39m")); // 4
* unicodeWidth(stripAnsiCode("\x1B]8;;https://deno.land\x07🦕\x1B]8;;\x07")); // 2
* ```
*
* Use
* {@linkcode https://jsr.io/@std/fmt/doc/colors/~/stripAnsiCode | stripAnsiCode}
* to remove ANSI escape codes from a string before passing it to
* {@linkcode unicodeWidth}.
*/
export function unicodeWidth(str: string): number {
return [...str].map((ch) => charWidth(ch) ?? 0).reduce((a, b) => a + b, 0);
}
File renamed without changes.
4 changes: 4 additions & 0 deletions console/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* unicodeWidth("天地玄黃宇宙洪荒"); // 16
* ```
*
* @deprecated Use {@link https://jsr.io/@std/cli | std/cli} instead. This
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure about this?

* module will be removed once the Standard Library migrates to
* {@link https://jsr.io/ | JSR}.
*
* @module
*/

Expand Down
42 changes: 5 additions & 37 deletions console/unicode_width.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Ported from unicode_width rust crate, Copyright (c) 2015 The Rust Project Developers. MIT license.

import data from "./_data.json" with { type: "json" };
import { runLengthDecode } from "./_run_length.ts";

let tables: Uint8Array[] | null = null;
function lookupWidth(cp: number) {
if (!tables) tables = data.tables.map(runLengthDecode);

const t1Offset = (tables[0] as Uint8Array)[(cp >> 13) & 0xff] as number;
const t2Offset =
(tables[1] as Uint8Array)[128 * t1Offset + ((cp >> 6) & 0x7f)] as number;
const packedWidths =
(tables[2] as Uint8Array)[16 * t2Offset + ((cp >> 2) & 0xf)] as number;

const width = (packedWidths >> (2 * (cp & 0b11))) & 0b11;

return width === 3 ? 1 : width;
}

const cache = new Map<string, number | null>();
function charWidth(ch: string) {
if (cache.has(ch)) return cache.get(ch)!;

const cp = ch.codePointAt(0)!;
let v: number | null = null;

if (cp < 0x7f) {
v = cp >= 0x20 ? 1 : cp === 0 ? 0 : null;
} else if (cp >= 0xa0) {
v = lookupWidth(cp);
} else {
v = null;
}

cache.set(ch, v);
return v;
}
import { unicodeWidth as _unicodeWidth } from "../cli/unicode_width.ts";

/**
* Calculate the physical width of a string in a TTY-like environment. This is
Expand Down Expand Up @@ -73,7 +38,10 @@ function charWidth(ch: string) {
* {@linkcode https://jsr.io/@std/fmt/doc/colors/~/stripAnsiCode | stripAnsiCode}
* to remove ANSI escape codes from a string before passing it to
* {@linkcode unicodeWidth}.
*
* @deprecated Use {@linkcode unicodeWidth} from `std/cli` instead. This will be
* removed once the Standard Library migrates to {@link https://jsr.io/ | JSR}.
*/
export function unicodeWidth(str: string): number {
return [...str].map((ch) => charWidth(ch) ?? 0).reduce((a, b) => a + b, 0);
return _unicodeWidth(str);
}