### Install anser using npm or yarn Source: https://github.com/ionicabizau/anser/blob/master/README.md This snippet shows how to install the anser package using either npm or yarn. It's a prerequisite for using the library in your project. ```sh # Using npm npm install --save anser # Using yarn yarn add anser ``` -------------------------------- ### anser processChunk function documentation Source: https://github.com/ionicabizau/anser/blob/master/README.md Documentation for the `processChunk` function within the anser library. It explains the function's purpose, parameters (text, options, markup), and return value (object or string). ```javascript /** * Processes the current chunk of text. * @param {String} text The input text. * @param {Object} options An object containing the following fields: * - json (Boolean): If `true`, the result will be an object. * - use_classes (Boolean): If `true`, HTML classes will be appended to the HTML output. * @param {Boolean} markup If false, the colors will not be parsed. * @returns {Object|String} The result (object if `json` is wanted back or string otherwise). */ function processChunk(text, options, markup) ``` -------------------------------- ### Import anser in TypeScript (with esModuleInterop) Source: https://github.com/ionicabizau/anser/blob/master/README.md Demonstrates importing the anser library in a TypeScript project when the `esModuleInterop` flag is enabled. This allows for a more standard ES module import syntax. ```typescript import Anser from 'anser'; const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m"; console.log(Anser.ansiToHtml(txt)); // Hello World ``` -------------------------------- ### Add Contributor to package.json Source: https://github.com/ionicabizau/anser/blob/master/CONTRIBUTING.md When contributing to the project, if a package.json or bower.json file exists, add your name to the 'contributors' array. This JSON snippet shows the expected format for adding a contributor. ```json { "contributors": [ "Your Name (http://your.website)" ] } ``` -------------------------------- ### Convert ANSI to HTML with anser (JavaScript) Source: https://github.com/ionicabizau/anser/blob/master/README.md Demonstrates converting ANSI escape codes to HTML using the `ansiToHtml` function. It shows basic conversion and conversion using CSS classes. This is useful for rendering colored terminal output in a web browser. ```javascript const Anser = require("anser"); const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m"; console.log(Anser.ansiToHtml(txt)); // Hello World console.log(Anser.ansiToHtml(txt, { use_classes: true })); // Hello World ``` -------------------------------- ### Import anser in TypeScript (without esModuleInterop) Source: https://github.com/ionicabizau/anser/blob/master/README.md Shows how to import the anser library in a TypeScript project when the `esModuleInterop` flag is not enabled in `tsconfig.json`. This uses a specific import syntax for CommonJS modules. ```typescript import Anser = require('anser'); const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m"; console.log(Anser.ansiToHtml(txt)); // Hello World ``` -------------------------------- ### Convert ANSI to JSON with anser (JavaScript) Source: https://github.com/ionicabizau/anser/blob/master/README.md Illustrates converting ANSI escape codes into a JSON structure using the `ansiToJson` function. This output provides a structured representation of the text, including color, background, and other formatting information. ```javascript const Anser = require("anser"); const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m"; console.log(Anser.ansiToJson(txt)); // [ { content: '', // fg: null, // bg: null, // fg_truecolor: null, // bg_truecolor: null, // clearLine: undefined, // decoration: null, // was_processed: false, // isEmpty: [Function: isEmpty] }, // { content: 'Hello', // fg: '255, 0, 0', // bg: null, // fg_truecolor: null, // bg_truecolor: null, // clearLine: false, // decoration: null, // was_processed: true, // isEmpty: [Function: isEmpty] }, // { content: ' ', // fg: null, // bg: null, // fg_truecolor: null, // bg_truecolor: null, // clearLine: false, // decoration: null, // was_processed: false, // isEmpty: [Function: isEmpty] }, // { content: 'World', // fg: null, // bg: '255, 255, 0', // fg_truecolor: null, // bg_truecolor: null, // clearLine: false, // decoration: null, // was_processed: true, // isEmpty: [Function: isEmpty] }, // { content: '', // fg: null, // bg: null, // fg_truecolor: null, // bg_truecolor: null, // clearLine: false, // decoration: null, // was_processed: false, // isEmpty: [Function: isEmpty] } ] ``` -------------------------------- ### TypeScript Support for Anser Source: https://context7.com/ionicabizau/anser/llms.txt Provides TypeScript definitions for the Anser library, enabling type-safe development in TypeScript projects. This includes type definitions for the main Anser object, its methods, options, and the structure of its JSON output. It demonstrates how to import Anser in different module systems and how to use its types for variables and function parameters. ```typescript // Without --esModuleInterop import Anser = require('anser'); // With --esModuleInterop enabled import Anser from 'anser'; // Type-safe usage const text: string = "\x1B[38;5;196mHello\x1B[39m"; const html: string = Anser.ansiToHtml(text); const json: Anser.AnserJsonEntry[] = Anser.ansiToJson(text); // Options interface const options: Anser.AnserOptions = { use_classes: true, remove_empty: true }; // JSON entry structure json.forEach((entry: Anser.AnserJsonEntry) => { console.log(entry.content); // string console.log(entry.fg); // string | null console.log(entry.bg); // string | null console.log(entry.decorations); // Array<'bold' | 'dim' | 'italic' | 'underline' | 'blink' | 'reverse' | 'hidden' | 'strikethrough'> console.log(entry.was_processed); // boolean console.log(entry.isEmpty()); // boolean }); ``` -------------------------------- ### Convert URLs to Clickable Links with Anser.linkify Source: https://context7.com/ionicabizau/anser/llms.txt Converts URLs within a given text string into clickable HTML anchor elements. This function is typically used after converting ANSI codes to HTML to ensure that links within styled terminal output are correctly rendered. It takes a string as input and returns a string with URLs replaced by `` tags. ```javascript const Anser = require("anser"); // Convert URL to clickable link const text = "Visit http://link.to/me for more info"; console.log(Anser.linkify(text)); // Output: Visit http://link.to/me for more info // Combine with ansiToHtml for full terminal output rendering const terminalOutput = "\x1B[32mSuccess!\x1B[0m See https://example.com/docs"; const html = Anser.ansiToHtml(terminalOutput); const linkedHtml = Anser.linkify(html); console.log(linkedHtml); // Output: Success! See https://example.com/docs // Full safe rendering pipeline const rawOutput = "Check at http://example.com"; const escaped = Anser.escapeForHtml(rawOutput); const htmlified = Anser.ansiToHtml(escaped); const final = Anser.linkify(htmlified); console.log(final); // Output: Check <docs> at http://example.com ``` -------------------------------- ### Strip ANSI to Plain Text with Anser.ansiToText (JavaScript) Source: https://context7.com/ionicabizau/anser/llms.txt Strips all ANSI escape codes from a given string, returning only the plain text content. This method is useful for logging, creating accessible versions of terminal output, or when styling is not supported. It handles various ANSI sequences, including color, decoration, and cursor movement codes. ```javascript const Anser = require("anser"); // Remove color codes from text const colored = "foo \x1B[1;32mbar\x1B[0m baz"; console.log(Anser.ansiToText(colored)); // Remove unsupported sequences (cursor movement, etc.) const withCursor = "foo \x1B[1Abar"; console.log(Anser.ansiToText(withCursor)); // Preserve multiline content const multiline = "foo \x1B[1;32mbar\nbaz\x1B[0m qux"; console.log(Anser.ansiToText(multiline)); // Clean up complex terminal output for logging const terminalOutput = "\x1B[38;5;196m[ERROR]\x1B[0m \x1B[1mCritical failure\x1B[0m at line 42"; console.log(Anser.ansiToText(terminalOutput)); ``` -------------------------------- ### Anser.ansiToText Source: https://context7.com/ionicabizau/anser/llms.txt Strips all ANSI escape codes from a given string, returning only the plain text content. This is useful for logging or contexts where styling is not needed. ```APIDOC ## Anser.ansiToText ### Description Strips all ANSI escape codes from the input text, returning plain text without any formatting. This is useful for logging to files, creating accessible text versions, or processing terminal output in contexts where styling is not supported. ### Method `Anser.ansiToText(text: string): string` ### Parameters #### Path Parameters - **text** (string) - Required - The input string containing ANSI escape codes. ### Request Example ```javascript const Anser = require("anser"); const colored = "foo \x1B[1;32mbar\x1B[0m baz"; console.log(Anser.ansiToText(colored)); ``` ### Response #### Success Response (200) - **string**: The input string with all ANSI escape codes removed. #### Response Example ``` foo bar baz ``` ``` -------------------------------- ### Stateful Terminal Processing with Anser Instance Source: https://context7.com/ionicabizau/anser/llms.txt Enables stateful processing of terminal output, particularly useful for streaming scenarios where ANSI color and style information might span across multiple data chunks. An Anser instance maintains the current state, allowing for accurate rendering of continuous terminal output. It provides methods like `ansiToHtml` and `process` that operate on this maintained state. ```javascript const Anser = require("anser"); // Create instance for stateful processing const anser = new Anser(); // Process chunks while maintaining color state const chunk1 = "\x1B[32mStarting process..."; const chunk2 = "still green\x1B[0m done"; // Instance methods maintain state between calls const html1 = anser.ansiToHtml(chunk1); const html2 = anser.ansiToHtml(chunk2); console.log(html1); // Output: Starting process... console.log(html2); // Output: still green done // Low-level process method with full control const anser2 = new Anser(); const jsonResult = anser2.process("\x1B[1;31mError\x1B[0m", { json: true }, true); console.log(jsonResult[1].content); // "Error" console.log(jsonResult[1].fg); // "187, 0, 0" console.log(jsonResult[1].decorations); // ["bold"] // Process individual chunks for fine-grained control const anser3 = new Anser(); const chunkResult = anser3.processChunk("31mRed Text", {}, true); console.log(chunkResult); // Output: Red Text ``` -------------------------------- ### Anser.escapeForHtml Source: https://context7.com/ionicabizau/anser/llms.txt Escapes HTML special characters (&, <, >, ") in a string to make it safe for rendering in HTML. This should be used before `ansiToHtml` if the input might contain user-generated content. ```APIDOC ## Anser.escapeForHtml ### Description Escapes HTML special characters (&, <, >, ") in the input text to make it safe for HTML rendering. This should be called before `ansiToHtml` when the input might contain user-generated content or HTML-like strings. ### Method `Anser.escapeForHtml(text: string): string` ### Parameters #### Path Parameters - **text** (string) - Required - The input string potentially containing HTML special characters. ### Request Example ```javascript const Anser = require("anser"); console.log(Anser.escapeForHtml("")); ``` ### Response #### Success Response (200) - **string**: The input string with HTML special characters escaped. #### Response Example ``` <script>alert('xss')</script> ``` ``` -------------------------------- ### Convert ANSI to HTML with Anser Source: https://context7.com/ionicabizau/anser/llms.txt Converts ANSI terminal escape codes to HTML span elements with inline styles or CSS classes. Supports foreground/background colors, text decorations, and 256-color/24-bit true color palettes. Options include `use_classes` for CSS-based styling. ```javascript const Anser = require("anser"); // Basic color conversion with inline styles (default) const coloredText = "\x1B[32mSuccess!\x1B[0m Operation completed."; console.log(Anser.ansiToHtml(coloredText)); // Output: Success! Operation completed. // Using CSS classes instead of inline styles const boldRed = "\x1B[1;31mError:\x1B[0m File not found"; console.log(Anser.ansiToHtml(boldRed, { use_classes: true })); // Output: Error: File not found // 256-color palette support const palette256 = "\x1B[38;5;196mHello\x1B[39m \x1B[48;5;226mWorld\x1B[49m"; console.log(Anser.ansiToHtml(palette256)); // Output: Hello World // 24-bit true color support const trueColor = "\x1B[38;2;42;142;242mCustom Blue\x1B[0m"; console.log(Anser.ansiToHtml(trueColor)); // Output: Custom Blue // Multiple text decorations: underline, blink, bold, blue on red const decorated = "\x1B[4m\x1B[5m\x1B[1;34m\x1B[41mStyled Text\x1B[0m"; console.log(Anser.ansiToHtml(decorated)); // Output: Styled Text // Reverse/inverse colors const inversed = "\x1B[7m\x1B[34m\x1B[41mInverted\x1B[0m"; console.log(Anser.ansiToHtml(inversed)); // Output: Inverted ``` -------------------------------- ### Parse ANSI to JSON with Anser.ansiToJson (JavaScript) Source: https://context7.com/ionicabizau/anser/llms.txt Parses ANSI escape codes from a string and returns an array of JSON objects. Each object represents a styled segment of the text, including content, foreground/background colors, and decorations. Options can be provided to remove empty segments or use CSS classes for theming. This is useful for virtual DOM rendering. ```javascript const Anser = require("anser"); // Parse colored text into JSON segments const text = "\x1B[38;5;196mHello\x1B[39m \x1B[48;5;226mWorld\x1B[49m"; const segments = Anser.ansiToJson(text); console.log(JSON.stringify(segments, null, 2)); // Remove empty segments for cleaner output const cleanSegments = Anser.ansiToJson(text, { remove_empty: true }); console.log(cleanSegments.map(s => ({ content: s.content, fg: s.fg, bg: s.bg }))); // Using CSS classes mode for theming const classSegments = Anser.ansiToJson("\x1B[1;32mBold Green\x1B[0m", { use_classes: true }); console.log(classSegments[1]); // Detect carriage returns for terminal line clearing const withCR = "\x1B[32mProgress\x1B[0m\r"; const crSegments = Anser.ansiToJson(withCR); console.log(crSegments[0].clearLine); // true - indicates line should be cleared ``` -------------------------------- ### Anser.ansiToJson Source: https://context7.com/ionicabizau/anser/llms.txt Parses ANSI escape codes from a string and returns an array of JSON objects, each representing a styled segment of the text. This is useful for virtual DOM rendering. ```APIDOC ## Anser.ansiToJson ### Description Parses ANSI escape codes and returns an array of JSON objects representing each styled segment. This method is ideal for virtual DOM libraries (React, Vue, etc.) where you need programmatic access to color and style information rather than pre-rendered HTML strings. ### Method `Anser.ansiToJson(text: string, options?: { remove_empty?: boolean, use_classes?: boolean }) => object[]` ### Parameters #### Query Parameters - **text** (string) - Required - The input string containing ANSI escape codes. - **options** (object) - Optional - Configuration options: - **remove_empty** (boolean) - If true, empty segments are removed from the output. - **use_classes** (boolean) - If true, CSS class names are used for styling instead of RGB values. ### Request Example ```javascript const Anser = require("anser"); const text = "\x1B[38;5;196mHello\x1B[39m \x1B[48;5;226mWorld\x1B[49m"; const segments = Anser.ansiToJson(text); console.log(JSON.stringify(segments, null, 2)); ``` ### Response #### Success Response (200) - **Array of objects**: Each object represents a text segment with properties like `content`, `fg`, `bg`, `decorations`, etc. #### Response Example ```json [ { "content": "Hello", "fg": "255, 0, 0", "bg": null, "fg_truecolor": null, "bg_truecolor": null, "clearLine": false, "decoration": null, "decorations": [], "was_processed": true }, { "content": " ", "fg": null, "bg": null, "clearLine": false, "was_processed": false }, { "content": "World", "fg": null, "bg": "255, 255, 0", "clearLine": false, "was_processed": true } ] ``` ``` -------------------------------- ### Escape HTML Special Characters with Anser.escapeForHtml (JavaScript) Source: https://context7.com/ionicabizau/anser/llms.txt Escapes HTML special characters (like &, <, >, ") in a string to prevent cross-site scripting (XSS) vulnerabilities and ensure safe rendering in HTML. This function should be used before converting ANSI codes to HTML if the input might contain user-generated content. ```javascript const Anser = require("anser"); // Escape HTML special characters console.log(Anser.escapeForHtml("&")); console.log(Anser.escapeForHtml("")); console.log(Anser.escapeForHtml("a > b && c < d")); // Safe rendering pipeline: escape HTML first, then convert ANSI codes const unsafeInput = "\x1B[32mHello & Goodbye\x1B[0m"; const safeHtml = Anser.ansiToHtml(Anser.escapeForHtml(unsafeInput)); console.log(safeHtml); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.