Learning Ruby Curses

Ruby Curses taking control of the enter key

Well it seems as though Curses.nonl just means that when ENTER is pressed, "\r" is written rather than "\r\n", so the only way I managed to solve this problem is to disable any output to the screen using Curses.raw and implement all the data writing to the screen myself.

I don't know why the constants don't much up. Perhaps that's the constant for the other ENTER key on the keyboard (in the bottom right-hand corner). Doesn't really matter though, I can just do puts Curses.getch to find out the number of the key.

How do I use (n)curses in Ruby?

You might be able to get some implementation ideas from the Ruby/ProgressBar library, which generates text progress bars. I stumbled across it a couple of months back but haven't made any use of it.

Creating a menu with curses

there is no need for threads in your example.

you can see how this can be done here: https://gist.github.com/phoet/6988038

require "curses"
include Curses

init_screen
start_color
noecho

def draw_menu(menu, active_index=nil)
4.times do |i|
menu.setpos(i + 1, 1)
menu.attrset(i == active_index ? A_STANDOUT : A_NORMAL)
menu.addstr "item_#{i}"
end
end

def draw_info(menu, text)
menu.setpos(1, 10)
menu.attrset(A_NORMAL)
menu.addstr text
end

position = 0

menu = Window.new(7,40,7,2)
menu.box('|', '-')
draw_menu(menu, position)
while ch = menu.getch
case ch
when 'w'
draw_info menu, 'move up'
position -= 1
when 's'
draw_info menu, 'move down'
position += 1
when 'x'
exit
end
position = 3 if position < 0
position = 0 if position > 3
draw_menu(menu, position)
end

Ruby curses: how to get pure white

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 can I get Ruby curses to respond properly to arrow keys?

In the line to enable the keypad, you're actually creating a local variable called 'keypad' because that method is on the class Curses::Window.
Since you're not making your own windows (apart from with init_screen), you can just refer to the standard one using the stdscr method. If I change line 8 to:

stdscr.keypad = true

then you sample code works for me.

How to properly work with Curses::KEY_BACKSPACE in Ruby

You're not doing anything wrong. The problem is the definition of KEY_BACKSPACE, vs. what the terminal actually returns... it's a historical mess, basically.

Just check for \b (or 8) instead of, or in addition to, KEY_BACKSPACE. (I'm not sure you'll ever get a returned value of KEY_BACKSPACE, in practice, but it's harmless enough to check for it.)



Related Topics



Leave a reply



Submit