com-lihaoyi / fansi Goto Github PK
View Code? Open in Web Editor NEWScala/Scala.js library for manipulating Fancy Ansi colored strings
License: Other
Scala/Scala.js library for manipulating Fancy Ansi colored strings
License: Other
scala> Nil.mkString(",")
val res0: String = ""
scala> fansi.Str.join(Nil, "")
val res1: fansi.Str =
scala> fansi.Str.join(Nil, ",")
java.lang.NegativeArraySizeException: -1
at fansi.Str$.join(Fansi.scala:370)
... 66 elided
I'm not sure if it's a bug, but allowing (Nil, ",") sounds more consistent.
Scala 3.1.3, fansi 0.3.1
Hello, thanks for the awesome libraries you have made!
Can we make errorMode
in
def apply(raw: CharSequence, errorMode: ErrorMode = ErrorMode.Throw): fansi.Str
implicit?
That way would be possible to overwrite it when using PPrint as in com-lihaoyi/PPrint#35 and com-lihaoyi/PPrint#78, and in my case when using Almond, when we don't have control of the underlying code.
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.
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
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.
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.
I tried a number of ways to use fansi in printing log too. Is it possible to do so ?
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.
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)
}
file:///Users/lihaoyi/fansi/fansi/shared/src/main/scala/fansi/Fansi.scala
As discussed on Gitter, https://github.com/lihaoyi/fansi/blob/master/fansi/shared/src/test/scala/fansi/FansiTests.scala#L220 is missing assert
. After adding assert
, the assertion always fails.
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?
Continuing from a discussion from Discord:
It's an ancient tradition to write fansi-2-html converters and copypaste them into projects.
For example:
Perhaps we could make this project multi-module and add blessed implementations, with tests and other nice things?
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.
Which OSS license is this code distributed under?
Would it be possible to publish a release or a snapshot that supports scala 2.13.0-M4?
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.