Giter VIP home page Giter VIP logo

fansi's People

Contributors

alexarchambault avatar anatoliykmetyuk avatar ckipp01 avatar felixbr avatar jodersky avatar lihaoyi avatar lolgab avatar masseguillaume avatar scala-steward avatar sethtisue avatar xuwei-k avatar yilinwei 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fansi's Issues

Add a way to efficiently build up a large fansi.Str

Using ++ is inefficient if done repeatedly to many small items, and there's no equivalent of mkString. You could serialize everything into a StringBuilder and parse it again after, but that's wasteful. Ideally you want something like

  class FansiBuilder{
    val sb = new StringBuilder
    val state = new Aggregator[fansi.Str.State]()
    def append(c: Char) = {
      sb.append(c)
      if (state.isEmpty) state.append(0)
      else state.append(state.last)
    }
    def toStr = fansi.Str.fromArrays(sb.toArray, state.toArray)
  }
  

Expose getColor(i: Index) and getChar(i: Index) methods on fansi.Str

Currently the only way to access the char or color data is through getColors or getChars, which does an expensive copy to return the an array while preserving the immutability of the `fansi.Str.

We could expose single-index versions of these that would let a performance-minded user get the chars and colors one by one, while preserving the immutability of fansi.Str.

Publishing for Scala.js 1.0

Scala.js 1.0 is finally released and Scala.js ecosystem is moving forward to 1.0.
It would be great if fansi also published for Scala.js 1.0z

Add explicit dependency to scala-reflect

When just adding a dependency to fansi, once it is instantiated it throws the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/reflect/api/TypeCreator
	at fansi.ColorCategory.$anonfun$Full$1(Fansi.scala:851)
	at fansi.ColorCategory.$anonfun$Full$1$adapted(Fansi.scala:850)
	at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:234)
	at scala.collection.immutable.Range.foreach(Range.scala:156)
	at scala.collection.TraversableLike.map(TraversableLike.scala:234)
	at scala.collection.TraversableLike.map$(TraversableLike.scala:227)
	at scala.collection.AbstractTraversable.map(Traversable.scala:104)
	at fansi.ColorCategory.<init>(Fansi.scala:850)
	at fansi.Color$.<init>(Fansi.scala:717)
	at fansi.Color$.<clinit>(Fansi.scala)

One can avoid this error by adding the following dependency to the project using fansi:

libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value

However, it would be ideal if it was already in the transitive dependencies of the original library.

Disable ansi escapes

Is it possible to disable escapes for range of symbols? It is inconvenient to use tools such ammonite or almond when there are ansi codes instead of Russian letters

amm 2021-03-03 14-13-40

Fansi scala — fansi 2021-03-03 14-14-36

reconsider failing on escape characters

Hello!

I was looking at this issue for ammonite. The problem comes from fansi, because it would fail anything that contains \u001b or \u009b if that's not a part of a valid color. So, something like this will fail:
fansi.Str("\u001b")
I understand that this was the idea, but maybe there's no need to fail on "invalid" strings like that? If fansi can't parse it, maybe let it be part of the string?
Please let me know what you think. I can also make a PR, if that makes sense.

ANSI-to-HTML converter?

Continuing from a discussion from Discord:

It's an ancient tradition to write fansi-2-html converters and copypaste them into projects.

For example:

  1. Mine to be used from Scala.js https://github.com/neandertech/langoustine/blob/main/modules/tracer/frontend/src/main/scala/Ansi2Html.scala
  2. Mine to be used from JVM: https://github.com/indoorvivants/subatomic/blob/master/docs/Ansi2Html.scala
  3. @lihaoyi's to be used with scalatags (which he doesn't remember writing): https://gist.github.com/lihaoyi/f0545f714d105f30afd5f4191997e3ea

Perhaps we could make this project multi-module and add blessed implementations, with tests and other nice things?

Mention Scala Native in GitHub repo description

The description of this repo is currently "Scala/Scala.js library for manipulating Fancy Ansi colored strings." Someone who wants a library for Scala Native may dismiss fansi because of that. Could the description be modified to include Scala Native?

IllegalArgumentException: Unknown ansi-escape [2m

Summary

One of the Metals users hit an issue when Metals wasn't displaying errors which came from ZIO test suite. After an investigation I found that issue is caused by Fansi. ZIO sets various ansi styles in their tests and at least one of them (Faint - "\u001b[2m") is unsupported by Fansi:

Caused by: java.lang.IllegalArgumentException: Unknown ansi-escape [2m at index 206 inside string cannot be parsed into an fansi.Str`

In Metals, we use Fansi to obtain plain text from bsp diagnostic before forwarding it to the lsp client.


Reproduction

//> using scala "2.13.8"
//> using lib "com.lihaoyi::fansi:0.3.0"

object Fansi extends App {
  val Reset = "\u001b[0m"
  val Faint = "\u001b[2m"
  
  val byHand = Faint + "Foo" + Reset

  println("Foo")
  println(byHand)

  val plainText: fansi.Str = fansi.Str(byHand).plainText

  println(plainText)
}

Behavior

➜  scala-cli run Fansi.scala
Compiling project (Scala 2.13.8, JVM)
Compiled project (Scala 2.13.8, JVM)
Foo
Foo
Exception in thread "main" java.lang.IllegalArgumentException: Unknown ansi-escape [2m at index 0 inside string cannot be parsed into an fansi.Str
        at fansi.ErrorMode$Throw$.handle(Fansi.scala:440)
        at fansi.ErrorMode$Throw$.handle(Fansi.scala:428)
        at fansi.Str$.apply(Fansi.scala:280)
        at Fansi$.delayedEndpoint$Fansi$1(Fansi.scala:13)
        at Fansi$delayedInit$body.apply(Fansi.scala:4)
        at scala.Function0.apply$mcV$sp(Function0.scala:39)
        at scala.Function0.apply$mcV$sp$(Function0.scala:39)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
        at scala.App.$anonfun$main$1(App.scala:76)
        at scala.App.$anonfun$main$1$adapted(App.scala:76)
        at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
        at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
        at scala.collection.AbstractIterable.foreach(Iterable.scala:926)
        at scala.App.main(App.scala:76)
        at scala.App.main$(App.scala:74)
        at Fansi$.main(Fansi.scala:4)
        at Fansi.main(Fansi.scala)

Expected Behavior

Fansi should parse \u001b[2m ansi code, as well as other styles defined in linked file from ZIO repository.

Apply overlay by pattern

It would be nice to be able to apply an overlay on a fansi.Str using a pattern to find instead of specifying indexes by hand.

Colour leakage on Windows CMD & Cygwin

Hey,

I'm not sure if fansi supports Windows at the moment. I have seen colours leaking in both Windows 7 CMD and Cygwin.

Issuing the command:

fansi.Color.Red("Hello World Ansi!")

produces:

image

and I have to manually reset the colours back.

I have tested this on MacOSX and it's not an issue:

image

Parsing standard-conform ansi encodings from other applications

Fansi doesn't currently parse many standard-conform ansi encoded strings. E.g. we're using source-highlight to render coloured sourcecode, which on the terminal renders nicely coloured text:

echo "if" > if.c
source-highlight-esc.sh if.c

So that we can display the output with fansi, I came up with four regexes to preprocess the ansi-encoded Strings before handing them to fansi.Str.apply. The comments give an example for each of the four differences, and tests are at https://github.com/ShiftLeftSecurity/codepropertygraph/blob/9a7b743/console/src/test/scala/io/shiftleft/console/PPrinterTest.scala

Would you like fansi to handle those standard-conform ansi encoded Strings, or rather make it a non-goal and not have the complexity in the parsing logic?

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.