ku1ik / rainbow

Ruby gem for colorizing printed text on ANSI terminals
MIT License
813 stars 68 forks source link

X11 named color methods not available through String refinement #128

Closed tobyh closed 11 months ago

tobyh commented 12 months ago

Version

Ruby: 3.2.2 Rainbow gem: 3.1.1 or Rainbow commit: 629cde5

Description

Using the String refinement interface, ANSI foreground color methods can be used but their X11 counterparts cannot.

$ irb --simple-prompt -r rainbow/refinement
>> using Rainbow
=> main
>> Rainbow("hello").red
=> "\e[31mhello\e[0m"
>> "hello".red
=> "\e[31mhello\e[0m"
>> Rainbow("hello").tomato
=> "\e[38;5;209mhello\e[0m"
>> "hello".tomato
(irb):5:in `<main>': undefined method `tomato' for "hello":String (NoMethodError)

Workarounds

Diagnosis

The reason the ANSI methods work but the X11 ones don't is that the refinement works by defining Presenter's instance methods onto String. While the ANSI methods are explicitly defined on Presenter, the X11 ones are handled by Presenter's method_missing, so they don't get added. I'll make a PR.

olleolleolle commented 11 months ago

Fixed in #129