Comments (9)
I agree that we shouldn't extend the String class. We could use the Rainbow gem and refine String just for AP's use like described here. Then @ddarbyson can do the same in his scripts if needed. To detect whether the TTY supports colors, use tty-color.
from amazing_print.
Seems like this removing the monkey patches would also close #36
from amazing_print.
Hi @ddarbyson and thanks for using AmazingPrint and asking your question here!
There's two ways that colors can get set. With Ruby 2.7+ IRB supports colorization natively. This can be disabled with
IRB.conf[:USE_COLORIZE] = true
See https://docs.ruby-lang.org/en/2.7.0/IRB.html for more info
When colors are off in IRB but on in AmazingPrint, we can turn them off for a single output providing the plain: true
option to ap
We see that nil
has color again because we only turned off colors for formatting the string and not the return value of the ap
command itself.
from amazing_print.
Hey, @HarlemSquirrel thanks for getting back to me.
I should clarify my intention. We are not using IRB and Amazing Print in the traditional sense. For us, AP has become a staple for writing programs so we require the module always.
But doing this conflicts with the String mixin you have.
https://github.com/amazing-print/amazing_print/blob/master/lib/amazing_print/core_ext/string.rb
By design, AP is injecting colour methods directly into the String class (i.e. "my string".red
). You can see, if we are using AP as a staple gem, then these assumed methods actually conflict and assume too much.
For example, this conflicts with other code which handles the colour of the string. In this case, we opted to drop our previous gems which handle colour in favour of AP String mixins. I suppose that is okay, however, when using our program in none tty, there is no control over the colour.
So I would propose 2 changes
- Don't assume anything should be mixed into standard Ruby classes unless specified
- Allow the string.rb mixing to detect no tty and do not use colour
from amazing_print.
@ddarbyson We are doing some detection for tty here
amazing_print/lib/amazing_print/inspector.rb
Lines 98 to 115 in 5a1a940
I also agree that we should shouldn't mix in to standard library. For colorizing output there's the more robust rainbow gem that can add those mix ins.
@paddor what do you think?
I'm happy to review a PR to refactor how we colorize here but I don't have the time to dig in myself at the moment.
from amazing_print.
hi @HarlemSquirrel I investigated the colourize?
method and I do see ENV['TERM']
control, however, it has no effect when using the colours methods provided by the String mixin in such as "mystringexample".red
Having said that, .red
is not influenced by ENV['TERM']
and therefore using the colour mixins provided by AP String class still has coloured output in none tty sessions - which produces unescaped character sequences in email or log files; that's why i'm looking to turn off colour.
To reiterate, in our case, we use AP String mixin for colour, b/c there's no way of turning it off, and there's no point in introducing another gem to handle colour.
What would be ideal for AP is to introduce two improvements:
- Have AP colour String mixin optional
- Have AP colour String be influenced by
ENV['TERM']
from amazing_print.
I really like the idea and appreciate the thought you guys have put into it. It really does solve all my issues at once and furthermore, our developments will benefit from the Rainbow gem.
I would be more than happy to test once it's ready.
from amazing_print.
@ddarbyson I currently donβt have a lot of spare time. If possible, could you provide a PR that does this? I guess that PR would remove the coloring methods and do something like using Rainbow
wherever colors are used.
from amazing_print.
See: #91
from amazing_print.
Related Issues (20)
- v1.5 contains a breaking change due to the string convenience methods being removed HOT 1
- IRB integration error in OS Ventura HOT 9
- Raw Mode to avoid double encoding HOT 3
- Gemfile order matters HOT 2
- Argument error with Ruby 2.7.2 HOT 4
- Problems in use Rack::Test HOT 2
- Replace .aprc with .amazing_print.yml HOT 4
- Peculiar error hit when doing inspect in rails HOT 2
- Is there a way to limit or exclude a verbose fields? (i.e. large JSON values) HOT 5
- Is there a way to not include associated records? HOT 3
- JSON HOT 8
- class_name option doesn't seem to do anything HOT 1
- Default colors only last for one print HOT 6
- Use paging with Pry? HOT 2
- Implement a depth options HOT 2
- rails ActiveModel::Attributes support HOT 1
- undefined method `marshal_dump' HOT 5
- object_id option not operating as expected HOT 1
- Output with ruby19_syntax: true is not copy pastable HOT 3
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 amazing_print.