Print a solid colored square with Ruby - ruby

I'm working on modifying Brett Weaver's Trello CLI ruby-based utility to include information from a Trello card object, namely the label colors associated with that card. Right now, I can do something like the following to iterate through the labels on each card:
list_cards.each_with_index do |card, i|
puts "#{i+1} #{card.name}"
card.labels.each do |label|
puts "(#{label.name})"
end
end
Rather than printing the label.name attribute, I would prefer to print a solid colored square that corresponds to the label.color attribute. An example of this can be seen in another library seen here. I've read about using the colorize library to get the color, but first I can't figure out how to get it to print a color dynamically rather than explicitly and second, I can't figure out how I would print a square. Additionally, the colorize gem doesn't have the range of colors that are needed from Trello (like orange or purple).

With the colorize gem, all you need to do is print two spaces on a background color, like:
puts " ".colorize(background: :blue)
to display a blue square.

Related

Color codes and string interpolation

I am trying to colorize text. Say I want blue text. This is the way I get it:
"\e[34mThis is blue text.\e[0m"
I am using define_method to create multiple methods (one for each color). I keep the color code for each color in an array. I iterate over both the color array and the color code, and do this:
"\e#{code}m[#{self}\e[0m"
When I run it, I get "m[test" instead of the colorized text.
Any thoughts? If, instead of #{code}, I put the actual code, it works, but that'd be like 20 ifs, one for each color, and it won't be DRY.
You are likely a victim of copy-paste :)
# ⇓ incorrect
puts "\e#{code}m[#{self}\e[0m"
# ⇓ correct
puts "\e[#{code}m#{self}\e[0m"
The opening square bracket should follow \e, not m.

colored text with highlighted data via colored gem

I am trying to achieve multi colored text with the colored gem in ruby.
Basically I want to set a base color for the text, but highlight specific details in another color while still keeping all the other text in the base color.
For example if I do something like:
puts "Found #{book_title.yellow} and it has #{chapters.to_s.yellow} chapters".green
I would of expected that the entire phrase would be green except for the book title and number of chapters which should be yellow.
Instead the results are that "Found" is green, book title is yellow, "and it has" is white (no color), chapters is yellow, and "chapters" is white (no color).
It simply appears that appending .green to the surrounding string doesn't re-add the color codes after a color change when using #{} to insert data.
I want to build this functionality into a info function like so:
def info(message)
time_stamp = "[#{Time.new.strftime("%H:%M:%S")}]"
puts "#{time_stamp} Info: #{message}".green
end
So that when I call it like so:
info "Found #{book_title.yellow} and it has #{chapters.to_s.yellow} chapters"
It will print the entire statement in green with highlighted text in yellow as described above.
Is this possible without breaking the string into substrings and concatting them, or specifying a hard coded list of arguments to the info function (the number of highlighted texts is dynamic). Is there a VA_LIST type like in C's printf I could use?
I wouldn't mind calling the function as:
info "Found %s and it has %d chapters", book_title.yellow, chapters.to_s.yellow
or something similar. But I would still need to figure out a way to set the color codes for each segment of text and concat them together.
Hopefully this makes sense.

Ruby curses: how to get pure white

I'm having a hard time trying to get a pure white (background) and black (foreground) text with ruby and curses.
With this code
Curses.init_pair(1,COLOR_BLACK,COLOR_WHITE)
Curses.attron(Curses.color_pair(1))
Curses.stdscr.addstr str
Curses.attroff(Curses.color_pair(1))
I get black text on a grayish background (the bottom three lines in
screenshot)
What could I be doing wrong? I tried switching from iterm2 to mac terminal
still the same.
Most of the terminals which implement "ANSI color" use a more intense foreground color when told to render bold. But there is no corresponding workaround for the background.
However, the majority of these also implement SGR 39 and SGR 49, which reset the foreground and background to its "default" colors. Using that would get your original terminal background (which is likely what you want). That is a feature of ncurses called use_default_colors, which I see is available in ruby.
Using that feature, your example would
call the Curses.use_default_colors method,
not bother (for example) to create a color pair, since the default pair 0 would draw using the terminal's default colors.
other color pairs (1 through the maximum number of pairs) would work just like regular curses.
The example given uses color pair 1. If you were using the default-colors extension, you could initialize Curses with the use_default_colors method. Then, using the default color pair 0, you would see on most terminals the original terminal colors. This method is from the underlying ncurses library irregardless of whether the Ruby package is called "curses" or "ncurses".
Color pair 0 is special (see manpage). It cannot be set to specific colors by your application. As an extension, ncurses provides a way to alter this in a useful way.

How do I make a form that changes color with input text in Ruby?

I'm new to Ruby, and I want to develop a program that asks, "What's your favorite color?." And once you type in your favorite color, the submit button changes to the color, and the next screen's background will be that color you typed in.
Can anyone help? Thanks!
You could do this with CSS and javascript. You could have a class background-color-blue with background-color: blue for example on your body or custom element.
Then, when submit is clicked, you simply change the class to background-color-red.
document.getElementById("MyElement").className = "background-color-red";
I would do some research using google/stackoverflow on how to change an elements css class depending on which web framework and programming languages you are using.
Ruby has a colorize gem, where you can change the color of text.
First, install the gem
gem install 'colorize'
Then, require the gem
require 'colorize'
You can now easily change the color of text.
string.colorize(:blue) => Changes your text color to blue
In your case, this can be implemented as follows
puts "What's your favorite color?"
color = gets.chomp
puts color.colorize(color.to_sym)
Hope this helps.

how to outline a font character in prawn / ruby (pdf)

I would like to draw a character (TTF) as vector and wondering how to perhaps draw the outline of the character. It appears that characters are "filled" with color suggesting the possibility to outline. I've tried stroke_color to no avail. I am using ruby and prawn to render in pdf.
Thanks.
I've been needing this feature myself, so I just added it to prawn. With the caveat that the API may change slightly after feature review from the other devs, here's how it works:
Prawn::Document.generate "rendering_mode.pdf" do |pdf|
pdf.fill_color "00ff00"
pdf.stroke_color "0000ff"
pdf.text("Inline mode", :mode => 1, :size => 40)
end
For a list of valid values to mode, check the code docs for the text_rendering_mode method.
If you want to cherry pick the changes, the specific commit that adds support is at here.

Resources