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.
Related
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.
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.
printf "\033[1;32;40mGreen text on black background.\033[0m\n"
That is the green, but how can i get light green or other variation of color?
http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html , are only those color available for gnome-terminal as escape code?
Also how can i get bigger font with ruby?
using the "1" as the first parameter, as you are doing already, that's as "light" a green as you're going to get. this guy's webpage might be helpful: http://www.linuxfocus.org/English/May2004/article335.shtml
testing on urxvt:
[added later] there is a DEC extension for double-sized characters: Printing double-size characters with Ncurses but urxvt doesn't support it, I don't know about Gnome terminal.
With an offset of 90 you can create bright/high contrast colors.
See here for a reference. Wikipedia mentions the bright color range, but doesn't really explain how to use them (if I haven't missed it).
If you combine it with the bold style you can create 4 variations of a color.
Example:
It's a matter of the terminal support. The ansi codes that you list are interpreted by the terminal emulator and those codes are the only available colours (it comes from the days before windows and when 16 colours caused a stir).
If you need more you could consider using a graphical interface to your ruby app such as tk.
You can use an RGB escape code:
printf "\033[38;2;r;g;bmText\033[0m"
or, for background colour:
printf "\033[48;2;r;g;bmText\033[0m"
Just replace r, g, and b with the desired RGB value.
I know there is a gem called Color. I installed it.
But for the life of me, I can't figure out how to use the thing.
I just want to convert a color name into its RGB values, if possible without copying the whole color table into my code.
I want to be able to convert something like red or Navy into three numeric values.
require 'color/css'
red_code = Color::CSS["red"].html
#=> "#ff0000"
Old question, but I just came across this gem in a current project and had to do the same. I needed RBG values like OP asked for and so I used the css_rbg instance method similar to how tokland produced the hex value with the html instance method.
require 'color/css'
red_code = Color::CSS["red"].css_rgb
#=> "rgb(100.00%, 0.00%, 0.00%)"
Html colours are expressed as an Hexadecimal colour, and it turns out that a RGB colour is not more than a Hexadecimal expression of the colour so:
#ff0000 = r:255 g:0 b:0
I would like to tidy up my Vim color scheme file, by replacing #ABCDEF colors
with variables. For example, I would like to replace this:
highlight String guifg=#61CE3C
highlight Identifier guifg=#61CE3C
highlight Type guifg=#84A7C1
with something like this (pseudo-code vimscript):
my_string =#61CE3C
my_type =#84A7C1
highlight String guifg=my_string
highlight Identifier guifg=my_string
highlight Type guifg=my_type
I wasn't sure whether vimscript considers the hex color to be a string, or a
hexadecimal number. Apparently it's a string, cause this seems to work:
:highlight Normal guifg='#ffffff'
So I thought I'd try this:
:let my_color='#ffffff'
:highlight Normal guifg=my_color
But this gives the error "E254: Cannot allocate color my_color". Can anyone
suggest a way to make this work?
Since :highlight doesn't accept a variable as an argument, you have to build the command to run as an expression and then evaluate it with the :execute command.
:let my_color='#ffffff'
:exe 'highlight Normal guifg=' . my_color
As jamessan has said, you need to use :exe to do this. I found the general syntax for the colour scheme files a bit difficult to manage, so I made my own, which you may be interested in. I find it a lot more maintainable, but you may still find it a bit too verbose, in which case see the alternative at the end of this answer.
Rather than writing the long :hi lines, you create a dictionary along the lines of:
" Unspecified colours default to NONE, EXCEPT cterm(.*) which default to matching gui(.*)
" ctermfg will default to 'Blue' and ctermbg to 'NONE' if gui(.*) are RGB
"
" In most cases, only GUIFG is therefore important unless support for Black and White
" terminals is essential
let ColourAssignment['Normal'] = {"GUIFG": 'White', "GUIBG": 'Black'}
let ColourAssignment['Comment'] = {"GUIFG": '#00ff00'}
As mentioned in the comment, all unspecified parts assume sensible defaults, so you don't have to do the common:
:hi Comment guifg=green ctermfg=green guibg=black ctermfg=black
repetition. You can also (of course) put variables in place of the '#00ff00' bit if you want.
It's currently designed around dark background colour schemes: for light background colour schemes, it automatically chooses an appropriate colour (it makes bright colours darker basically) unless you override it, but if you prefer light background colour schemes, it wouldn't be too hard to change so that the default is light.
The other advantage of it is that it comes with a syntax highlighting file that automatically highlights the "ColourAssignment" bit in the colour that you've selected.
Anyway, if that's any interest to you, you can get it from here.
An alternative you could use would be to create a command like this:
command! -nargs=+ Hi call CustomHighlighter(<f-args>)
function! CustomHighlighter(name, ...)
let colour_order = ['guifg', 'guibg']
let command = 'hi ' . a:name
if (len(a:000) < 1) || (len(a:000) > (len(colour_order)))
echoerr "No colour or too many colours specified"
else
for i in range(0,len(a:000)-1)
let command .= ' ' . colour_order[i] . '=' . a:000[i]
endfor
exe command
endif
endfunc
You could then use:
Hi Comment #00ff00
Hi String Yellow
Hi Normal White Black