Search code examples
rubycurses

Overflow-y + scroll on Ruby curses window


I've created two windows in Ruby using curses, which look something like this:

------------------------
|                      |
|       Window 1       |
|                      |
|                      |
------------------------
------------------------
|     Window 2         |
------------------------

Window 2 is static. Window 1 will have be filled with text. Currently, if the text gets to the bottom line, the next word is appended to the bottom line, rather than going on the next line. How do I make one of the following options possible:

(a) Window 1 grows is new lines are needed (b) Window one has an 'overflow-y' feature, like in css.

I've tried having Curses.stdscr.scrollok enabled, and each time I get to the bottom of the window using window1.scroll, but neither achieve what I'm looking for.


Solution

  • Looking at the C library for curses, I've figured this out. Thought I'd post the answer in case anyone else needs it!

    Overflow-y

    You need to have two things set:

    Curses.stdscr.scrollok true
    window.scrollok(true)
    

    When you get to the last line, it'll just keep appending to the current line. To solve this, I added:

    if (window.cury + 1) == window.maxy
        window.addstr("\n")
    end
    

    Scrollable window

    window.scroll() # scrolls up one line
    window.scrl(-1) # scrolls down one line