The Accidental Rubyist

invalid byte sequence in UTF-8

ncurses: friendly colors

leave a comment »

It was a lot easier than I thought. English color names such as green and red can be converted to their constant names (thanks to Sup):

 Ncurses.const_get "COLOR_#{colorstring.upcase}"

Using this simple line of code, one may maintain a hash containing color_pairs for whatever colors are in use. Whenever, a color is requested, one returns it from the hash , or creates a new one.

Thus, as may be seen in these samples, we can set or modify a widgets foreground or background color using:


color "green"
bgcolor "red"

In example 1, the password field has a red foreground and blue background set at startup. “show” means that for every character entered an asterisk will be displayed.

foreground and background color or password and radio buttons

foreground and background color or password and radio buttons


    @form.by_name["password"].show '*'
    @form.by_name["password"].color 'red'
    @form.by_name["password"].bgcolor 'blue'

Notice also that “Select a color” is cyan, and red and green radio buttons are .. red and green.

In example 2, I have checked the checkbox and selected red. The label “Select a color” has become bold and red.

change of color on selection of checkbox and radio button

change of color on selection of checkbox and radio button

In example 3, I have checked our new CheckBoxMenuItem, and doing so has made the label reverse-video.

change of attributes on selection of checkboxmenuitem

change of attributes on selection of checkboxmenuitem

Advertisements

Written by totalrecall

November 30, 2008 at 7:16 pm

Posted in ncurses, rbcurse, ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: