Skip to content

Commit

Permalink
refactor: use stripColor from deno/std
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Aug 29, 2020
1 parent 580bacd commit 56bcc89
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 37 deletions.
7 changes: 3 additions & 4 deletions command/test/command/help-command_test.ts
@@ -1,5 +1,4 @@
import { assertEquals } from "../../../dev_deps.ts";
import { stripeColors } from "../../../table/utils.ts";
import { assertEquals, stripColor } from "../../../dev_deps.ts";
import { CompletionsCommand } from "../../completions/mod.ts";
import { HelpCommand } from "../../help/mod.ts";
import { Command } from "../../command.ts";
Expand Down Expand Up @@ -90,7 +89,7 @@ Deno.test("command: help command", async () => {
SOME_ENV_VAR_2 <value:string> - Description 2 ...
`,
stripeColors(output),
stripColor(output),
);
});

Expand Down Expand Up @@ -127,6 +126,6 @@ Deno.test("command: help command", async () => {
SOME_ENV_VAR_2 <value:string> - Description 2 ...
`,
stripeColors(output),
stripColor(output),
);
});
5 changes: 2 additions & 3 deletions command/test/command/hidden-command_test.ts
@@ -1,5 +1,4 @@
import { assertEquals } from "../../../dev_deps.ts";
import { stripeColors } from "../../../table/utils.ts";
import { assertEquals, stripColor } from "../../../dev_deps.ts";
import { CompletionsCommand } from "../../completions/mod.ts";
import { HelpCommand } from "../../help/mod.ts";
import { Command } from "../../command.ts";
Expand Down Expand Up @@ -50,6 +49,6 @@ Deno.test("hidden command help", async () => {
completions - Generate shell completions.
`,
stripeColors(output),
stripColor(output),
);
});
5 changes: 2 additions & 3 deletions command/test/option/hidden_test.ts
@@ -1,5 +1,4 @@
import { assertEquals } from "../../../dev_deps.ts";
import { stripeColors } from "../../../table/utils.ts";
import { assertEquals, stripColor } from "../../../dev_deps.ts";
import { Command } from "../../command.ts";

function command(): Command {
Expand Down Expand Up @@ -42,6 +41,6 @@ Deno.test("hidden option help", async () => {
-V, --version - Show the version number for this program.
`,
stripeColors(output),
stripColor(output),
);
});
1 change: 1 addition & 0 deletions dev_deps.ts
Expand Up @@ -9,4 +9,5 @@ export {
export {
bold,
red,
stripColor,
} from "https://deno.land/std@0.66.0/fmt/colors.ts";
5 changes: 2 additions & 3 deletions prompt/_generic-input.ts
@@ -1,11 +1,10 @@
import { KeyEvent } from "../keycode/key-event.ts";
import { stripeColors } from "../table/utils.ts";
import {
GenericPrompt,
GenericPromptOptions,
GenericPromptSettings,
} from "./_generic-prompt.ts";
import { underline } from "./deps.ts";
import { stripColor, underline } from "./deps.ts";

export interface GenericInputKeys {
moveCursorLeft?: string[];
Expand Down Expand Up @@ -51,7 +50,7 @@ export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
protected setPrompt(message: string) {
message += " " + this.settings.pointer + " ";

const length = new TextEncoder().encode(stripeColors(message)).length;
const length = new TextEncoder().encode(stripColor(message)).length;

message += underline(this.input);

Expand Down
1 change: 1 addition & 0 deletions prompt/deps.ts
Expand Up @@ -6,4 +6,5 @@ export {
green,
red,
underline,
stripColor,
} from "https://deno.land/std@0.66.0/fmt/colors.ts";
5 changes: 2 additions & 3 deletions prompt/list.ts
@@ -1,5 +1,4 @@
import { stripeColors } from "../table/utils.ts";
import { blue, underline } from "./deps.ts";
import { blue, stripColor, underline } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericInput,
Expand Down Expand Up @@ -48,7 +47,7 @@ export class List extends GenericInput<string[], ListSettings> {
protected setPrompt(message: string) {
message += " " + this.settings.pointer + " ";

const length = new TextEncoder().encode(stripeColors(message)).length;
const length = new TextEncoder().encode(stripColor(message)).length;

const oldInput: string = this.input;
const oldInputParts: string[] = oldInput.trimLeft().split(this.regexp());
Expand Down
5 changes: 2 additions & 3 deletions prompt/secret.ts
@@ -1,5 +1,4 @@
import { stripeColors } from "../table/utils.ts";
import { blue, green, underline } from "./deps.ts";
import { blue, green, stripColor, underline } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericInput,
Expand Down Expand Up @@ -49,7 +48,7 @@ export class Secret extends GenericInput<string, SecretSettings> {

message += " " + this.settings.pointer + " ";

const length = new TextEncoder().encode(stripeColors(message)).length;
const length = new TextEncoder().encode(stripColor(message)).length;

const secret = this.settings.hidden ? "" : "*".repeat(this.input.length);

Expand Down
3 changes: 3 additions & 0 deletions table/deps.ts
@@ -0,0 +1,3 @@
export {
stripColor,
} from "https://deno.land/std@0.66.0/fmt/colors.ts";
9 changes: 5 additions & 4 deletions table/layout.ts
@@ -1,7 +1,8 @@
import { Cell, ICell } from "./cell.ts";
import { stripColor } from "./deps.ts";
import { IRow, Row } from "./row.ts";
import type { IBorderOptions, ITableSettings, Table } from "./table.ts";
import { consumeWords, longest, stripeColors } from "./utils.ts";
import { consumeWords, longest } from "./utils.ts";

interface IRenderSettings {
padding: number[];
Expand Down Expand Up @@ -285,19 +286,19 @@ export class TableLayout {
): { current: string; next: Cell } {
const length: number = Math.min(
maxLength,
stripeColors(cell.toString()).length,
stripColor(cell.toString()).length,
);
let words: string = consumeWords(length, cell.toString());

// break word if word is longer than max length
const breakWord = stripeColors(words).length > length;
const breakWord = stripColor(words).length > length;
if (breakWord) {
words = words.slice(0, length);
}

// get next content and remove leading space if breakWord is not true
const next = cell.toString().slice(words.length + (breakWord ? 0 : 1));
const fillLength = maxLength - stripeColors(words).length;
const fillLength = maxLength - stripColor(words).length;
const current = words + " ".repeat(fillLength);

return {
Expand Down
18 changes: 4 additions & 14 deletions table/utils.ts
Expand Up @@ -5,6 +5,7 @@
* @param content The text content.
*/
import { Cell, ICell } from "./cell.ts";
import { stripColor } from "./deps.ts";

export function consumeWords(length: number, content: string): string {
let consumed = "";
Expand All @@ -20,8 +21,8 @@ export function consumeWords(length: number, content: string): string {

// consume minimum one word
if (consumed) {
const nextLength = stripeColors(word).length;
const consumedLength = stripeColors(consumed).length;
const nextLength = stripColor(word).length;
const consumedLength = stripColor(consumed).length;
if (consumedLength + nextLength >= length) {
break;
}
Expand All @@ -37,17 +38,6 @@ export function consumeWords(length: number, content: string): string {
return consumed;
}

const COLOR_REGEX = /(\x1b|\e|\033)\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]/g;

/**
* Remove color codes from string.
*
* @param str
*/
export function stripeColors(str: string): string {
return str.replace(COLOR_REGEX, "");
}

/**
* Get longest cell from given row index.
*
Expand All @@ -69,7 +59,7 @@ export function longest(
const str = typeof maxWidth === "undefined"
? r
: consumeWords(maxWidth, r);
return stripeColors(str).length || 0;
return stripColor(str).length || 0;
})
).flat(),
);
Expand Down

0 comments on commit 56bcc89

Please sign in to comment.