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CAAC,MAAM,GAAG,EAAE,CAAC;QAClB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AA5ED,4DA4EC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { AnsiEscape } from './AnsiEscape';\nimport { TextRewriter, type TextRewriterState } from './TextRewriter';\n\nenum State {\n // Buffer is empty, and we're looking for the ESC character\n Start,\n // We're looking for the '[' character\n AwaitingBracket,\n // We're reading the codes after the '[' character\n ReadingCodes\n}\n\ninterface IRemoveColorsTextRewriterState extends TextRewriterState {\n buffer: string;\n parseState: State;\n}\n\n/**\n * For use with {@link TextRewriterTransform}, this rewriter removes ANSI escape codes\n * including colored text.\n *\n * @remarks\n * The implementation also removes other ANSI escape codes such as cursor positioning.\n * The specific set of affected codes may be adjusted in the future.\n *\n * @public\n */\nexport class RemoveColorsTextRewriter extends TextRewriter {\n public initialize(): TextRewriterState {\n return { buffer: '', parseState: State.Start } as IRemoveColorsTextRewriterState;\n }\n\n public process(unknownState: TextRewriterState, text: string): string {\n const state: IRemoveColorsTextRewriterState = unknownState as IRemoveColorsTextRewriterState;\n\n // We will be matching AnsiEscape._csiRegExp:\n //\n // /\\x1b\\[([\\x30-\\x3f]*[\\x20-\\x2f]*[\\x40-\\x7e])/gu\n //\n const ESC: string = '\\x1b';\n\n let result: string = '';\n let index: number = 0;\n\n while (index < text.length) {\n if (state.parseState === State.Start) {\n // The buffer is empty, which means we haven't found anything yet\n\n const csiIndex: number = text.indexOf(ESC, index);\n if (csiIndex < 0) {\n // We reached the end of \"text\" without finding another CSI prefix\n result += text.substring(index);\n break;\n }\n\n // Append everything up to the CSI prefix\n result += text.substring(index, csiIndex);\n\n // Save the partial match in the buffer\n state.buffer = ESC;\n index = csiIndex + 1;\n state.parseState = State.AwaitingBracket;\n } else {\n // The buffer has characters, which means we started matching a partial sequence\n\n // Read another character into the buffer\n const c: string = text[index];\n ++index;\n state.buffer += c;\n\n if (state.parseState === State.AwaitingBracket) {\n if (c === '[') {\n state.parseState = State.ReadingCodes;\n } else {\n // Failed to match, so append the buffer and start over\n result += state.buffer;\n state.buffer = '';\n state.parseState = State.Start;\n }\n } else {\n // state.state === State.ReadingCodes\n\n // Stop when we reach any character that is not [\\x30-\\x3f] or [\\x20-\\x2f]\n const code: number = c.charCodeAt(0);\n if (code < 0x20 || code > 0x3f) {\n result += AnsiEscape.removeCodes(state.buffer);\n state.buffer = '';\n state.parseState = State.Start;\n }\n }\n }\n }\n\n return result;\n }\n\n public close(unknownState: TextRewriterState): string {\n const state: IRemoveColorsTextRewriterState = unknownState as IRemoveColorsTextRewriterState;\n\n const result: string = state.buffer;\n state.buffer = '';\n return result;\n }\n}\n"]}