{"_id":"@alcalzone/ansi-tokenize","_rev":"289888","name":"@alcalzone/ansi-tokenize","description":"Efficiently modify strings containing ANSI escape codes","dist-tags":{"latest":"0.1.3"},"maintainers":[{"name":"alcalzone","email":""}],"time":{"modified":"2023-10-06T06:27:47.000Z","created":"2023-03-20T16:39:55.198Z","0.1.3":"2023-09-07T13:27:59.703Z","0.1.2":"2023-08-07T13:48:07.202Z","0.1.1":"2023-04-05T21:06:11.247Z","0.1.0":"2023-03-20T16:39:55.198Z"},"users":{},"author":{"name":"AlCalzone","email":"d.griesel@gmx.net"},"versions":{"0.1.3":{"name":"@alcalzone/ansi-tokenize","version":"0.1.3","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":">=14.13.1"},"devDependencies":{"@alcalzone/release-script":"~3.5.9","@alcalzone/release-script-plugin-license":"~3.5.9","@tsconfig/node14":"^14.1.0","@types/node":"^14.18.54","@typescript-eslint/eslint-plugin":"^5.55.0","@typescript-eslint/parser":"^5.55.0","ava":"^4.3.3","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","tsx":"^3.12.8","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":"NODE_OPTIONS='--loader tsx' ava","lint":"eslint .","release":"release-script"},"ava":{"extensions":{"ts":"module"}},"packageManager":"yarn@3.5.0","gitHead":"d9313397d7fd91b0da5e1aed7445830911622929","_id":"@alcalzone/ansi-tokenize@0.1.3","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"shasum":"9f89839561325a8e9a0c32360b8d17e48489993f","size":7207,"noattachment":false,"key":"/@alcalzone/ansi-tokenize/-/@alcalzone/ansi-tokenize-0.1.3.tgz","tarball":"http://name.csiicloud.com:7001/@alcalzone/ansi-tokenize/download/@alcalzone/ansi-tokenize-0.1.3.tgz"},"_npmUser":{"name":"alcalzone","email":"d.griesel@gmx.net"},"directories":{},"maintainers":[{"name":"alcalzone","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ansi-tokenize_0.1.3_1694093279523_0.712518496010744"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-09-07T13:27:59.703Z","publish_time":1694093279703,"_source_registry_name":"default","_cnpm_publish_time":1694093279703},"0.1.2":{"name":"@alcalzone/ansi-tokenize","version":"0.1.2","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":">=14.13.1"},"devDependencies":{"@alcalzone/release-script":"~3.5.9","@alcalzone/release-script-plugin-license":"~3.5.9","@tsconfig/node14":"^14.1.0","@types/node":"^14.18.54","@typescript-eslint/eslint-plugin":"^5.55.0","@typescript-eslint/parser":"^5.55.0","ava":"^4.3.3","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":"92afcc4632d52760d39aad3cdd54ebd75291d4a7","_id":"@alcalzone/ansi-tokenize@0.1.2","_nodeVersion":"16.20.1","_npmVersion":"8.19.4","dist":{"shasum":"4bbc953eebc94005ef133c1df966f4ffb3f9fee8","size":6396,"noattachment":false,"key":"/@alcalzone/ansi-tokenize/-/@alcalzone/ansi-tokenize-0.1.2.tgz","tarball":"http://name.csiicloud.com:7001/@alcalzone/ansi-tokenize/download/@alcalzone/ansi-tokenize-0.1.2.tgz"},"_npmUser":{"name":"alcalzone","email":"d.griesel@gmx.net"},"directories":{},"maintainers":[{"name":"alcalzone","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ansi-tokenize_0.1.2_1691416087027_0.5747837863950656"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-08-07T13:48:07.202Z","publish_time":1691416087202,"_source_registry_name":"default","_cnpm_publish_time":1691416087202},"0.1.1":{"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":{"shasum":"aa0d88ff15bae958977f0dc636ec2a9563921195","size":6354,"noattachment":false,"key":"/@alcalzone/ansi-tokenize/-/@alcalzone/ansi-tokenize-0.1.1.tgz","tarball":"http://name.csiicloud.com:7001/@alcalzone/ansi-tokenize/download/@alcalzone/ansi-tokenize-0.1.1.tgz"},"_npmUser":{"name":"alcalzone","email":"d.griesel@gmx.net"},"directories":{},"maintainers":[{"name":"alcalzone","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ansi-tokenize_0.1.1_1680728771037_0.9736578662432367"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-04-05T21:06:11.247Z","publish_time":1680728771247,"_cnpm_publish_time":1680728771247},"0.1.0":{"name":"@alcalzone/ansi-tokenize","version":"0.1.0","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":"73fb52d357f8bff7d1de16efadf373a20c69b880","_id":"@alcalzone/ansi-tokenize@0.1.0","_nodeVersion":"16.19.1","_npmVersion":"8.19.3","dist":{"shasum":"c62f18fead18c728f063e2d2e0b94a6147fa3918","size":6260,"noattachment":false,"key":"/@alcalzone/ansi-tokenize/-/@alcalzone/ansi-tokenize-0.1.0.tgz","tarball":"http://name.csiicloud.com:7001/@alcalzone/ansi-tokenize/download/@alcalzone/ansi-tokenize-0.1.0.tgz"},"_npmUser":{"name":"alcalzone","email":"d.griesel@gmx.net"},"directories":{},"maintainers":[{"name":"alcalzone","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ansi-tokenize_0.1.0_1679330395050_0.6447349584082223"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-03-20T16:39:55.198Z","publish_time":1679330395198,"_cnpm_publish_time":1679330395198}},"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","_attachments":{},"license":"MIT"}