Skip to main content

`AmazingPrintInspect` does not colorize

Consider a service that includes the AmazingPrintInspect configuration.

Here is a minimal example.

ConvenientService::Dependencies.require_amazing_print_inspect

class Service
include ::ConvenientService::Standard::Config
include ::ConvenientService::AmazingPrintInspect::Config

def result
success(foo: :bar, baz: :qux)
end
end

When it is called from the console like IRB.

Service.result

The result should be pretty-printed by amazing_print.

But it actually outputs some "weird escape characters" ^[[0;37m, ^[[0m, or ^[[0;33m like in the screenshot below.

After a deep diving into IRB source code, it was figured out that the issue may be caused by the upgrade of the Reline gem from Ruby std lib.

To be more precise, the Reline::Unicode.escape_for_print method now has more escaping rules.

One of them replaces 0x1B.ord to ^[.

That is why the coloring becomes broken.

There is one workaround to avoid the issue.

Check the following monkey patch.

class Reline::Unicode
def self.escape_for_print(str)
str
end
end

Once it is applied, the output is colorized as expected.

danger

Currently, it is not known which exact problem was solved by the Reline developers by introducing additional escape rules.

We are preparing to open a GitHub issue to ask.

Consequently, although the suggested monkey patch looks safe, we can not estimate its possible negative effect.

Use it at your own risk.