onevcat / rainbow Goto Github PK
View Code? Open in Web Editor NEWDelightful console output for Swift developers.
License: MIT License
Delightful console output for Swift developers.
License: MIT License
CocoaPods
and Carthage
are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.
It would be cool to add the Manual installation guide in your README.md
. You can take a look at my iOS Readme Template to see how you can do it.
Thanks for the great library! Any chance of supporting 256 colors? (like https://github.com/junegunn/ansi256/)
xcode 12不能实现所说的效果,是我没有使用对吗
These two lines return an optional value now, and are throwing the error Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
I'm not sure what the best way to handle this would be for this project, but in the return line, maybe doing something like return (codes, text ?? "")
Rainbow/Sources/ModesExtractor.swift
Line 47 in 5d5e3d3
Rainbow/Sources/ModesExtractor.swift
Line 78 in 5d5e3d3
Wei, thanks for your contribution to this project, my new tool relies on it strongly.
After upgrading to the new version 4.0.0, I use the following code to join a colorful message in terminal console, but got something unexpected results:
import RainbowSwift
let entry = Rainbow.Entry(
segments: [
.init(text: "Hello ", color: .named(.magenta)),
.init(text: "Rainbow ", color: .bit8(214), backgroundColor: .named(.black), styles: [.underline]),
.init(text: "Hello ", color: .named(.magenta)/*, backgroundColor: .named(.default)*/), // Comment 1
.init(text: "again", color: .named(.magenta), backgroundColor: .named(.red)/*, styles: [.default]*/), // Comment 2
]
)
print(Rainbow.generateString(for: entry))
It shows the black background and styles from the second segment on the second Hello
, this is weird, the underline style also affects to the last segment:
If I uncomment the Comment 1
only:
If I uncoment the Comment 2
only:
I really don't know how to explain them even after trying to look into the Rainbow
inside, please confirm is it a bug or how should I use the Segment
.
Thanks!
Using Xcode 7.3 cocopods. I added Rainbow in the Podfile per your README. I also remembered to import RaindobowSwift (vs Rainbow). Now, when I try to use it, type-ahead shows me the colors so I know the class is being read. However, no text comes out colored. (ie print("hello myself".red)
just prints "hello myself" in the normal console color. I feel like xcode is somehow overriding the coloring maybe.
Ideas?
PS I am in a bridged obj-c/swift project if that's a clue. However, I am only trying this from the Swift side without luck...
There is no reason that we do not add the crossline support to Style
.
First of all, thank you for putting together this library. It's great work!
I share your motivation that Swift will become incredibly useful for command line programs. This is the reason that I started SwiftyTextTable to better organize complex console output.
I think our libraries are a natural pair, and I would like to make them work seamlessly together. Unfortunately, right now the escape sequences that Rainbow uses to inject color and style information are not compatible with my (rather naive) method of calculating string lengths to determine column widths. This is being tracked in this issue: scottrhoyt/SwiftyTextTable#5.
It would be my preference to not introduce a dependency on Rainbow to keep the library as lightweight as possible. Therefore I was wondering if you could assist me in designing an algorithm to strip Rainbow styles and colors from a string for length calculations. From my light reading of the extraction methods already present in Rainbow, it looks like I can detect the presence of a formatted string, and if necessary, apply a regex to extract the text information out of the style information, but I would like to hear your thoughts on implementation. Thanks!
Please consider supporting FORCE_COLOR
: https://force-color.org/ and/or enabling color programmatically.
Use case is some tool that runs in a daemon mode, but the output gets copied to a tty in a roundabout way.
Here is a minimal example:
import Foundation
import Rainbow // marathon: https://github.com/onevcat/Rainbow.git
print("teste".lightGreen)
Which, under Ubuntu 18.04 does not generate any color. Am I doing something wrong?
Hi,
I test the Rainbow with below enviorment:
xcode : Version 12.0.1
but can't print text color.
i found getEnvValue("XcodeColors")
return false.
so how to solve it? can you help me?
In my command line tool, when I add > filename.txt
to output results in a file, it creates an empty file. The output is provided in the console though.
See more details: rsrbk/GoSwifty#3
Hi, I ❤️ using Rainbow in my projects.
I am however stumbling across an issue where I need to use the length of a string as in .characters.count
or just .count
in Swift 4. Obviously this changes when introducing escape sequences, so I was looking for a way to remove these. Rainbow offers the handy .removingColor()
method on strings, but unfortunately this seems to just replace the escape sequences with default values. Is there a way to access only the actual string content itself? That would be fantastic!
Hi, could you please add a pre-built .framework?
Thank you
Hey, I'd love to see this lib available in Swift 3 so I can implement it in my project.
Thanks 😄
I followed the directions in the readme file and while I'm able to see Rainbow install when I add the .package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0")
line to the dependencies array, I get No such module Rainbow
when I attempt to import it in main.swift
.
I tried adding it to targets, too, like so:
targets: [
.target(
name: "MyCLIUtility",
dependencies: ["SPMUtility", "Rainbow"]),
.testTarget(
name: "MyCLIUtilityTests",
dependencies: ["MyCLIUtility"]),
]
What am I missing?
Hello,
This simple code has an unexpected result:
print(("a" + "b".underline + "c").lightGreen)
I would expect the whole string to be in green, and the b
only to be underlined, however the underlining doesn't get reset, so the c
gets underlined too!
Expected result: \e[92ma\e[92;4mb\e[0;92mc\e[0m
Actual result: \e[92ma\e[92;4mb\e[92mc\e[0m
This happens with all styles when applied to a substring where a foreground color is inherited.
just tried to install via cocoapods and ran into a buttload of errors:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'RainbowSwift', '~> 1.1'
installed again without '~> 1.1'
and it went fine
Hey there. I'm trying to build a string similar to a Powerline prompt.
It's a series of segments, separated by a custom powerline triangle glyph. The glyph is coloured predicably: the triangle part of is coloured based on the text colour, and the background is coloured based on the background. But the glyph exists to
connect two different background colours.
This means you want to set its text colour to the equivalent of the background colour of whats to the left, and the background colour to be equal to the background colour of whats to the right. To achieve this, you need a way of converting a BackgroundColor
to its Color
counterpart.
Here's my example:
import Rainbow
extension BackgroundColor {
var toTextColor: Color {
switch self {
case .black: return .black
case .red: return .red
case .green: return .green
case .yellow: return .yellow
case .blue: return .blue
case .magenta: return .magenta
case .cyan: return .cyan
case .white: return .white
case .default: return .default
}
}
}
struct Segment {
let text: String
let textColor: Color = .default
let backgroundColor: BackgroundColor
}
extension Collection where Element == Segment {
func joined() -> String {
let dummySegment = Segment(text: "", backgroundColor: .default)
let strings = zip(self, self.dropFirst() + [dummySegment]).map { current, next -> String in
let separator = "".applyingCodes(current.backgroundColor.toTextColor, next.backgroundColor)
let text = current.text.applyingCodes(current.textColor, current.backgroundColor)
return text + separator
}
return strings.joined()
}
}
let segments = [
Segment(text: "abc", backgroundColor: .red),
Segment(text: "def", backgroundColor: .green),
Segment(text: "ghi", backgroundColor: .blue),
]
let result = segments.joined() + " The rest of the message"
print("before\n")
print(result)
print("\nafter")
Output (rendered in SF mono nerd font:
I wanted to contribute the implementation of toTextColor
, because I think it might be useful to others. The reverse direction might also be useful, though there are more Color
s than can map onto BackgroundColor
s, so its unclear how to handle the unmappable cases (make the computed property optional and return nil
?)
Do you think this would be useful for me to contribute?
If so, how do you think I should write the test for it?
Following to the documentation, I tried the code
let output = "The quick brown fox jumps over the lazy dog"
.stringByApplying(Color.Red, BackgroundColor.Yellow, Style.Bold)
but the code didn't compile with the error
error: value of type 'String' has no member 'stringByApplying'
let output = "The quick brown fox jumps over the lazy dog"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My logs are still black. When I set Environment Variable "XcodeColors" to YES, my output is like this: �[fg255,0,0;Recognized a double tap�[;
The inconsistency in naming (Rainbow / RainbowSwift) makes it difficult to build the same project using Xcode and SPM, without editing imports to one or the other.
could this use for iOS project? i use this in my iOS project , and it doesn't work(doesn't show extra console).
what i' done was:
1.add RainbowSwift in my pod file (with right swift version)
2.import RainbowSwift
3.print("xxxxx".red)
please help
Xcode 8+不去签名 XcodeColors插件不能~~~ @onevcat ☠️
Hi!
Awesome looking library but for me it doesn't print in colour. I installed it with Cocoapods. I must be missing something here. I'm guessing there are some dependencies I need? Do I need this: https://github.com/robbiehanson/XcodeColors ??
Cheers
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.