7

How can I get colors with Ruby's default Curses library?

So something like:

puts "\e[0m\e[30;47mtest\e[0m"

works great. Gives a nice black on light gray background. But this:

#!/usr/bin/env ruby

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (for pageup/down)
Curses.stdscr.nodelay = 1

Curses.clear
Curses.setpos(0,0)
Curses.addstr "Hello!"
Curses.addstr "\e[0m\e[30;47mHello!\e[0m"

Displays:

Hello!

^[[0m^[[30;47mHello!^[[0m

Ultimately, I need colors for this:

How to capture a key press in Ruby?

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)

loop do
  case Curses.getch
  when Curses::Key::PPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Up")
  when Curses::Key::NPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Dn")
  end
end
Community
  • 1
  • 1
Aleksandr Levchuk
  • 3,751
  • 4
  • 35
  • 47
  • 1
    FYI `curses` from the stdlib is deprecated, should try `ncurses` - https://github.com/eclubb/ncurses-ruby/ which has proper documentation – basicxman Sep 07 '11 at 03:11
  • 1
    @basicxman, what makes you say the stdlib's curses is deprecated? – cam Sep 07 '11 at 03:33
  • libruby `curses` indeed has no proper documentation, I was only able to find/get answers here on SO – Aleksandr Levchuk Sep 07 '11 at 03:40
  • Also, regarding documentation http://ruby-doc.org/stdlib/libdoc/curses/rdoc/ has only function names but those are descriptive enough to find what you want. Then you can Google for those names as they are mostly the same in Python libraries which are well documented ... – Aleksandr Levchuk Sep 07 '11 at 16:17
  • 4
    No, don't use ncurses. Use curses. It hasn't been deprecated at all, and if you're creating a GUI with ncurses, your users will have to download and install the ncurses gem before they can use it. Though ncurses is better, the differences are negligible and it's not worth it. Just use curses, and use ncurses documentation to get a handle on things. – Starkers Oct 14 '13 at 14:30
  • curses are deprecated now, wanted to put in a link to https://github.com/sup-heliotrope/ncursesw-ruby which is the most active version of ncurses as far as I can see (disclaimer: I am currently one of the maintainer of this one) – gauteh Mar 20 '14 at 13:14
  • 2
    Curses package is **not** deprecated. And it's ruby core library, while *ncurses* package is not. Generally *curses* and *ncurses* are same thing, since *curses* is general name (or API) and *ncurses* is particular implementation of it. Moreover, ruby's *curses* package is actually using *ncurses* system library. This is also proof that it's not deprecated. Some people just confising name of system libs with general thing, and with ruby package. – catpnosis Jun 13 '14 at 13:49

1 Answers1

10

There are examples of curses usage in the ruby source code, see, e.g. here.

Your code could be something like:

require 'curses'
include Curses

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)
Curses.start_color
# Determines the colors in the 'attron' below
Curses.init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_BLACK) 
Curses.init_pair(COLOR_RED,COLOR_RED,COLOR_BLACK)

loop do

  case Curses.getch

  when Curses::Key::PPAGE
    Curses.clear
    Curses.setpos(0,0)
    # Use colors defined color_init
    Curses.attron(color_pair(COLOR_RED)|A_NORMAL){
      Curses.addstr("Page Up")
    }
  when Curses::Key::NPAGE
    Curses.clear
    Curses.setpos(0,0)
    Curses.attron(color_pair(COLOR_BLUE)|A_NORMAL){
      Curses.addstr("Page Down")
    }
  end
end
Tatu Lahtela
  • 4,514
  • 30
  • 29