rubyncursescurses

Creating a windowed menu in ruby ncurses


I'm trying to learn ruby and ncurses a bit more by creating a small menu in ncurses which lists different interfaces and the interface configuration for the selected interface.

Since the ncurses library I want to learn does not really have a documentation and no menu examples, I tries learning from the NCurses Programming HowTo.

The code I have atm produces the following screen when started up (foo is selected but missing), the cursor is here at 2/2 (x/y) in the subwindow:

 ┌────────────────────────────────────────┐
 │                                        │
 │ >                                      │
 │                                        │
 │   bar                                  │
 │                                        │
 └────────────────────────────────────────┘

And when selecting the other one (bar is selected but replaced with the marker), the cursor is here at 2/2 (x/y) in the subwindow:

 ┌────────────────────────────────────────┐
 │                                        │
 │   foo                                  │
 │                                        │
 │ >   >                                  │
 │                                        │
 └────────────────────────────────────────┘

The question now is: where is the error or is it a problem with the library?

Thank you in advance.

Code

#!/usr/bin/ruby

require 'ncurses'

$number_of_mgmt_devices = 2

# start ncurses
stdscr = Ncurses.initscr
Ncurses.cbreak
Ncurses.noecho
stdscr.keypad(true)

$foobar = Hash(:foo => "AAAAA", :bar => "BBBBB")

# create window and grab keyboard control
length = $number_of_mgmt_devices * 2 + 1
width = 40
devicesMenuWin = Ncurses.newwin(length+2, width + 2, 6, 1)
devicesMenuWin.keypad(true)

# create choices
choices = []
$foobar.each do |key, info| 
    choices.append(Ncurses::Menu::ITEM.new("#{key}", "#{info}"))
end

# create and configure menu
devicesMenu = Ncurses::Menu::MENU.new(choices)
devicesMenu.opts_off(Ncurses::Menu::O_SHOWDESC)
devicesMenu.opts_off(Ncurses::Menu::O_NONCYCLIC)

#associate windows with menu
devicesMenu.win = devicesMenuWin
devicesMenu.sub = devicesMenuWin.derwin(length, width, 2, 2)

devicesMenu.set_spacing(0, 2, 0)

devicesMenu.mark = " > "

Ncurses.box(devicesMenuWin, 0, 0)
devicesMenu.post
devicesMenuWin.refresh

begin
    while c = devicesMenuWin.wgetch; c != "o"
        case c
        when Ncurses::KEY_UP
            devicesMenu.driver(Ncurses::Menu::REQ_PREV_ITEM)
        when Ncurses::KEY_DOWN
            devicesMenu.driver(Ncurses::Menu::REQ_NEXT_ITEM)
        end
        devicesMenuWin.wrefresh
    end
ensure
    devicesMenu.unpost
    Ncurses.echo
    Ncurses.nocbreak
    Ncurses.nl
    Ncurses.endwin
end

Solution

  • After trying out a few different things the problem is the underlying library used for that or better the linking.

    I've tested two different systems:

    While some libraries like the fedora one link correctly against the menuw library (the one with widespace-char support), the debian one only links against the widespace-char supporting libraries panelw, formw and ncursesw but not the menuw, which is the error.

    Thank you all anyway. I'll add more information when I'll find more.