Package Exports
- anser
- anser/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (anser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
anser
A low level parser for ANSI sequences.
π Features
- Converts text containing ANSI color escape codes into equivalent HTML elements.
- Allows converting the input into JSON output.
- HTML escaping
- Converts links into HTML elements
- Friendly APIs to use with virtual dom libraries
βοΈ Installation
# Using npm
npm install --save anser
# Using yarn
yarn add anserπ Example
const Anser = require("anser");
const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";
console.log(Anser.ansiToHtml(txt));
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>
console.log(Anser.ansiToHtml(txt, { use_classes: true }));
// <span class="ansi-palette-196-fg">Hello</span> <span class="ansi-palette-226-bg">World</span>
console.log(Anser.ansiToJson(txt));
// [ { content: '',
// fg: null,
// bg: null,
// fg_truecolor: null,
// bg_truecolor: null,
// clearLine: undefined,
// decoration: null,
// was_processed: false,
// isEmpty: [Function: isEmpty] },
// { content: 'Hello',
// fg: '255, 0, 0',
// bg: null,
// fg_truecolor: null,
// bg_truecolor: null,
// clearLine: false,
// decoration: null,
// was_processed: true,
// isEmpty: [Function: isEmpty] },
// { content: ' ',
// fg: null,
// bg: null,
// fg_truecolor: null,
// bg_truecolor: null,
// clearLine: false,
// decoration: null,
// was_processed: false,
// isEmpty: [Function: isEmpty] },
// { content: 'World',
// fg: null,
// bg: '255, 255, 0',
// fg_truecolor: null,
// bg_truecolor: null,
// clearLine: false,
// decoration: null,
// was_processed: true,
// isEmpty: [Function: isEmpty] },
// { content: '',
// fg: null,
// bg: null,
// fg_truecolor: null,
// bg_truecolor: null,
// clearLine: false,
// decoration: null,
// was_processed: false,
// isEmpty: [Function: isEmpty] } ]When using TypeScript without --esModuleInterop enabled you can do the following:
import Anser = require('anser');
const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";
console.log(Anser.ansiToHtml(txt));
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>Or with --esModuleInterop enabled you can do the following:
import Anser from 'anser';
const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";
console.log(Anser.ansiToHtml(txt));
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>β Get Help
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. π
- For direct and quick help, you can use Codementor. π
π Documentation
Anser.escapeForHtml(txt)
Escape the input HTML.
This does the minimum escaping of text to make it compliant with HTML.
In particular, the '&','<', and '>' characters are escaped. This should
be run prior to ansiToHtml.
Params
- String
txt: The input text (containing the ANSI snippets).
Return
- String The escaped html.
Anser.linkify(txt)
Adds the links in the HTML.
This replaces any links in the text with anchor tags that display the
link. The links should have at least one whitespace character
surrounding it. Also, you should apply this after you have run
ansiToHtml on the text.
Params
- String
txt: The input text.
Return
- String The HTML containing the tags (unescaped).
Anser.ansiToHtml(txt, options)
This replaces ANSI terminal escape codes with SPAN tags that wrap the content.
This function only interprets ANSI SGR (Select Graphic Rendition) codes that can be represented in HTML. For example, cursor movement codes are ignored and hidden from output. The default style uses colors that are very close to the prescribed standard. The standard assumes that the text will have a black background. These colors are set as inline styles on the SPAN tags.
Another option is to set use_classes: true in the options argument.
This will instead set classes on the spans so the colors can be set via
CSS. The class names used are of the format ansi-*-fg/bg and
ansi-bright-*-fg/bg where * is the color name,
i.e black/red/green/yellow/blue/magenta/cyan/white.
Params
- String
txt: The input text. - Object
options: The options passed to the ansiToHTML method.
Return
- String The HTML output.
Anser.ansiToJson(txt, options)
Converts ANSI input into JSON output.
Params
- String
txt: The input text. - Object
options: The options passed to the ansiToHTML method.
Return
- String The HTML output.
Anser.ansiToText(txt)
Converts ANSI input into text output.
Params
- String
txt: The input text.
Return
- String The text output.
Anser()
The Anser class.
Return
- Anser
setupPalette()
Sets up the palette.
escapeForHtml(txt)
Escapes the input text.
Params
- String
txt: The input text.
Return
- String The escpaed HTML output.
π How to contribute
Have an idea? Found a bug? See how to contribute.
π Support my projects
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
Starring and sharing the projects you like π
βI love books! I will remember you after years if you buy me one. π π
βYou can make one-time donations via PayPal. I'll probably buy a
coffeetea. π΅βSet up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
BitcoinβYou can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! β€οΈ
π° Thanks
This project is highly based on ansi_up, by @drudru. Thanks! π°
π« Where is this library used?
If you are using this library in one of your projects, add it in this list. β¨
@_nomtek/react-native-shimmer-animation@adatechnology/react-native-android-getnet-pos@adembacaj/react-native-google-pay@affinidi/affinidi-auth-sdk-kernel@agbishop/react-ansi-18@ajaykewat/myboilerplate@ali5049/react-native-buttons@alloc/react-error-overlay@amirdiafi/react-native-ios-haptics@apardellass/react-native-audio-stream@ascorbic/gatsby@assystant/firebase@atom-ide-community/nuclide-commons-ui@axio/react-dev-utils@aysea/react-native-ui-library@azalpacir/react-native-dhp-printer@baloochat/react-native-svg-uri@blkmarketco/components-library@brantalikp/rn-resize@callstack/react-native-visionos@classflow/react-dev-utils@classyllama/code-style@cldcvr/flow-log@cocalc/ansi-to-react@codedown/ansi-to-react@codesandbox/sandpack-react@codewars/jest-reporter@con-test/react-native-concent-common@corelmax/react-native-my2c2p-sdk@cs6/react-native-test-native-view-library@cubejs-client/playground@curvenote/ansi-to-react@damian.lnc/core@damruravihara/react-native-testing-package@davidmccabe/sandpack-react@deix/paganini-ui@devpodio/console@digibear/socket-bridge@distasy/react-native-fix@ehyland-org/react-error-overlay@enkeledi/react-native-week-month-date-picker@expensify/react-native@felipesimmi/react-native-datalogic-module@geeky-apo/react-native-advanced-clipboard@glyw/react-native-tabbed-section-list@hbglobal/react-native-actions-shortcuts@hemith/react-native-tnk@heora/cubejs-client-playground@hitomeng/ide-debug-mod@hjopel/react-native@idas1/ui-component-lib@jfilipe-sparta/react-native-module_2@ko-developerhong/react-native-multiple-image-picker@kui-shell/plugin-client-common@labiebhn_/react-native-multiplier@lehuyaa/my-assets@leonchabbey/react-native-tvos@navabi/react-native-ssl-pinning@naze/error@ndjoe/react-native-bluetooth-escpos-printer@next/react-dev-overlay@npm_fluentco/adflow-react-native-sdk@office-iss/react-native-win32@oiti/rn-liveness2d@ollion/flow-log@openpolitica/matomo-next@opensumi/ide-debug@pandaninjas3017/react-native@phong95/react-native-multiple-image-picker@plansys/react-dev-utils@popbot/react-native-macos@praella/localisationist@prague-digi/react-error-overlay@prozilla-os/terminal@redux-devtools/inspector-monitor-trace-tab@routebuddies/react-native-mapbox-navigation@ryfow/react-native-windows@saad27/react-native-bottom-tab-tour@sebastien_rts/react-native-tvos@sephriot/react-native-persistable-uri@shuvi/error-overlay@sidghimire/react-native-mapbox-navigation@soyjak/utils@sridharetikala/react-native-rn-lib-custom-components@status-im/react-native-transparent-video@suen/react-native-tvos@taingo97/react-native-bluetooth-escpos-printer@taingo97/react-native-bluetooth-xprinter@taingo97/react-native-expo-key-rsa-kt@taingo97/react-native-generate-key-rsa@taingo97/react-native-key-rsa@taingo97/react-native-print-xprinter@taingo97/react-native-printer-imin@taingo97/react-native-rsa-expo@taingo97/react-native-sunmi-printer@taingo97/react-native-telpo-printer@tasumaniadiabori/react-native-draggable-flatlist@tg1518/react-native-lewin-aliyunplayer@th3rdwave/react-native@theia/console@thinxviewx/core-rn@throttleup/gatsby@trackier/react-native-trackier@unforgiven/react-native@viankakrisna/react-dev-utils@viankakrisna/react-error-overlay@vidit-me/react-native-wheely@wecraftapps/react-native-use-keyboard@wouterds/react-native-tvos@zotasys/native@zrnik/react-nativeact_mvvm_shop_cartada-packadyen-rnaffinidi-auth-sdk-kernelansi-html-themedansi-log-rendereransi-to-jsonansi-to-reactansi-to-react-18ansi-to-react-with-optionsansi-to-vue3ansicolor-utilsarchlibraryawesome-module-kdbiometric-stbirken-react-native-community-image-editorbn-react-nativecallstack-task-react-dev-utilscallstack-task-react-error-overlaycheckbox-componentcycle-dev-utilsdogandev-simple-toastdulcet-error-overlayemg-apifawaterak-online-paymentfawatrak-online-paymentfluent.adflow.reactnativesdkfluent.adflow.reactnativesdk-alphafmslframework_test_library_sixdeeframework_test_library_sixdee_newframework_test_library_sixdee_new_newgamification-integration-newgatsbygaurav-react-native-loopgenz-native-elementsipynb2htmlipynb2html-fixjordy-frijters-test-libjrennsoh88-react-native-scroll-indicatorkhaled-salem-custom-componentskunailambda-dev-utilslinklogmesh-devtoolmulti-termnative-apple-loginnative-date-picker-modulenative-google-loginnative-kakao-loginnative-modal-damage-vehiclenew-awesome-4321nove-repositorynpm_one_12_34_1_npm_one_1_2_3npm_one_2_2npm_qwertynuclidenuclide-commons-uipayutestingpnm-yph-react-native-custom-componentsprocrealms-clientraact-native-arunramya151reac-native-arun-ramya-testreact-ansireact-ansi-eighteenreact-dev-utils-custom-hmrreact-dev-utils-extrareact-dev-utils-sebfl-updatereact-error-guardreact-error-overlay-canaryreact-error-overlay-dangerousreact-ipynb-renderer-katexreact-nativereact-native-additionreact-native-android-native-viewreact-native-android-scoped-storagereact-native-android-video-player-viewreact-native-animate-textreact-native-ansi-viewreact-native-app-bubblereact-native-app-integrity-checksumreact-native-arps-authorize-netreact-native-arun-ramya-testreact-native-arunjeyam1987react-native-arunmeena1987react-native-arunramya151react-native-auth-service-clientreact-native-aventonfacetec-aventonreact-native-awesome-android-123react-native-awesome-android-123-zeotapreact-native-awesome-module-dharmeshreact-native-awesome-module-latestreact-native-awesome-module-tworeact-native-azure-communication-servicesreact-native-badge-controlreact-native-basic-appreact-native-basic-screenreact-native-biometric-authenticatereact-native-bleccs-componentsreact-native-bluetooth-device-detectreact-native-bridge-packagereact-native-bubble-chartreact-native-build-vesion-getterreact-native-check-componentreact-native-chenaarreact-native-components-designreact-native-conekta-card-tokenizerreact-native-contact-listreact-native-cplusreact-native-create-video-thumbnailreact-native-custom-poccomponentreact-native-custom-poccomponent-nextreact-native-datacapture-corereact-native-dff-components-demoreact-native-dhp-printerreact-native-dimensions-layoutreact-native-dishreact-native-dm-playerreact-native-dsphoto-modulereact-native-dummy-viewreact-native-escapereact-native-expendable-viewreact-native-expo-read-sms-ts-kfreact-native-fedlight-dsmreact-native-firebase-messagingreact-native-flyyreact-native-get-countriesreact-native-ghn-ekycreact-native-hlf-wrapperreact-native-innity-2react-native-innity-remasterreact-native-input-libraryreact-native-is7react-native-jsi-device-inforeact-native-kakao-mapsreact-native-klarify-iosreact-native-klarify-uireact-native-klcreact-native-lib-test-rn-1react-native-library-testing-422522react-native-line-login-androidreact-native-login-demo-testreact-native-lowlatencyreact-native-loyalty-platformsreact-native-macosreact-native-manh-testreact-native-manual-ios-sdkreact-native-meon-edocreact-native-meonsdkreact-native-meontoolreact-native-modal-progress-barreact-native-module-for-testingreact-native-multi-bluetooth-printerreact-native-multiplier-altroncosoreact-native-multiplier-componentreact-native-multiplier-demoreact-native-multiplier2react-native-multiplyreact-native-multiply-componentreact-native-multiselectorreact-native-mun-kitreact-native-my-first-try-arun-ramyareact-native-mymapreact-native-native-audio-enginereact-native-native-ios-test1react-native-nativewindreact-native-nghia-shareringreact-native-nice-learningreact-native-nyx-printerreact-native-offline-noticereact-native-okhttp-forkreact-native-omental-frameworkreact-native-onrampreact-native-otp-custom-libraryreact-native-patch-javareact-native-paynow-generatorreact-native-payu-payment-testingreact-native-plugpag-wrapperreact-native-pme-loadingreact-native-priya-demo-buttonreact-native-progress-arrowreact-native-pulsator-nativereact-native-rabbitmq-allreact-native-radio-bic-group-libreact-native-reanimated-sortable-listreact-native-recent-framework-updatereact-native-remote-cloverreact-native-responsive-helperreact-native-responsive-sizereact-native-return-usb-datareact-native-rn-common-components-examplereact-native-rn-icons-libraryreact-native-rn-tolkaplayerreact-native-rom-componentsreact-native-rtn-ips-poslin-testreact-native-sandycomponentreact-native-savczuk-feature-libraryreact-native-sayhello-modulereact-native-screen-idle-timerreact-native-scroll-tab-to-indexreact-native-sfreact-native-shared-gesturereact-native-sharing-intentreact-native-simple-timelinereact-native-sixdee_test_libreact-native-smartcall-videodemoreact-native-sp-test-commonreact-native-sunmi-printer-hkreact-native-swalreact-native-teads-sdk-modulereact-native-tejab41097-sample-libraryreact-native-teknoctrl-componentsreact-native-test-comlibraryreact-native-test-module-hhhreact-native-test-tooltipreact-native-test-viewreact-native-testwebreact-native-ticker-tapereact-native-tilt-blereact-native-tone-frameworkreact-native-tonesreact-native-trackierreact-native-transtracker-libraryreact-native-tvosreact-native-tvos-nzreact-native-ui-components-libraryreact-native-uvc-camera-androidreact-native-version-appreact-native-volume-phisicalreact-native-windowsreact-native-withframework-checkreact-native-wtfreact-native-xprinter-thermal-blereact-native-ytximkitreact-webpack-build-helperreact_native_pandey_marqueereactnativelyreat-native-multiplierkprredux-devtools-trace-monitorrenu-packrn-adyen-dropinrn-check-btnrn-circular-chartrn-counter-demorn-flatlist-forkrn-horizontal-listrn-keyboard-avoiding-viewrn-my-libraryrn-pay-sdkrn-session-multiplier-demorn-tm-notifyrn-use-modal-hookrn_unique_device_idrnttlocksingularityui-tailersmart_one_connectsmile-ansi-to-reactstack-frame-overlaytest-haptik-libtest-library-123test-zeo-collecttimer-react-dev-utilstransformimeuserfetchuyun-react-dev-utilsvision-camera-base64-resizedvision-camera-plugin-face-detectorvision-camera-plugin-scan-faceswebpack-isomorphic-dev-middlewarewebpack-universal-compilerwifi_configuration_packagewildev-react-nativeyarn-react-hook-formzc-react-dev-utilszf-rn-testaa
