Comments (26)
0.9.0 released here: https://github.com/scottrhoyt/SwiftyTextTable/releases/tag/0.9.0
We'll need @scottrhoyt to publish the release on CocoaPods because I don't have access rights to release it.
from swiftytexttable.
@kiliankoe, I was able to come up with a regex to strip the console escapes for column width calculations. The results are available in this branch: https://github.com/scottrhoyt/SwiftyTextTable/tree/console_escape_stripping
Feel free to start using that as the public API won't change, but I may clean it up a bit before merging into master, which should be in the next day or two.
from swiftytexttable.
I have, it seems to work just fine 😊 Thanks again!
from swiftytexttable.
@eneko and @jpsim works great now, thanks!
from swiftytexttable.
Just an FYI, it looks like underline and bold suffer from the same issue (see Magnitude 6.4 on the 5th row):
print("X".red.underline.debugDescription) // "\u{1B}[31;4mX\u{1B}[0m"
print("X".red.bold.debugDescription) // "\u{1B}[31;1mX\u{1B}[0m"
from swiftytexttable.
Thanks for filing this!
Getting proper support for Rainbow was on the roadmap, so I'll take a look at this when a couple of free hours open up. It's quite possible that we can calculate string length in an alternative fashion (probably by stripping escape sequences). This also dovetails a bit with my intention to add multiline rows (basic support is sitting on a feature branch).
If you have any inclination to contribute a PR, I think the fix might be pretty straightforward, and I'd certainly welcome the help! Otherwise, I will probably get to it this week.
Going to rename this issue to better track it.
from swiftytexttable.
Thanks for the quick reply! I'll look into a fix for this, but can't promise anything. My knowledge of escape sequences is rather limited. But I'll definitely give it a shot!
from swiftytexttable.
No problem. Let me know if I can be of any assistance. My first hunch is that casting the values
to NSString
would probably open up some more powerful string functions. The functions for trimming characters in a character set would be promising.
from swiftytexttable.
So I looked a bit more into this. Unfortunately the escape sequences used by Rainbow to generate color and style information are a bit more complex than I was hoping, and they differ for platforms. While it would be possible to write a regex that stripped these for length calculations, the simplest way to accomplish this is probably just creating a dependency on Rainbow and using it's built-in functions for clearing color and styles to calculate length. I'll have to give some thought as to whether that would be a good idea, as I would like to avoid dependencies to keep the library as lightweight as possible.
from swiftytexttable.
That's so cool, thank you for putting the work into this!
from swiftytexttable.
No problem! Having Rainbow support was needed IMO. In the future I'd like to explore potentially integrating table wide support for Rainbow. Also I'm going to add some protocols to make customizing the table display of values and displaying objects as rows easier.
from swiftytexttable.
Never mind, went ahead and cleaned it up before I went to bed. It's merged into master
and a new release (0.2.2) is available now. Cheers!
from swiftytexttable.
@kiliankoe were you able to successfully create tables with color info from Rainbow?
from swiftytexttable.
Awesome!
from swiftytexttable.
Hi @scottrhoyt 👋
Unfortunately this issue seems to have returned. I've also tried it with https://github.com/mtynior/ColorizeSwift to be sure.
import SwiftyTextTable // 0.6.0
import Rainbow // 2.0.1
let columns = ["foo", "bar", "baz"].map { TextTableColumn(header: $0) }
var table = TextTable(columns: columns)
table.addRow(values: ["1", "2", "3"])
table.addRow(values: ["4".green, "5".yellow, "6".red])
print(table.render())
leads to
I've looked around the source but can't seem to find anything pertaining to this. Also the tests seem to pass just fine. Do you know what the reason for this could be?
from swiftytexttable.
Hmm. That's interesting @kiliankoe. Thanks for bringing it to my attention. Can you grab the escaped strings and post them here? My initial guess is that the the escape sequence used by Rainbow changed slightly and the stripping regex no longer catches it.
Also, double checking, are you seeing the incorrect behavior in terminal or in Xcode's console?
If you are interested in contributing at all, it would be awesome to throw those sequences into some more tests and rework the stripping regex to catch them.
from swiftytexttable.
I've had the same problem and I've "fixed" it in my fork. I did not make a pull request because it most likely breaks something else. I hope this can be of some use to you! 😄
Changes
First I've changed the stripping parten. It probably only matches the escape sequences I use.
-private let strippingPattern = "(?:\u{001B}\\[(?:[0-9]|;)+m)*(.*?)(?:\u{001B}\\[0m)+"
+private let strippingPattern = "\\\u{001B}\\[[0-1];[0-9][0-9]m"
Then I changed the with padding to use strippedLength()
instead of count
private extension String {
func withPadding(count: Int) -> String {
-#if swift(>=3.2)
- let length = self.count
-#else
- let length = self.characters.count
-#endif
+ let length = self.strippedLength()
+
if length < count {
return self +
repeatElement(" ", count: count - length).joined()
Pictures
Before
After
Examples of the escape sequences I use for color
public enum StringColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case boldRed = "\u{001B}[1;31m"
case green = "\u{001B}[0;32m"
case boldGreen = "\u{001B}[1;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case boldBlue = "\u{001B}[1;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
}
from swiftytexttable.
Thanks for sharing this @enari ! 🥇 Are the unit tests still passing? If yes, I think if you can just come up with a unit test or two that fails on master
but passes on your fork, then we should absolutely merge your fork. Let me know!
from swiftytexttable.
The unit test's are not passing. The strippingPatterns has to be combined.
from swiftytexttable.
@scottrhoyt Seems like this PR is ready to go. @enari 👏
from swiftytexttable.
@eneko: Yes, maybe it could use more testing, but it works and the unit tests are passing. However, @scottrhoyt does not seem to have been active lately. Maybe @jpsim can have a look and merge #26?
from swiftytexttable.
I think this issue is back.
import SwiftyTextTable //0.8.2
import Rainbow // 3.1.4
var testTable = TextTable(columns: [
TextTableColumn(header: "col1"),
TextTableColumn(header: "col2"),
TextTableColumn(header: "col2")])
testTable.addRow(values: [1, "2".red, 3])
print(testTable.render())
from swiftytexttable.
@travispaul: Could you find what escape sequence Rainbow uses for .red
?
I think you can use print("2".red.debugDescription)
from swiftytexttable.
Hi @Roslund, Looks like the escape sequence is "\u{1B}[31m2\u{1B}[0m"
from swiftytexttable.
@travispaul This code has been landed on master
branch, however a new release has not been done since these updates were merged.
@jpsim could you please do a new 0.9.0 release?
from swiftytexttable.
@travispaul I think a change to the regex should solve this. Try changing /Source/SwiftyTextTable/TextTable.swift
line 13
-private let strippingPattern = "\\\u{001B}\\[([0-9][0-9]?m|[0-9](;[0-9]*)*m)"
+private let strippingPattern = "\\\u{(1B}\\[[0-9][0-9]?(;?[0-9]*)*m"
from swiftytexttable.
Related Issues (11)
- Run unit tests with SPM HOT 3
- CocoaPods HOT 1
- Document Public API
- Carthage failing on CI builds (Xcode 9.4 required)
- Swift 4.2 Carthage HOT 1
- new line support for long string
- Right alignment? HOT 2
- Multi-Byte Unicode Characters not counted correctly HOT 1
- Published CVE's in Swift text table HOT 2
- Updated Version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftytexttable.