1/** 2 * Copyright (c) 650 Industries. 3 * Copyright (c) Meta Platforms, Inc. and affiliates. 4 * 5 * This source code is licensed under the MIT license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8import Anser from 'anser'; 9import React from 'react'; 10import { StyleSheet, Text, View } from 'react-native'; 11// Afterglow theme from https://iterm2colorschemes.com/ 12const COLORS = { 13 'ansi-black': 'rgb(27, 27, 27)', 14 'ansi-red': 'rgb(187, 86, 83)', 15 'ansi-green': 'rgb(144, 157, 98)', 16 'ansi-yellow': 'rgb(234, 193, 121)', 17 'ansi-blue': 'rgb(125, 169, 199)', 18 'ansi-magenta': 'rgb(176, 101, 151)', 19 'ansi-cyan': 'rgb(140, 220, 216)', 20 // Instead of white, use the default color provided to the component 21 // 'ansi-white': 'rgb(216, 216, 216)', 22 'ansi-bright-black': 'rgb(98, 98, 98)', 23 'ansi-bright-red': 'rgb(187, 86, 83)', 24 'ansi-bright-green': 'rgb(144, 157, 98)', 25 'ansi-bright-yellow': 'rgb(234, 193, 121)', 26 'ansi-bright-blue': 'rgb(125, 169, 199)', 27 'ansi-bright-magenta': 'rgb(176, 101, 151)', 28 'ansi-bright-cyan': 'rgb(140, 220, 216)', 29 'ansi-bright-white': 'rgb(247, 247, 247)', 30}; 31export function Ansi({ text, style }) { 32 let commonWhitespaceLength = Infinity; 33 const parsedLines = text.split(/\n/).map((line) => Anser.ansiToJson(line, { 34 json: true, 35 remove_empty: true, 36 use_classes: true, 37 })); 38 parsedLines.map((lines) => { 39 // The third item on each line includes the whitespace of the source code. 40 // We are looking for the least amount of common whitespace to trim all lines. 41 // Example: Array [" ", " 96 |", " text", ...] 42 const match = lines[2] && lines[2]?.content?.match(/^ +/); 43 const whitespaceLength = (match && match[0]?.length) || 0; 44 if (whitespaceLength < commonWhitespaceLength) { 45 commonWhitespaceLength = whitespaceLength; 46 } 47 }); 48 const getText = (content, key) => { 49 if (key === 1) { 50 // Remove the vertical bar after line numbers 51 return content.replace(/\| $/, ' '); 52 } 53 else if (key === 2 && commonWhitespaceLength < Infinity) { 54 // Remove common whitespace at the beginning of the line 55 return content.substr(commonWhitespaceLength); 56 } 57 else { 58 return content; 59 } 60 }; 61 return (React.createElement(View, null, parsedLines.map((items, i) => (React.createElement(View, { style: styles.line, key: i }, items.map((bundle, key) => { 62 const textStyle = bundle.fg && COLORS[bundle.fg] 63 ? { 64 backgroundColor: bundle.bg && COLORS[bundle.bg], 65 color: bundle.fg && COLORS[bundle.fg], 66 } 67 : { 68 backgroundColor: bundle.bg && COLORS[bundle.bg], 69 }; 70 return (React.createElement(Text, { style: [style, textStyle], key: key }, getText(bundle.content, key))); 71 })))))); 72} 73const styles = StyleSheet.create({ 74 line: { 75 flexDirection: 'row', 76 }, 77}); 78//# sourceMappingURL=AnsiHighlight.js.map