Code Monkey home page Code Monkey logo

ansi-to-react's Introduction

ansi-to-react

This package convert ANSI escape codes to formatted text output for React.

Installation

$ yarn add ansi-to-react
$ npm install --save ansi-to-react

Usage

Basic

The example below shows how we can use this package to render a string with ANSI escape codes.

import Ansi from "ansi-to-react";

export function () => {
  return <Ansi>
    {'\u001b[34mhello world'}
  </Ansi>;
};

Will render:

<code>
  <span style="color:rgb(0, 0, 187)">hello world</span>
</code>

Classes

Style with classes instead of style attribute.

<Ansi useClasses>{"\u001b[34mhello world"}</Ansi>

Will render

<code>
  <span class="ansi-blue-fg">hello world</span>
</code>

Class Names

Font color Background Color
ansi-black-fg ansi-black-bg
ansi-red-fg ansi-red-bg
ansi-green-fg ansi-green-bg
ansi-yellow-fg ansi-yellow-bg
ansi-blue-fg ansi-blue-bg
ansi-magenta-fg ansi-magenta-bg
ansi-cyan-fg ansi-cyan-bg
ansi-white-fg ansi-white-bg
ansi-bright-black-fg
ansi-bright-red-fg
ansi-bright-green-fg
ansi-bright-yellow-fg
ansi-bright-blue-fg
ansi-bright-magenta-fg
ansi-bright-cyan-fg
ansi-bright-white-fg

Development

To develop on this project, fork and clone this repository on your local machine. Before making modifications, install the project's dependencies.

$ npm install

To run the test suite for this project, run:

$ npm test

Documentation

We're working on adding more documentation for this component. Stay tuned by watching this repository!

Support

If you experience an issue while using this package or have a feature request, please file an issue on the issue board,

License

BSD-3-Clause

ansi-to-react's People

Contributors

andrewosh avatar captainsafia avatar carreau avatar crystallinecat avatar dependabot[bot] avatar enriquevidal avatar gnestor avatar greenkeeper[bot] avatar ionicabizau avatar jdfreder avatar kenwheeler avatar kycutler avatar lgeiger avatar mlucool avatar rafipiccolo avatar rgbkrk avatar robertkowalski avatar rowanc1 avatar tregoning avatar williamstein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ansi-to-react's Issues

Add support to split data in chunks to reduce memory pressure

Describe the problem
When a extremely large output of text is being rendered, React cannot seem to handle the memory pressure and crashes the app. Chunking the outputs to allow react to handle them in pieces seems to relieve the memory pressure. However, this creates a problem with ansi-to-react using <code> which changes how the chunks are now rendered.

Describe the solution you'd like
If the data length passes a high threshold of character count, we split the output into chunks and then render the chunks as span using the ansi-to-react change and wrap it outside as code.

In order to do this, we will need to modify two nteract repos:

  1. outputs: add support to split the props.data in multiple chunks, map the array, and make sure to call ANSI in each iteration to render that piece of data.
  2. ansi-to-react: add support to treat each piece of chunk as a <span> element and let media plain wrap it in a <code> element.

Stream outputs don't show backspace characters sometimes

Migrating this from an external bug tracker.

I am not sure if it is our bug or not.
Try running "!conda install nltk -y", you can see that some of the output looks like this: "Solving environment: / ��- ��\ ��| ��/ ��- ��\ ��| ��/ ��- ��\ ��| ��/", where block character stands backspace, I assume, and it is supposed to erase previous state of the spinner.

On the other hand, later when you see progress bars growing such as this:
Downloading and Extracting Packages conda-4.7.11 | 3.0 MB | ##################################### | 100%
I think in that case backspace is being handled correctly, because you can see numbers appearing first, then they are getting erased, and then "#" appears.

So this is why I am hesitant to call that we do not handle backspaces - it looks like in some cases we do, but in others, such as with this spinner, we don't.

In either case, I think we should fix these spinners.

Wrong escapes, am I doing something wrong?

Hi! This is a really cool library, it made the problem of the rendering of ANSI for a project I work just stupidly easy 😅 Thanks!

I found a problem with some stuff that is not properly rendered and I'm not sure if I'm doing something wrong (with the input) or if it is a bug.

I have the following text:

$ docker-compose down
Stopping server_tsplay_1 ... ^M
^[[1A^[[2K^MStopping server_tsplay_1 ... ^[[32mdone^[[0m^M^[[1BRemoving server_tsplay_1 ... ^M
^[[1A^[[2K^MRemoving server_tsplay_1 ... ^[[32mdone^[[0m^M^[[1BRemoving network server_default
$ docker-compose up -d
Creating network "server_default" with the default driver
Creating server_tsplay_1 ... ^M
^[[1A^[[2K^MCreating server_tsplay_1 ... ^[[32mdone^[[0m^M^[[1B$ docker-compose logs
Attaching to server_tsplay_1

This is how it looks on the terminal:

image

And this is how it looks rendered (split by line):

image

<div className="logs">
  {log.split('\n').map((line, index) => (
    <Ansi key={index}>{line}</Ansi>
  ))}
</div>

This is just the text, just in case it serves as reference.

$ docker-compose down
Stopping server_tsplay_1 ... done
Removing server_tsplay_1 ... done
Removing network server_default
$ docker-compose up -d
Creating network "server_default" with the default driver
Creating server_tsplay_1 ... done
$ docker-compose logs
Attaching to server_tsplay_1

Specify the React peer dependency

This package implicitly relies on React yet doesn't specify it. I'm in the process of making PRs for other repos to bump to React 15 and noticed this one is missing it.

An in-range update of enzyme is breaking the build 🚨

Version 2.8.1 of enzyme just got published.

Branch Build failing 🚨
Dependency enzyme
Current Version 2.8.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As enzyme is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details
Commits

The new version differs by 33 commits .

  • 8ab9528 v2.8.1
  • 75d1390 Merge pull request #876 from kentcdodds/pr/support-15.5
  • 21f6e7a [Tests] create-react-class should be a static dev dependency.
  • 4464a17 [Tests] move helpers in to test/_helpers dir
  • 22f368f address final comments
  • cc78489 Update error message in react-compat
  • b48e551 Change condition in performBatchedUpdates to a version check
  • 2f957af REACT155 constant is now true for react 15.5 or above
  • f5f6001 Update ReactWrapperComponent to use prop-types package
  • 3ff9832 Update karma cofig to be compatible with [email protected]
  • ec7bbc5 Lint
  • 270ee7f Remove unnecessary tests
  • d6badda Fix import
  • edeb99c Remove dependency on create-react-class
  • b0e2fac Extract batchedUpdates to a function

There are 33 commits in total. See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Improve performance for rendering large amounts of text

Ansi can be very slow for rendering large amounts of text to the point where nteract cannot keep up and crashes.

For me performance issues are appearing when executing anything larger than:

print("This is too large to render smoothly :( "*500000)

This seems to be caused by the Ansi component even without the linkify option. Improving the performance of escape-carriage also didn't help 😕

cant install with react 17

could u change this in the package.json
to allow react v17 ?

i can confirm that it works for me with react 17, if i force install, but it would be better to just install without force.

"peerDependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2"
  }

it could be

    "react": "^16.3.2 || ^17.0.0",
    "react-dom": "^16.3.2 || ^17.0.0"

Support for React 18

Currently, peer dependencies are set to allow v16 and v17 of react and react-dom. This prevents projects using this module from upgrading to v18.

Is it straightforward to also support ^18.0.0?

Catastrophic backtracking regex condition

Looks like the current linkify regular expression does not handle well strings with a significant number of whitespaces characters in a row (potentially a Catastrophic backtracking regex condition / ReDoS?)

Here is a reduced test case that replicates the root issue showing the potential regex fix and the current regex crashing

Module not found: Can't resolve 'ansi-to-react' with next.js

I have an error when opening page of a project using nextjs :

./pages/xxx.js:10:0
Module not found: Can't resolve 'ansi-to-react'
> 10 | import Ansi from "ansi-to-react";

versions :

  • react 17
  • ansi-to-react 6.1.1

I have no error when using ansi-to-react 5

Does anyone have a clue ?

here is the code

// pages/xxx.js
import React from 'react'
import Ansi from "ansi-to-react";

function sexylogs() {
    return (
        <Ansi>{'test'}</Ansi>
    );
}

Runtime failure when trying to enable Ansi blocks

Why trying to enable <Ansi> via https://review.opendev.org/#/c/716251/4/web/src/containers/build/BuildOutput.jsx I observed that this produces failures like https://storage.gra.cloud.ovh.net/v1/AUTH_dcaab5e32b234d56b626f72581e3644c/zuul_opendev_logs_aa6/716251/4/check/zuul-build-dashboard/aa6b67f/npm/html/build/f88a6e20be834d5a8245b5473e609a8e

TypeError: "e.split is not a function"
    value index.js:291
    value index.js:251
    value index.js:99
    default index.js:28
    default index.js:115
    React 6
    unstable_runWithPriority scheduler.production.min.js:19
    React 6
    Redux 7
    N build.js:280
react-dom.production.min.js:209:194
    React 5
    unstable_runWithPriority scheduler.production.min.js:19
    React 4
    unstable_runWithPriority scheduler.production.min.js:19
    React 6
    Redux 7
    N build.js:280

This error happens only on this proposed change so it should have something to so with how I tried to use it.

Add support for multilines

Current implementation assumes it will render a single line which is really problematic because ANSI text cannot just be split by line and processed individually, mainly because an escape set on one line will not be reset at the next line.

Another issue with multilines is that i completely breaks the formatting if you feed it multiline strings by combining the lines.

Ideally it should produce results similar to <pre> blocks. This means it it should, at least, produce different spans+<br/> or for each line.

Release ESM module

Hello, it looks like the current npm package is a commonjs module, but when building locally I obtain an ESM module so it looks that the published version is not the latest one and ESM module looks like a better option anyway since it's the standard now.

Linkify breakage cases

I'm finding more and more notebooks that are breaking with ansi-to-reacts Linkify setup.

Example cases:

 <transport.model.TransportInfo
<module 'something' from '/usr/local/lib/python2.7/dist-packages/something/__init__.pyc'>

Document how to run tests

I want to contribute some changes to ansi-to-react but I am unable to find the command I need to run in order to run its unittesting. I am sure that this may be obvious for some JS devs but for occasional contributors it is not.

One or two lines added to the README would likely be enough to give a head start.

Make content linkifying optional

Could you please make linkifying of the content that was recently released optional?

It is causing side effects that are not desired.

Consider transpiling

There are some non-ES5 bits in this code that, while they don't affect our desktop app, do affect old browsers when bundled and uglified for production. We either need to ditch the template literals and use of const or rely on a babel setup here too.

Switch to flow instead of prop-types

ansi-to-react is AFAIK the only nteract module that uses prop-types as a direct peer dependency. I think we should switch to flow.

Should we add ansi-to-react to the nteract monorepo as well?

Broken version 6.0.6

Hi! Version 6.0.6 seems broken. There is no lib directory, so it can not load. All is fine in 6.0.5.

Dark/light mode.

I've spent 30 minutes trying to figure out how to achieve dark mode, only to find out it was REALLY simple.

For the benefit of others who wish to support ANSI output in both dark/light mode:


Ansi output does not normally dictates the background and text color of non highlighted text. Ansi colors render well on both dark and light backgrounds.

For dark/light modes, simply set the background-color and color style properties of the container:

import Ansi from "ansi-to-react";
export function AnsiOutput() {
  return (
    <div style={{ backgroundColor: 'black', color: 'white' }}>
      <Ansi>{'\u001b[34mhello world'}</Ansi>
    </div>
  );
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.