{"name":"@alcalzone/ansi-tokenize","version":"0.1.1","description":"Efficiently modify strings containing ANSI escape codes","publishConfig":{"access":"public"},"author":{"name":"AlCalzone","email":"d.griesel@gmx.net"},"license":"MIT","type":"module","module":"build/index.js","types":"build/index.d.ts","exports":{".":{"types":"./build/index.d.ts","import":"./build/index.js"},"./package.json":"./package.json"},"engines":{"node":">=16"},"devDependencies":{"@alcalzone/release-script":"~3.5.9","@alcalzone/release-script-plugin-license":"~3.5.9","@tsconfig/node16":"^1.0.3","@types/node":"16","@typescript-eslint/eslint-plugin":"^5.55.0","@typescript-eslint/parser":"^5.55.0","ava":"^5.2.0","eslint":"^8.36.0","eslint-config-prettier":"^8.7.0","eslint-plugin-prettier":"^4.2.1","prettier":"^2.8.4","source-map-support":"^0.5.21","ts-node":"^10.9.1","typescript":"^5.0.2"},"dependencies":{"ansi-styles":"^6.2.1","is-fullwidth-code-point":"^4.0.0"},"scripts":{"prepare":"tsc -p tsconfig.build.json","build":"tsc -p tsconfig.build.json","test":"ava","lint":"eslint .","release":"release-script"},"ava":{"extensions":{"ts":"module"},"nodeArguments":["--loader=ts-node/esm"]},"packageManager":"yarn@3.5.0","gitHead":"838ee9c193c7aee7c658468e8f2c9cf3e260e3d6","_id":"@alcalzone/ansi-tokenize@0.1.1","_nodeVersion":"16.19.1","_npmVersion":"8.19.3","dist":{"integrity":"sha512-0ePcNHsuFOdSZRTkcp6rD/EF6Ls4f2OzBBwXltsk8hHadr6PPKiL/obFrMwoff+twCTdNuvSKywGGPLD2OXV6A==","shasum":"aa0d88ff15bae958977f0dc636ec2a9563921195","tarball":"http://registry-npm.ywwl.com/@alcalzone/ansi-tokenize/download/@alcalzone/ansi-tokenize-0.1.1.tgz","fileCount":23,"unpackedSize":21618,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3NBNPeQnTfrdf1Qkmwjq3sZAbnHMrVuFbo/AxIOzGogIgXFnCNqXujVBWZYXbTTTdC2oP6JRK7INmELBkW3/1lh4="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkLeLDACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqkfw/+OhVM+jozM/UMDvXTlQQfsRVE5dqbit232hsJ4QWfOtQCiSOY\r\nNc4CJGSB6RoVgLvNrkZsDPzOdqo8jAodEuPQvBSbQEymntJ326Ah/tEG1pR4\r\nZiUBmBaBtg/Ii4IMIX62uM7HNWDkf4lQefhpqY2mgpDJp+wnHjZiALuSoCiF\r\nWXc6AIDgEyvJdjFa/lCNUi+iWRRQlVpz5i1bDlMtC5w2A2SWYjPAjz6+0KzW\r\ngIfcXpty+6DZ0IWwjLYqQtbD5eMYSl7OjNEv5s1rUpTM1bm/rlu3EgMXz2ZB\r\n2+CKcqJix4BM5uGQxMZOe3aKk2hPjnAoMGHds0gbFvyXbpmk3SbnsspwnF3L\r\nWag7AezsfGuqQ3Ah7rJSboL3atkz6OWZOEwPlb2noi00wPfX8RhxNqP2i5kt\r\nHTfWFzBAqQU6lWOGzgXfWN9jKq5qUgpx2FCEkOwYOfjvu9q3YSZ7VJz2wz4Z\r\nMq03HEW8DLQQ1Iu0U/aMT/2m/x4FrtyKc6DyOJAtQVbNh0szKYtBY1S7I5Pk\r\neba3nGcc76+m9EjBl+9quq7tH2LFweSvKkQqx97EC2DCK82d+SnK4VPsXhWV\r\nQwEycpA/bnbzO6uJtCyvCKcvCRS/ReM/exH65MzFUAybBnBuuUHIRWIz4sy2\r\nZtU2WRN+RUXkjr1bMDPgBRVQOQCY+wtA5cU=\r\n=54BM\r\n-----END PGP SIGNATURE-----\r\n","size":6354,"noattachment":false,"key":"/@alcalzone/ansi-tokenize/-/@alcalzone/ansi-tokenize-0.1.1.tgz"},"_npmUser":{"name":"alcalzone","email":"d.griesel@gmx.net"},"directories":{},"maintainers":[{"name":"alcalzone","email":"d.griesel@gmx.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ansi-tokenize_0.1.1_1680728771037_0.9736578662432367"},"_hasShrinkwrap":false,"readme":"# @alcalzone/ansi-tokenize\n\n> Efficiently modify strings containing [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)\n\nIf you find yourself modifying styled strings repeatedly, alternatives like [`slice-ansi`](https://github.com/chalk/slice-ansi/) may end up doing a lot of unnecessary work by re-parsing the string each time. This module provides a way to parse the string into an array of tokens (characters or ANSI codes), which can then be modified and re-serialized into a styled string.\n\n## Install\n\n```\n$ npm install @alcalzone/ansi-tokenize\n```\n\n## Usage\n\n### Tokenize a string\n\n```js\nimport { tokenize } from \"@alcalzone/ansi-tokenize\";\n\n// red \"foo\", followed by unstyled \"bar\"\nconst str = \"\\x1B[31mfoo\\x1B[39mbar\";\nconst tokens = tokenize(str);\n\n// tokens will now look like this:\n[\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: \"\\x1B[31m\",\n\t\tendCode: \"\\x1B[39m\",\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"f\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: \"\\x1B[39m\",\n\t\tendCode: \"\\x1B[39m\",\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"b\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"a\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"r\",\n\t\tfullWidth: false,\n\t},\n];\n```\n\nEach token is either a character\n\n```ts\nexport interface Char {\n\ttype: \"char\";\n\tvalue: string;\n\tfullWidth: boolean;\n}\n```\n\nwhere\n\n-   `value` is the string representation of the character\n-   `fullWidth` is `true` if the character is full width (takes up 2 characters in monospace, like CJK characters)\n\nor an ANSI code\n\n```ts\nexport interface AnsiCode {\n\ttype: \"ansi\";\n\tcode: string;\n\tendCode: string;\n}\n```\n\nwhere\n\n-   `code` is the ANSI code that starts the style\n-   and `endCode` is the corresponding ANSI code that ends the style.\n\nAn `AnsiCode` can also be an end code, in which case `code` and `endCode` will be the same.\n\n### Convert an array of tokens into an array of \"styled\" chars\n\nThis representation is a 1:1 mapping of the original string, but not very useful for modifying the string. The `styledCharsFromTokens` function converts a token array to an array of characters, where each character has an all currently active styles associated with it:\n\n```ts\nexport interface StyledChar {\n\ttype: \"char\";\n\tvalue: string;\n\tfullWidth: boolean;\n\tstyles: AnsiCode[];\n}\n```\n\nUsing the above example:\n\n```js\nimport { tokenize, styledCharsFromTokens } from \"@alcalzone/ansi-tokenize\";\n\n// red \"foo\", followed by unstyled \"bar\"\nconst str = \"\\x1B[31mfoo\\x1B[39mbar\";\nconst tokens = tokenize(str);\n\nconst styledChars = styledCharsFromTokens(tokens);\n\n// styledChars will contain the following:\n[\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"f\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"b\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"a\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"r\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n];\n```\n\n### Modify an array of styled characters\n\nFor modification simply edit the items in the array as necessary, as long as the following rules are followed:\n\n1. The `code` and `endCode` properties must match. You can use the `ansi-styles` module to do this.\n2. The `fullWidth` property must be correct. You can use the `is-fullwidth-code-point` module to do this, or if working with multiple strings, turn those into styled char arrays first.\n\nE.g. to make the first `o` blue and bold:\n\n```js\nimport ansiStyles from \"ansi-styles\";\n\n// ... include the above code\n\nstyledChars[1].styles = [\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: ansiStyles.blue.open,\n\t\tendCode: ansiStyles.blue.close,\n\t},\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: ansiStyles.bold.open,\n\t\tendCode: ansiStyles.bold.close,\n\t},\n];\n```\n\n### Serialize a styled character array back to a string\n\nThe `styledCharsToString` function converts a styled character array back to a string:\n\n```js\nimport { styledCharsToString } from \"@alcalzone/ansi-tokenize\";\n\n// ... include the above code\n\nconst strOut = styledCharsToString(styledChars);\n\n// str will now be '\\x1B[31mf\\x1B[34m\\x1B[1mo\\x1B[22m\\x1B[31mo\\x1B[39mbar'\n```\n\nThis automatically figures out the least amount of escape codes necessary to achieve the desired result, as long as the `styles` arrays contain no unnecessary styles, e.g. blue + red foreground.\n\n## Changelog\n\n<!--\n\tPlaceholder for next release:\n\t### __WORK IN PROGRESS__\n-->\n### 0.1.3 (2023-09-07)\n\n-   Fix: Support links\n\n### 0.1.2 (2023-08-07)\n\n-   Fix: Reduce minimum Node.js version to `14.13.1`\n\n### 0.1.1 (2023-04-05)\n\n-   Fix: Active styles are now correctly reset at the end of the string\n\n### 0.1.0 (2023-03-20)\n\nInitial release\n","publish_time":1680728771247,"_cnpm_publish_time":1680728771247,"dist-tags":{"latest":"0.3.1"}}