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

anser 2.0.2 #66

Merged
merged 6 commits into from
Sep 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2012-20 Ionică Bizău <[email protected]> (https://ionicabizau.net)
Copyright (c) 2012-21 Ionică Bizău <[email protected]> (https://ionicabizau.net)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,17 @@ console.log(Anser.ansiToJson(txt));



When using **TypeScript** you can do the following:
When using **TypeScript** without --esModuleInterop enabled you can do the following:
```ts
import * as Anser from 'anser';
import Anser = require('anser');
const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";
console.log(Anser.ansiToHtml(txt));
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>
```

Or with --esModuleInterop enabled you can do the following:
```ts
import Anser from 'anser';
const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";
console.log(Anser.ansiToHtml(txt));
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>
Expand Down Expand Up @@ -418,17 +426,22 @@ This project is highly based on [`ansi_up`](https://github.com/drudru/ansi_up),
If you are using this library in one of your projects, add it in this list. :sparkles:

- `react-native`
- `react-native-windows`
- `gatsby`
- `ansi-to-react`
- `mesh-devtool`
- `react-native-windows`
- `@next/react-dev-overlay`
- `@cubejs-client/react`
- `mesh-devtool`
- `nuclide-commons-ui`
- `@atom-ide-community/nuclide-commons-ui`
- `@redux-devtools/inspector-monitor-trace-tab`
- `@theia/console`
- `transformime`
- `@viankakrisna/react-dev-utils`
- `react-webpack-build-helper`
- `ansi-to-json`
- `redux-devtools-trace-monitor`
- `@kui-shell/plugin-client-common`
- `@axio/react-dev-utils`
- `react-error-overlay-dangerous`
- `timer-react-dev-utils`
Expand All @@ -440,13 +453,11 @@ If you are using this library in one of your projects, add it in this list. :spa
- `lambda-dev-utils`
- `react-error-overlay-canary`
- `@classflow/react-dev-utils`
- `nuclide`
- `react-native-okhttp-fork`
- `@devpodio/console`
- `ipynb2html-fix`
- `ipynb2html`
- `webpack-isomorphic-dev-middleware`
- `@theia/console`
- `react-native-macos`
- `@ehyland-org/react-error-overlay`
- `stack-frame-overlay`
Expand All @@ -462,7 +473,6 @@ If you are using this library in one of your projects, add it in this list. :spa
- `linklog`
- `@naze/error`
- `react-error-guard`
- `singularityui-tailer`
- `@unforgiven/react-native`
- `@digibear/socket-bridge`
- `ada-pack`
Expand All @@ -475,18 +485,38 @@ If you are using this library in one of your projects, add it in this list. :spa
- `@proteria/react-scripts`
- `kunai`
- `react-native-kakao-maps`
- `react-ansi`
- `@geeky-apo/react-native-advanced-clipboard`
- `native-apple-login`
- `native-google-login`
- `@ryfow/react-native-windows`
- `native-kakao-login`
- `@prague-digi/react-error-overlay`
- `@hemith/react-native-tnk`
- `react-native-sf`
- `react-native-contact-list`
- `@corelmax/react-native-my2c2p-sdk`
- `singularityui-tailer`
- `react-ansi`
- `react-native-responsive-size`
- `rn-flatlist-fork`
- `react-native-test-module-hhh`
- `react-native-is7`
- `@alloc/react-error-overlay`
- `@openpolitica/matomo-next`
- `@datalogic/react-native-datalogic-module`
- `@blkmarketco/components-library`
- `birken-react-native-community-image-editor`
- `native-kakao-login`
- `react-native-modal-progress-bar`
- `react-native-savv`
- `nuclide`
- `react-native-dsphoto-module`
- `rn-custom-tabview`
- `react-native-dm-player`
- `@felipesimmi/react-native-datalogic-module`
- `react-native-tvos`
- `react-native-ytximkit`
- `@mindinventory/rn-top-navbar`
- `react-native-flyy`
- `@office-iss/react-native-win32`


Expand Down
95 changes: 50 additions & 45 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
// Type definitions for Anser
// Project: https://github.com/IonicaBizau/anser

type DecorationName = 'bold' | 'dim' | 'italic' | 'underline' | 'blink' | 'reverse' | 'hidden' | 'strikethrough';

export interface AnserJsonEntry {
/** The text. */
content: string;
/** The foreground color. */
fg: string;
/** The background color. */
bg: string;
/** The foreground true color (if 16m color is enabled). */
fg_truecolor: string;
/** The background true color (if 16m color is enabled). */
bg_truecolor: string;
/** `true` if a carriageReturn \r was fount at end of line. */
clearLine: boolean;
/** The decoration last declared before the text. */
decoration: null | DecorationName;
/** All decorations that apply to the text. */
decorations: Array<DecorationName>;
/** `true` if the colors were processed, `false` otherwise. */
was_processed: boolean;
/** A function returning `true` if the content is empty, or `false` otherwise. */
isEmpty(): boolean;
declare namespace Anser {
type DecorationName = 'bold' | 'dim' | 'italic' | 'underline' | 'blink' | 'reverse' | 'hidden' | 'strikethrough';

export interface AnserJsonEntry {
/** The text. */
content: string;
/** The foreground color. */
fg: string;
/** The background color. */
bg: string;
/** The foreground true color (if 16m color is enabled). */
fg_truecolor: string;
/** The background true color (if 16m color is enabled). */
bg_truecolor: string;
/** `true` if a carriageReturn \r was fount at end of line. */
clearLine: boolean;
/** The decoration last declared before the text. */
decoration: null | DecorationName;
/** All decorations that apply to the text. */
decorations: Array<DecorationName>;
/** `true` if the colors were processed, `false` otherwise. */
was_processed: boolean;

/** A function returning `true` if the content is empty, or `false` otherwise. */
isEmpty(): boolean;
}

export interface AnserOptions {
/** If `true`, the result will be an object. */
json?: boolean;
/** If `true`, HTML classes will be appended to the HTML output. */
use_classes?: boolean;
remove_empty?: boolean;
}

type OptionsWithJson = AnserOptions & { json: true };
}

export interface AnserOptions {
/** If `true`, the result will be an object. */
json?: boolean;
/** If `true`, HTML classes will be appended to the HTML output. */
use_classes?: boolean;
remove_empty?: boolean;
}

type OptionsWithJson = AnserOptions & { json: true };

export = class Anser {
declare class Anser {
/**
* Escape the input HTML.
*
Expand Down Expand Up @@ -83,7 +86,7 @@ export = class Anser {
* @param options The options.
* @returns The HTML output.
*/
static ansiToHtml (txt: string, options?: AnserOptions): string;
static ansiToHtml (txt: string, options?: Anser.AnserOptions): string;

/**
* Converts ANSI input into JSON output.
Expand All @@ -92,15 +95,15 @@ export = class Anser {
* @param options The options.
* @returns The HTML output.
*/
static ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];
static ansiToJson (txt: string, options?: Anser.AnserOptions): Anser.AnserJsonEntry[];

/**
* Converts ANSI input into text output.
*
* @param txt The input text.
* @returns The text output.
*/
static ansiToText (txt: string, options?: AnserOptions): string;
static ansiToText (txt: string, options?: Anser.AnserOptions): string;

/**
* Sets up the palette.
Expand Down Expand Up @@ -130,7 +133,7 @@ export = class Anser {
* @param options The options.
* @returns The HTML output.
*/
ansiToHtml (txt: string, options?: AnserOptions): string;
ansiToHtml (txt: string, options?: Anser.AnserOptions): string;

/**
* Converts ANSI input into HTML output.
Expand All @@ -139,15 +142,15 @@ export = class Anser {
* @param options The options.
* @returns The JSON output.
*/
ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];
ansiToJson (txt: string, options?: Anser.AnserOptions): Anser.AnserJsonEntry[];

/**
* Converts ANSI input into HTML output.
*
* @param txt The input text.
* @returns The text output.
*/
ansiToText (txt: string, options?: AnserOptions): string;
ansiToText (txt: string, options?: Anser.AnserOptions): string;

/**
* Processes the input.
Expand All @@ -156,8 +159,8 @@ export = class Anser {
* @param options The options.
* @param markup If false, the colors will not be parsed.
*/
process (txt: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry[];
process (txt: string, options?: AnserOptions, markup?: boolean): string;
process (txt: string, options: Anser.OptionsWithJson, markup?: boolean): Anser.AnserJsonEntry[];
process (txt: string, options?: Anser.AnserOptions, markup?: boolean): string;

/**
* Processes the current chunk into json output.
Expand All @@ -167,7 +170,7 @@ export = class Anser {
* @param markup If false, the colors will not be parsed.
* @return The JSON output.
*/
processChunkJson (text: string, options?: AnserOptions, markup?: boolean): AnserJsonEntry;
processChunkJson (text: string, options?: Anser.AnserOptions, markup?: boolean): Anser.AnserJsonEntry;

/**
* Processes the current chunk of text.
Expand All @@ -177,6 +180,8 @@ export = class Anser {
* @param markup If false, the colors will not be parsed.
* @return The result (object if `json` is wanted back or string otherwise).
*/
processChunk (text: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry;
processChunk (text: string, options?: AnserOptions, markup?: boolean): string;
processChunk (text: string, options: Anser.OptionsWithJson, markup?: boolean): Anser.AnserJsonEntry;
processChunk (text: string, options?: Anser.AnserOptions, markup?: boolean): string;
}

export = Anser;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anser",
"version": "2.0.1",
"version": "2.0.2",
"description": "A low level parser for ANSI sequences.",
"keywords": [
"ansi",
Expand Down Expand Up @@ -46,11 +46,23 @@
}
],
"example": [
"When using **TypeScript** you can do the following:",
"When using **TypeScript** without --esModuleInterop enabled you can do the following:",
{
"code": {
"content": [
"import * as Anser from 'anser';",
"import Anser = require('anser');",
"const txt = \"\\u001b[38;5;196mHello\\u001b[39m \\u001b[48;5;226mWorld\\u001b[49m\";",
"console.log(Anser.ansiToHtml(txt));",
"// <span style=\"color:rgb(255, 0, 0)\">Hello</span> <span style=\"background-color:rgb(255, 255, 0)\">World</span>"
],
"language": "ts"
}
},
"Or with --esModuleInterop enabled you can do the following:",
{
"code": {
"content": [
"import Anser from 'anser';",
"const txt = \"\\u001b[38;5;196mHello\\u001b[39m \\u001b[48;5;226mWorld\\u001b[49m\";",
"console.log(Anser.ansiToHtml(txt));",
"// <span style=\"color:rgb(255, 0, 0)\">Hello</span> <span style=\"background-color:rgb(255, 255, 0)\">World</span>"
Expand Down