### Parse ANSI Escape Sequences Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Parses a string, triggering terminal callbacks for recognized escape sequences. Sequences can span multiple parse calls. This example demonstrates parsing CSI, OSC, and DCS sequences. ```javascript var AnsiParser = require('node-ansiparser'); var output = []; var terminal = { inst_p: function(s) { output.push(['print', s]); }, inst_o: function(s) { output.push(['osc', s]); }, inst_x: function(flag) { output.push(['exe', flag]); }, inst_c: function(collected, params, flag) { output.push(['csi', collected, params, flag]); }, inst_e: function(collected, flag) { output.push(['esc', collected, flag]); }, inst_H: function(collected, params, flag) { output.push(['dcs hook', collected, params, flag]); }, inst_P: function(dcs) { output.push(['dcs put', dcs]); }, inst_U: function() { output.push(['dcs unhook']); } }; var parser = new AnsiParser(terminal); // Parse CSI sequence with print and execute parser.parse('\x1b[<31;5mHello World! öäü€\nabc'); // Output: [ // ['csi', '<', [31, 5], 'm'], // ['print', 'Hello World! öäü€'], // ['exe', '\n'], // ['print', 'abc'] // ] output = []; // Parse OSC sequence (set terminal title) parser.parse('\x1b]0;abc123€öäü\x07'); // Output: [['osc', '0;abc123€öäü']] output = []; // Parse DCS sequence parser.parse('\x1bP1;2;3+$abc;de\x9c'); // Output: [ // ['dcs hook', '+$', [1, 2, 3], 'a'], // ['dcs put', 'bc;de'], // ['dcs unhook'] // ] output = []; // Multi-part DCS sequence spanning multiple parse calls parser.parse('\x1bP1;2;3+$abc;de'); // Output: [['dcs hook', '+$', [1, 2, 3], 'a'], ['dcs put', 'bc;de']] parser.parse('more-data\x9c'); // Output adds: [['dcs put', 'more-data'], ['dcs unhook']] ``` -------------------------------- ### Initialize and use AnsiParser Source: https://github.com/netzkolchose/node-ansiparser/blob/master/README.md Demonstrates how to instantiate the parser with a terminal object and process ANSI escape sequences. ```javascript var AnsiParser = require('node-ansiparser'); var terminal = { inst_p: function(s) {console.log('print', s);}, inst_o: function(s) {console.log('osc', s);}, inst_x: function(flag) {console.log('execute', flag.charCodeAt(0));}, inst_c: function(collected, params, flag) {console.log('csi', collected, params, flag);}, inst_e: function(collected, flag) {console.log('esc', collected, flag);}, inst_H: function(collected, params, flag) {console.log('dcs-Hook', collected, params, flag);}, inst_P: function(dcs) {console.log('dcs-Put', dcs);}, inst_U: function() {console.log('dcs-Unhook');} }; var parser = new AnsiParser(terminal); parser.parse('\x1b[31mHello World!\n'); parser.parse('\x1bP0!u%5\x1b\''); ``` -------------------------------- ### Initialize AnsiParser with Terminal Callbacks Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Creates a new parser instance. The terminal object should implement callback methods for different sequence types; missing methods are injected as dummies. ```javascript var AnsiParser = require('node-ansiparser'); // Create a terminal object with callback methods var terminal = { inst_p: function(s) { // Handle printable string console.log('print:', s); }, inst_o: function(s) { // Handle OSC (Operating System Command) console.log('osc:', s); }, inst_x: function(flag) { // Handle execute character (control codes) console.log('execute:', flag.charCodeAt(0)); }, inst_c: function(collected, params, flag) { // Handle CSI (Control Sequence Introducer) console.log('csi:', collected, params, flag); }, inst_e: function(collected, flag) { // Handle ESC sequence console.log('esc:', collected, flag); }, inst_H: function(collected, params, flag) { // Handle DCS hook (start of DCS sequence) console.log('dcs-Hook:', collected, params, flag); }, inst_P: function(dcs) { // Handle DCS put (data within DCS sequence) console.log('dcs-Put:', dcs); }, inst_U: function() { // Handle DCS unhook (end of DCS sequence) console.log('dcs-Unhook'); } }; // Initialize parser with terminal object var parser = new AnsiParser(terminal); ``` -------------------------------- ### AnsiParser Constructor Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Initializes a new parser instance with a terminal object that receives callbacks for parsed escape sequences. ```APIDOC ## Constructor: new AnsiParser(terminal) ### Description Creates a new parser instance. The terminal object should implement callback methods for different sequence types (inst_p, inst_o, inst_x, inst_c, inst_e, inst_H, inst_P, inst_U). If methods are missing, the parser injects dummy methods. ### Parameters - **terminal** (Object) - Required - An object containing callback methods for terminal events. ### Request Example var terminal = { inst_p: function(s) { console.log('print:', s); }, inst_c: function(collected, params, flag) { console.log('csi:', collected, params, flag); } }; var parser = new AnsiParser(terminal); ``` -------------------------------- ### Implement Error Callback for AnsiParser Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Implement the `inst_E` callback to handle parsing errors. Return `{abort: true}` to stop parsing or nothing to continue with error recovery. ```javascript var AnsiParser = require('node-ansiparser'); var terminal = { inst_p: function(s) { console.log('print:', s); }, inst_c: function(collected, params, flag) { console.log('csi:', collected, params, flag); }, // Error callback - called when unicode error in escape sequence inst_E: function(e) { console.log('Parse error at position:', e.pos); console.log('Invalid character:', e.character); console.log('Parser state:', e.state); console.log('Collected:', e.collect); console.log('Params:', e.params); // Return {abort: true} to stop parsing // Return nothing or false to continue with error recovery return { abort: false }; } }; var parser = new AnsiParser(terminal); // Parse sequence with unicode error in CSI parameters parser.parse('\x1b[<31;5€normal print'); // Error callback receives: // { // pos: 7, // character: '€', // state: 4, // print: -1, // dcs: -1, // osc: '', // collect: '<', // params: [31, 5] // } // Then continues to output: print: normal print // Example: abort on error var abortTerminal = { inst_p: function(s) { console.log('print:', s); }, inst_E: function(e) { console.log('Aborting parse due to error'); return { abort: true }; } }; var abortParser = new AnsiParser(abortTerminal); abortParser.parse('\x1b[<31;5€this will not print'); // Only outputs: Aborting parse due to error // 'this will not print' is never processed ``` -------------------------------- ### reset() Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Resets the parser state to initial values, clearing any incomplete escape sequences. ```APIDOC ## Method: reset() ### Description Resets the parser state to initial values, clearing any incomplete escape sequences and returning to the GROUND state. ### Request Example parser.reset(); ``` -------------------------------- ### Node-Ansiparser API Source: https://github.com/netzkolchose/node-ansiparser/blob/master/README.md This section details the core methods and terminal object interface for the node-ansiparser library. ```APIDOC ## Node-Ansiparser API ### Description The node-ansiparser library provides a parser for ANSI escape code sequences. It requires a terminal object with specific methods to handle parsed sequences. The parser implements the logic described at http://vt100.net/emu/dec_ansi_parser. ### Terminal Object Methods The terminal object passed to the AnsiParser constructor should implement the following methods: * `inst_p(s)`: Prints the string `s`. * `inst_o(s)`: Handles OSC (Operating System Command) strings `s`. * `inst_x(flag)`: Triggers a one-character method based on `flag`. * `inst_c(collected, params, flag)`: Triggers a CSI (Control Sequence Introducer) method with collected data, parameters, and flag. * `inst_e(collected, flag)`: Triggers an ESC (Escape) method with collected data and flag. * `inst_H(collected, params, flag)`: Handles DCS (Device Control String) commands (hook) with collected data, parameters, and flag. * `inst_P(data)`: Handles DCS data `data`. * `inst_U()`: Handles DCS unhook. * `inst_E(e)` (Optional): Tracks parsing errors, with `e` containing internal parser states. Returning a modified object can inject new values or abort parsing with `{abort:true}`. **Note:** If these methods are not provided, the parser will inject dummy methods. ### Parser Methods * `parse(s)`: Parses the given string `s` and calls the corresponding terminal methods. * `reset()`: Resets the parser to its initial state. ### Usage Example ```javascript var AnsiParser = require('node-ansiparser'); var terminal = { inst_p: function(s) {console.log('print', s);}, inst_o: function(s) {console.log('osc', s);}, inst_x: function(flag) {console.log('execute', flag.charCodeAt(0));}, inst_c: function(collected, params, flag) {console.log('csi', collected, params, flag);}, inst_e: function(collected, flag) {console.log('esc', collected, flag);}, inst_H: function(collected, params, flag) {console.log('dcs-Hook', collected, params, flag);}, inst_P: function(dcs) {console.log('dcs-Put', dcs);}, inst_U: function() {console.log('dcs-Unhook');} }; var parser = new AnsiParser(terminal); parser.parse('\x1b[31mHello World!\n'); parser.parse('\x1bP0!u%5\x1b\' '); ``` ### Parser Throughput The parser achieves a throughput of 50 - 100 MB/s on a desktop computer. ``` -------------------------------- ### Implement Terminal Callback Interface for AnsiParser Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Implement various callback methods on the terminal object to handle different types of ANSI escape sequences, such as printing, OSC commands, control characters, CSI sequences, and simple ESC sequences. ```javascript var AnsiParser = require('node-ansiparser'); var terminal = { // inst_p(s) - Print callback // Receives printable string content inst_p: function(s) { process.stdout.write(s); }, // inst_o(s) - OSC (Operating System Command) callback // Receives OSC string content (e.g., "0;window title") inst_o: function(s) { var parts = s.split(';'); var code = parts[0]; var value = parts.slice(1).join(';'); if (code === '0') { console.log('Set window title:', value); } }, // inst_x(flag) - Execute callback // Receives single control character (newline, carriage return, etc.) inst_x: function(flag) { var code = flag.charCodeAt(0); if (code === 10) console.log('Newline'); if (code === 13) console.log('Carriage return'); if (code === 9) console.log('Tab'); }, // inst_c(collected, params, flag) - CSI callback // collected: intermediate characters (e.g., '<', '>', '?') // params: array of numeric parameters // flag: final character determining the command inst_c: function(collected, params, flag) { if (flag === 'm') { // SGR - Select Graphic Rendition (colors, styles) console.log('Set graphics:', params); } else if (flag === 'H') { // Cursor position console.log('Move cursor to row:', params[0], 'col:', params[1]); } }, // inst_e(collected, flag) - ESC callback // Receives simple escape sequences inst_e: function(collected, flag) { if (flag === 'c') console.log('Reset terminal'); if (flag === '7') console.log('Save cursor'); if (flag === '8') console.log('Restore cursor'); }, // inst_H(collected, params, flag) - DCS Hook callback // Called at start of DCS sequence inst_H: function(collected, params, flag) { console.log('DCS start - collected:', collected, 'params:', params, 'flag:', flag); }, // inst_P(dcs) - DCS Put callback // Receives data within DCS sequence (may be called multiple times) inst_P: function(dcs) { console.log('DCS data:', dcs); }, // inst_U() - DCS Unhook callback // Called at end of DCS sequence inst_U: function() { console.log('DCS end'); } }; var parser = new AnsiParser(terminal); // Example: Parse colored text with cursor movement parser.parse('\x1b[2;10H\x1b[31;1mRed Bold Text\x1b[0m\n'); // Output: // Move cursor to row: 2 col: 10 // Set graphics: [31, 1] // print: Red Bold Text // Set graphics: [0] // Newline ``` -------------------------------- ### parse(s) Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Parses a string containing ANSI escape sequences and triggers the corresponding terminal callbacks. ```APIDOC ## Method: parse(s) ### Description Parses the given string and triggers appropriate terminal callbacks. The parser maintains state between invocations, allowing sequences to span multiple parse calls. ### Parameters - **s** (String) - Required - The string containing ANSI escape sequences to parse. ### Request Example parser.parse('\x1b[<31;5mHello World!'); ``` -------------------------------- ### Reset AnsiParser State Source: https://context7.com/netzkolchose/node-ansiparser/llms.txt Resets the parser's internal state to its initial values, discarding any incomplete escape sequences. This is useful for ensuring a clean parsing state before processing new input. ```javascript var AnsiParser = require('node-ansiparser'); var terminal = { inst_p: function(s) { console.log('print:', s); }, inst_c: function(collected, params, flag) { console.log('csi:', collected, params, flag); } }; var parser = new AnsiParser(terminal); // Parse partial escape sequence parser.parse('\x1b[31;5'); // Sequence is incomplete, waiting for more input // Reset parser to discard incomplete sequence parser.reset(); // Parser is back to initial state // Parser internal state after reset: // parser.current_state === 0 (GROUND) // parser.osc === '' // parser.params === [0] // parser.collected === '' // Now parse fresh input parser.parse('Hello'); // Output: print: Hello ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.