Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No aligns with ANSI characters (for colors) #6

Open
alexandre-mbm opened this issue Sep 20, 2014 · 3 comments
Open

No aligns with ANSI characters (for colors) #6

alexandre-mbm opened this issue Sep 20, 2014 · 3 comments

Comments

@alexandre-mbm
Copy link

No description provided.

@Larusso
Copy link
Contributor

Larusso commented Feb 4, 2015

The problem is, that ljust, rjust and center calculate the length of the string value with the hidden escaped ANSI characters.
I use this in my code. I apply the color after the alignment:

require 'colorize'

module Text
    class Table
        class Cell
            def initialize(options = {}) #:nodoc:
                @value        = options[:value].to_s
                @row          = options[:row]
                @align        = options[:align  ] || :left
                @colspan      = options[:colspan] || 1
                @color        = options[:color] || :default
                @background   = options[:background] || :default
                @effect       = options[:effect] || :default
            end

            def set_color(string)
                string.colorize color: @color, background: @background, mode: @effect
            end

            def to_s #:nodoc:
                ([' ' * table.horizontal_padding]*2).join case align
                when :left
                    set_color(value.ljust cell_width)
                when :right
                    set_color(value.rjust cell_width)
                when :center
                    set_color(value.center cell_width)
                end
            end
        end
    end
end 

https://gist.github.com/ce7df9a788e945e83cae.git
I thinking about if it is worth to either integrate into text-table or create a separate gem that extents text-table and integrates this.

@aptinio
Copy link
Owner

aptinio commented Feb 4, 2015

I'd like people to be able to do this using text-table but I haven't decided on the best way to do it. One of the things I've been thinking of is allowing a post_processor proc to be passed in like so:

highlight = ->(cell) { cell.content.colorize :yellow }
table.rows << [{ value: 'reminder', post_processor: highlight }]

In this example, Cell#content still needs to be implemented.

I was also planning of rewriting everything except the specs. That's why I haven't gotten to resolving this issue. But that remains a plan. Pull requests are welcome though :-)

@mattheworiordan
Copy link

Thanks @Larusso I am using your patch, works well, although it's a bit hacky! Hopefully @aptinio will find a better solution. Sorry @aptinio not enough of an issue for me to do a PR at this stage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants