Skip to content

Commit

Permalink
feat: ansi regex & escape
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Aug 25, 2020
1 parent 0e207a1 commit d0fe042
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ansi_regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function ansiRegex({ onlyFirst = false } = {}): RegExp {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|");
return new RegExp(pattern, onlyFirst ? undefined : "g");
}
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ export type AsyncStream = Deno.Writer;
export * from "./tty_async.ts";
export * from "./tty_sync.ts";
export * from "./wcwidth.ts";
export * from "./ansi_regex.ts";
export * from "./strip_ansi.ts";
5 changes: 5 additions & 0 deletions strip_ansi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ansiRegex } from "./mod.ts";

export function stripAnsi(dirty: string): string {
return dirty.replace(ansiRegex(), "");
}
6 changes: 3 additions & 3 deletions wcwidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type Defaults = {
* This implementation assumes that characters are encoded in ISO 10646.
*/
export function wcswidth(
str: any,
str: string,
{ nul = 0, control = 0 }: Defaults = {},
): number {
const opts = { nul, control };
Expand All @@ -45,7 +45,7 @@ export function wcswidth(
return s;
}

function wcwidth(ucs: any, { nul = 0, control = 0 }: Defaults = {}): number {
function wcwidth(ucs: number, { nul = 0, control = 0 }: Defaults = {}): number {
// test for 8-bit control characters
if (ucs === 0) return nul;
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) return control;
Expand Down Expand Up @@ -74,7 +74,7 @@ function wcwidth(ucs: any, { nul = 0, control = 0 }: Defaults = {}): number {
);
}

function bisearch(ucs: any): boolean {
function bisearch(ucs: number): boolean {
var min = 0;
var max = combining.length - 1;
var mid;
Expand Down

0 comments on commit d0fe042

Please sign in to comment.