├── README ├── add1.rb ├── add2.rb ├── add3.rb ├── arrowkeys.rb ├── bgcolor1.rb ├── bgcolor2.rb ├── blah1.rb ├── change_colors.rb ├── colorme.rb ├── colortest.rb ├── corners1.rb ├── corners2.rb ├── ctitle.rb ├── flush1.rb ├── goodbye.rb ├── hamlet1.rb ├── hamlet4.rb ├── hamlet5.rb ├── hamlet6.rb ├── helpmenu1.rb ├── kbhit.rb ├── keywait1.rb ├── kid1.rb ├── kid2.rb ├── kid4.rb ├── marquee1.rb ├── marquee2.rb ├── menubar.rb ├── quad1.rb ├── quad3.rb ├── screensize.rb ├── string1.rb ├── twinkle.rb ├── twowin1.rb ├── typewriter.rb ├── whereami.rb ├── whoru.rb ├── whoru2.rb ├── window_scrolling.rb ├── window_scrolling2.rb └── yoda.rb /README: -------------------------------------------------------------------------------- 1 | These examples are based on the book Programmer's Guide to nCurses by Dan Gookin. The book by Gookin was written targeted for C, and these examples are targeted for the Ncurses wrapper library in Ruby. 2 | 3 | These are not to explain or introduce nCurses or its ruby library. I am simply sharing my journey of learning about nCurses with you by providing the examples free of charge. In my search for using nCurses I have seen a lot of questions asked and not very many answers given. Perhaps these examples will help provide some clarification. 4 | 5 | If they do provide help for you I'd love to know! 6 | 7 | zach.dennis@gmail.com -------------------------------------------------------------------------------- /add1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | "Greetings from ncurses".each_byte do |ch| 7 | window.addch ch 8 | window.refresh 9 | Ncurses.napms 100 # refreshes every 1/10th of a second 10 | end 11 | window.getch 12 | ensure 13 | Ncurses.endwin 14 | end -------------------------------------------------------------------------------- /add2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | text1 = "Oh give me a clone!\n" 8 | text2 = "Yes a clone of my own" 9 | 10 | window.addstr(text1) 11 | window.addstr(text2) 12 | window.refresh 13 | 14 | window.getch 15 | ensure 16 | Ncurses.endwin 17 | end -------------------------------------------------------------------------------- /add3.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | text1 = "Oh give me a clone!\n" 8 | text2 = "Yes a clone of my own" 9 | 10 | window.addstr(text1) 11 | window.addstr(text2) 12 | window.move(2,0) # move to third row, first column 13 | window.addstr("With the Y chromosome changed to the X.") 14 | window.refresh 15 | 16 | window.getch 17 | ensure 18 | Ncurses.endwin 19 | end -------------------------------------------------------------------------------- /arrowkeys.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # All keys on a keyboard generate some key code 8 | # and this example focuses on paying attention to 9 | # your arrow keys. Although you can also catch the following keycodes: 10 | # KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, 11 | # KEY_HOME, KEY_NPAGE, KEY_PPAGE, KEY_END, KEY_BACKSPACE 12 | # KEY_F(n) - KEY_F is a method and n is the number. ie: KEY_F(5) for pressing the F5 key 13 | 14 | 15 | # +keypad+ is used to determine what window/screen 16 | # the arrow keys will affect, pass in true to turn 17 | # it on and false to turn it off. Without 'true' this 18 | # would display crazy character codes on the screen when hitting 19 | # arrow keys. 20 | Ncurses.keypad(window, true) 21 | window.addstr("Type 'q' to quit. In the meantime hit your arrow keys\n") 22 | while( (ch=window.getch) != 'q'[0]) 23 | case ch 24 | when Ncurses::KEY_DOWN 25 | window.addstr("down ") 26 | when Ncurses::KEY_UP 27 | window.addstr("up ") 28 | when Ncurses::KEY_RIGHT 29 | window.addstr("right ") 30 | when Ncurses::KEY_LEFT 31 | window.addstr("left ") 32 | end 33 | window.refresh 34 | end 35 | 36 | ensure 37 | Ncurses.endwin 38 | end -------------------------------------------------------------------------------- /bgcolor1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | Ncurses.start_color 7 | Ncurses.init_pair(1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE) 8 | 9 | # +bkgd+ changes the color scheme for the whole screen/window. 10 | # It affects all attributes and unused character places on the 11 | # screen. any text you've written stays as on the screen as 12 | # text, only the attributes are changed. 13 | window.bkgd(Ncurses.COLOR_PAIR(1)) 14 | 15 | window.addstr("So this is what a color screen looks like?\n") 16 | 17 | window.refresh 18 | window.getch 19 | 20 | # +bkgd+ can be used to fill each character space on the screen 21 | # with a given character. 22 | window.bkgd(Ncurses.COLOR_PAIR(1) | '-'[0]) 23 | 24 | window.refresh 25 | window.getch 26 | 27 | ensure 28 | Ncurses.endwin 29 | end -------------------------------------------------------------------------------- /bgcolor2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | Ncurses.start_color 7 | 8 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 9 | Ncurses.init_pair 2, Ncurses::COLOR_GREEN, Ncurses::COLOR_WHITE 10 | Ncurses.init_pair 3, Ncurses::COLOR_RED, Ncurses::COLOR_GREEN 11 | 12 | window.bkgd Ncurses.COLOR_PAIR(1) 13 | window.addstr "I think that I shall never see\n" 14 | window.addstr "a color screen as pretty as thee.\n" 15 | window.addstr "For seasons may change\n" 16 | window.addstr "and storms may thunder\n" 17 | window.addstr "But color text shall always wonder." 18 | window.refresh 19 | window.getch 20 | 21 | window.bkgd Ncurses.COLOR_PAIR(2) 22 | window.refresh 23 | window.getch 24 | 25 | window.bkgd Ncurses.COLOR_PAIR(3) 26 | window.refresh 27 | window.getch 28 | ensure 29 | Ncurses.endwin 30 | end -------------------------------------------------------------------------------- /blah1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | rows, cols = [], [] 8 | window.getmaxyx(rows, cols) 9 | rows, cols = rows.first, cols.first 10 | 11 | populate_screen = lambda do 12 | text = "blah " 13 | col_max = (rows * cols) / (text.length - 1) 14 | (col_max - cols).times do 15 | window.addstr text 16 | end 17 | window.refresh 18 | window.getch 19 | end 20 | 21 | populate_screen.call 22 | # clears the entire screen, this moves the cursor back to 0,0 23 | window.clear 24 | window.refresh 25 | window.getch 26 | 27 | # erase the entire screen, same as clear, but clear does a better job of tidying up 28 | populate_screen.call 29 | window.erase 30 | window.refresh 31 | window.getch 32 | 33 | populate_screen.call 34 | window.move(rows/2,0) 35 | # +clrtoeol+ clears the screen from the current cursor position to the end of the line 36 | window.clrtoeol 37 | window.refresh 38 | window.getch 39 | 40 | populate_screen.call 41 | window.move(rows/2,0) 42 | # +clrtobot+ clears the screen from the current cursor position to the end of screen 43 | window.clrtobot 44 | window.refresh 45 | window.getch 46 | 47 | ensure 48 | Ncurses.endwin 49 | end -------------------------------------------------------------------------------- /change_colors.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ncurses.start_color 8 | changeable = Ncurses.can_change_color? 9 | if changeable 10 | window.addstr "you can change colors!" 11 | else 12 | window.addstr "you can't change colors!" 13 | end 14 | window.refresh 15 | 16 | window.getch 17 | rescue 18 | Ncurses.endwin 19 | end -------------------------------------------------------------------------------- /colorme.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ncurses.start_color 8 | 9 | # +init_pair+ assigns a pair of foreground/background colors 10 | # to a number. That number can be used later to set the terminal 11 | # colors to that pair using the Ncurses.COLOR_PAIR method. 12 | 13 | Ncurses.init_pair(1, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED) 14 | Ncurses.init_pair(2, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK) 15 | 16 | window.attrset(Ncurses.COLOR_PAIR(1)) 17 | window.addstr("My name is Mr. Black!\n") 18 | 19 | window.attrset(Ncurses.COLOR_PAIR(2)) 20 | window.addstr("My name is Mr. Blue!\n") 21 | 22 | window.attrset(Ncurses.COLOR_PAIR(1)) 23 | window.addstr("How do you do?\n") 24 | 25 | window.attrset(Ncurses.COLOR_PAIR(2)) 26 | window.addstr("How do I do ") 27 | window.attron(Ncurses::A_BOLD) 28 | window.addstr("what") 29 | window.attroff(Ncurses::A_BOLD) 30 | window.addch("?"[0]) 31 | window.refresh 32 | 33 | window.getch 34 | ensure 35 | Ncurses.endwin 36 | end -------------------------------------------------------------------------------- /colortest.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.instance_eval do 8 | addstr("Does this terminal support colors? ") 9 | if Ncurses.has_colors? 10 | addstr("yes!\n") 11 | # Ncurses.COLORS() will return 0 until Ncurses is told to start using colors 12 | printw("Ncurses reports that you have %d colors you can use\n", Ncurses.COLORS()) 13 | # we want to use colors! 14 | Ncurses.start_color 15 | printw("colors have been properly initialized.\n") 16 | printw("Congratulations!") 17 | 18 | # COLORS is a method on NCurses to report the number of colors the terminal 19 | printw("Ncurses reports that you have %d colors you can use\n", Ncurses.COLORS()) 20 | 21 | # COLOR_PAIRS is a method on NCurses to report the number of bg/fg color 22 | # combinations there are for the terminal 23 | printw("Ncurses reports that you have %d colors you can use\n", Ncurses.COLOR_PAIRS()) 24 | 25 | else 26 | addstr("no!\n") 27 | end 28 | 29 | refresh 30 | getch 31 | end 32 | ensure 33 | Ncurses.endwin 34 | end -------------------------------------------------------------------------------- /corners1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | lines = [] 8 | cols = [] 9 | window.getmaxyx(lines, cols) 10 | rows = lines.first - 1 # 0-based 11 | cols = cols.first - 1 # 0-based 12 | 13 | window.move(0,0) 14 | window.addch('*'[0]) 15 | window.refresh 16 | Ncurses.napms(500) # pause half a second 17 | 18 | window.move(0, cols) 19 | window.addch('*'[0]) 20 | window.refresh 21 | Ncurses.napms(500) # pause half a second 22 | 23 | window.move(rows, 0) 24 | window.addch('*'[0]) 25 | window.refresh 26 | Ncurses.napms(500) # pause half a second 27 | 28 | window.move(rows, cols) 29 | window.addch('*'[0]) 30 | window.refresh 31 | Ncurses.napms(500) # pause half a second 32 | 33 | window.move(rows/2, cols/2) 34 | window.refresh 35 | 36 | window.getch 37 | ensure 38 | Ncurses.endwin 39 | end -------------------------------------------------------------------------------- /corners2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | lines, cols = [], [] 8 | window.getmaxyx(lines, cols) 9 | rows = lines.first - 1 10 | cols = cols.first - 1 11 | 12 | window.mvaddch 0, 0, '*'[0] 13 | window.refresh 14 | Ncurses.napms(500) 15 | 16 | window.mvaddch 0, cols, '*'[0] 17 | window.refresh 18 | Ncurses.napms(500) 19 | 20 | window.mvaddch rows, 0, '*'[0] 21 | window.refresh 22 | Ncurses.napms(500) 23 | 24 | window.mvaddch rows, cols, '*'[0] 25 | window.refresh 26 | Ncurses.napms(500) 27 | 28 | window.move rows/2, cols/2 29 | window.refresh 30 | 31 | window.getch 32 | ensure 33 | Ncurses.endwin 34 | end -------------------------------------------------------------------------------- /ctitle.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | class MyScreen 4 | def initialize(ncurses_screen, &blk) 5 | @screen = ncurses_screen 6 | instance_eval &blk if block_given? 7 | end 8 | 9 | def center(row, text) 10 | start_col = ((cols) - text.size) / 2 11 | @screen.mvaddstr row, start_col, text 12 | end 13 | 14 | def right_justify row, text 15 | @screen.mvprintw row, 0, "%#{cols}s", text 16 | end 17 | 18 | def left_justify row, text 19 | @screen.mvprintw row, 0, "%-#{cols}s", text 20 | end 21 | 22 | private 23 | 24 | def rows_and_cols 25 | rows, cols = [], [] 26 | @screen.getmaxyx(rows, cols) 27 | [rows.first, cols.first] 28 | end 29 | 30 | def cols 31 | rows_and_cols.last 32 | end 33 | end 34 | 35 | 36 | begin 37 | window = Ncurses.initscr 38 | Ncurses.cbreak 39 | 40 | MyScreen.new window do 41 | center 1, "Penguin Soccer Finals" 42 | center 5, "Cattle DungSamples from Temecula" 43 | center 7, "Catatonic Theater" 44 | center 9, "Why Do Ions Hate Each Other?" 45 | right_justify 11, "this is right justified" 46 | left_justify 13, "this is left justified" 47 | end 48 | 49 | 50 | window.getch 51 | ensure 52 | Ncurses.endwin 53 | end -------------------------------------------------------------------------------- /flush1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.addstr "Type on the keyboard whilst I wait...\n" 8 | window.refresh 9 | Ncurses.napms 1500 10 | 11 | window.addstr "Here is what you typed: \n " 12 | # +getnstr+ reads from the input buffer and puts it back out on the screen. While 13 | # the program was thinking during the napms call the keystrokes you typed 14 | # were pushed onto an input buffer (even though you couldn't see them until 15 | # getnstr is called 16 | str = "" 17 | window.getnstr str, 80 18 | window.refresh 19 | 20 | window.getch 21 | window.addstr "Type on the keyboard whilst I wait...\n" 22 | window.refresh 23 | Ncurses.napms 1500 24 | 25 | window.addstr "Here is what you typed: \n " 26 | # +flushinp+ flushes the input buffer, so no matter what you typed while 27 | # the program was thinking during the napms call it's gone now! 28 | Ncurses.flushinp 29 | str = "" 30 | window.getnstr str, 80 31 | window.refresh 32 | ensure 33 | Ncurses.endwin 34 | end -------------------------------------------------------------------------------- /goodbye.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | # initialize ncurses 5 | window = Ncurses.initscr 6 | Ncurses.cbreak # provide unbuffered input 7 | 8 | # same as Ncurses.stdscr.addstr 9 | window.addstr("Press a key to continue") # output string 10 | 11 | # update the screen with the string 12 | # same as Ncurses.stdscr.refresh 13 | window.refresh 14 | 15 | # +getch+ is used to pause the screen because terminals support a feature 16 | # known as +rmcup+ which restores the terminal screen to what is looked like 17 | # before the program was run immediately after it exits. 18 | # same as Ncurses.stdscr.getch 19 | window.getch # get a charachter 20 | 21 | ensure 22 | # restores the terminal back to what it was before ncurses started 23 | # Must do this, otherwise unpredicted results in ncurses 24 | Ncurses.endwin 25 | end -------------------------------------------------------------------------------- /hamlet1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # +insch+ inserts a single character 8 | # +insertline+ inserts a blank line of text (must move cursor to the line you want to insert the line on) 9 | # +delch+ deletes a character of text 10 | # +deleteln+ deletes an entire line of text 11 | 12 | Ham1 = "To be, or not to be: that is the question:\n" 13 | Ham2 = "Whether 'tis nobler in the mind to suffer\n" 14 | Ham3 = "The slings and arrows of outrageous fortune,\n" 15 | Ham4 = "Or to take arms against a sea of troubles,\n" 16 | Ham5 = "And by opposing end them?\n" 17 | 18 | window.addstr Ham1 19 | window.addstr Ham3 20 | window.addstr Ham5 21 | window.refresh 22 | window.getch 23 | 24 | window.move(1,0) 25 | window.insertln 26 | window.addstr Ham2 27 | window.refresh 28 | window.getch 29 | 30 | window.move(3,0) 31 | window.insertln 32 | window.addstr Ham4 33 | window.refresh 34 | window.getch 35 | ensure 36 | Ncurses.endwin 37 | end -------------------------------------------------------------------------------- /hamlet4.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ham1 = "To be, or not to be: that is the question:\n" 8 | Ham2 = "Whether 'tis nobler in the mind to suffer\n" 9 | Ham3 = "The slings and arrows of outrageous fortune,\n" 10 | Ham4 = "Or to take arms against a sea of troubles,\n" 11 | Ham5 = "And by opposing end them?\n" 12 | 13 | window.addstr Ham1 14 | window.addstr Ham2 15 | window.addstr Ham3 16 | window.addstr Ham4 17 | window.addstr Ham5 18 | window.refresh 19 | window.getch 20 | 21 | window.move(1,0) 22 | # +deleteln+ deletes a full line of of text and backscrolls. It does not 23 | # affect the position of the cursor, after this call the cursor will still be 24 | # at 1, 0 25 | window.deleteln 26 | window.refresh 27 | window.getch 28 | 29 | ensure 30 | Ncurses.endwin 31 | end -------------------------------------------------------------------------------- /hamlet5.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ham1 = "To be, or not to be: that is the question:\n" 8 | Ham2 = "Whether 'tis nobler in the mind to suffer\n" 9 | Ham3 = "The slings and arrows of outrageous fortune,\n" 10 | Ham4 = "Or to take arms against a sea of troubles,\n" 11 | Ham5 = "And by opposing end them?\n" 12 | 13 | window.addstr Ham1 14 | window.addstr Ham2 15 | window.addstr Ham3 16 | window.addstr Ham4 17 | window.addstr Ham5 18 | window.refresh 19 | window.getch 20 | 21 | window.move 2, 25 22 | 11.times { window.delch ; window.refresh; Ncurses.napms 100 } 23 | window.refresh 24 | window.getch 25 | 26 | ensure 27 | Ncurses.endwin 28 | end -------------------------------------------------------------------------------- /hamlet6.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ham1 = "To be, or not to be: that is the question:\n" 8 | Ham2 = "Whether 'tis nobler in the mind to suffer\n" 9 | Ham3 = "The slings and arrows of outrageous fortune,\n" 10 | Ham4 = "Or to take arms against a sea of troubles,\n" 11 | Ham5 = "And by opposing end them?\n" 12 | 13 | window.addstr Ham1 14 | window.addstr Ham2 15 | window.addstr Ham3 16 | window.addstr Ham4 17 | window.addstr Ham5 18 | window.refresh 19 | window.getch 20 | 21 | window.move 2, 25 22 | 11.times { window.delch ; window.refresh; Ncurses.napms 100 } 23 | "obnoxious ".reverse.each_byte { |ch| window.insch(ch) ; window.refresh ; Ncurses.napms 100 } 24 | 25 | window.refresh 26 | window.getch 27 | 28 | ensure 29 | Ncurses.endwin 30 | end -------------------------------------------------------------------------------- /helpmenu1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | class HelpMenu 4 | def initialize(ncurses_screen) 5 | @screen = ncurses_screen 6 | @help_screen = Ncurses.newwin(0,0,0,0) 7 | Ncurses.mvwaddstr(@help_screen, 6, 32, "Help menu Screen") 8 | Ncurses.mvwaddstr(@help_screen, 9, 28, "Press the ~ key to quit") 9 | Ncurses.mvwaddstr(@help_screen, 12, 28, "Press ENTER to go back") 10 | end 11 | 12 | def show_help 13 | @help_screen.refresh 14 | @screen.getch 15 | 16 | # Calling @screen.refresh won't actually update the screen because 17 | # Ncurses will only update text that has changed, we can use +touchwin+ 18 | # tell Ncurses that every character has changed and needs to be redrawn. 19 | # If you comment out @screen.touchwin and uncomment @screen.refresh you'll 20 | # see the program no longer work! 21 | # @screen.refresh 22 | @screen.touchwin 23 | end 24 | end 25 | 26 | begin 27 | window = Ncurses.initscr 28 | Ncurses.cbreak 29 | 30 | help_menu = HelpMenu.new window 31 | 32 | window.addstr("Typer Program\n") 33 | window.addstr("Press + for help:\n\n") 34 | window.refresh 35 | Ncurses.noecho 36 | while ch=window.getch 37 | window.refresh 38 | if ch == '+'[0] 39 | help_menu.show_help 40 | else 41 | window.addch ch 42 | end 43 | end 44 | 45 | ensure 46 | Ncurses.endwin 47 | end -------------------------------------------------------------------------------- /kbhit.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | kbhit = lambda do 8 | Ncurses.nodelay(window, true) 9 | Ncurses.noecho 10 | ch = window.getch 11 | if ch == Ncurses::ERR 12 | result = false 13 | else 14 | result = true 15 | Ncurses.ungetch ch 16 | end 17 | 18 | Ncurses.echo 19 | Ncurses.nodelay(window, false) 20 | result 21 | end 22 | 23 | window.addstr("Press any key to end this program:") 24 | while !kbhit.call 25 | end 26 | ensure 27 | Ncurses.endwin 28 | end -------------------------------------------------------------------------------- /keywait1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.addstr "Press any key to begin:\n" 8 | window.refresh 9 | window.getch 10 | 11 | # +nodelay+ makes the getch method non-blocking, whereas typically it is 12 | # blocking. To return it to its normal blocking just do Ncurses.nodelay(window, false) 13 | Ncurses.nodelay(window, true) 14 | window.addstr "Press any key to stop the insane loop\n" 15 | value = 0 16 | while window.getch == Ncurses::ERR 17 | window.printw("%d", value+=1) 18 | Ncurses.napms 100 19 | window.refresh 20 | end 21 | ensure 22 | Ncurses.endwin 23 | end -------------------------------------------------------------------------------- /kid1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | Ncurses.start_color 8 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 9 | Ncurses.init_pair 2, Ncurses::COLOR_RED, Ncurses::COLOR_YELLOW 10 | 11 | # create a sub-window, same arguments as newwin 12 | # This can be called as Ncurses.subwin, but then it takes a window 13 | # as the explicit first argument, ie: Ncurses.subwin window, 5, 20, 10, 30 14 | # Coordinates are relative to the screen. 15 | subwindow = window.subwin(5, 20, 10, 30) 16 | 17 | window.bkgd Ncurses.COLOR_PAIR(1) 18 | window.addstr "Hello son." 19 | 20 | subwindow.bkgd Ncurses.COLOR_PAIR(2) 21 | subwindow.addstr "Hello dad." 22 | 23 | window.refresh 24 | window.getch 25 | ensure 26 | Ncurses.endwin 27 | end -------------------------------------------------------------------------------- /kid2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | Ncurses.initscr 5 | Ncurses.stdscr.refresh 6 | Ncurses.cbreak 7 | 8 | Ncurses.start_color 9 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 10 | Ncurses.init_pair 2, Ncurses::COLOR_RED, Ncurses::COLOR_YELLOW 11 | 12 | # create a sub-window using derwin, same arguments as newwin 13 | # This can be called as Ncurses.derwin, but then it takes a window 14 | # as the explicit first argument, ie: Ncurses.derwin window, 5, 20, 10, 30 15 | window = Ncurses.newwin 100, 100, 10, 10 16 | 17 | subwindow = window.derwin(5, 20, 10, 30) 18 | 19 | window.bkgd Ncurses.COLOR_PAIR(1) 20 | window.addstr "Hello son." 21 | 22 | subwindow.bkgd Ncurses.COLOR_PAIR(2) 23 | subwindow.addstr "Hello dad." 24 | 25 | window.refresh 26 | window.getch 27 | ensure 28 | Ncurses.endwin 29 | end -------------------------------------------------------------------------------- /kid4.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | Ncurses.initscr 5 | Ncurses.stdscr.refresh 6 | Ncurses.cbreak 7 | 8 | Ncurses.start_color 9 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 10 | Ncurses.init_pair 2, Ncurses::COLOR_RED, Ncurses::COLOR_YELLOW 11 | 12 | window = Ncurses.newwin 100, 100, 10, 10 13 | subwindow = window.derwin(5, 20, 10, 30) 14 | window.bkgd Ncurses.COLOR_PAIR(1) 15 | window.addstr "Hello son." 16 | subwindow.bkgd Ncurses.COLOR_PAIR(2) 17 | subwindow.addstr "Hello dad. Hit enter to delete me." 18 | window.getch 19 | 20 | # delete the sub-window 21 | subwindow.delwin 22 | window.clear 23 | window.addstr "the sub-window has been removed" 24 | window.refresh 25 | window.getch 26 | ensure 27 | Ncurses.endwin 28 | end -------------------------------------------------------------------------------- /marquee1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # +insch+ will eventually push text off the screen. 8 | # It doesn't wrap text onto the next line. 9 | 10 | text = "Stock Market Swells! DOW tops 15,000" 11 | text.reverse.each_byte do |ch| 12 | window.move 5, 5 13 | window.insch ch 14 | window.refresh 15 | Ncurses.napms 100 16 | end 17 | window.insch ' '[0] 18 | Ncurses.getch 19 | ensure 20 | Ncurses.endwin 21 | end -------------------------------------------------------------------------------- /marquee2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | class MyScreen 4 | def initialize(ncurses_screen, &blk) 5 | @screen = ncurses_screen 6 | instance_eval &blk if block_given? 7 | end 8 | 9 | def fill 10 | start = 0 11 | x = rows 12 | y = cols 13 | while start < y 14 | @screen.addstr("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n") 15 | start +=1 16 | end 17 | end 18 | 19 | private 20 | 21 | def rows_and_cols 22 | rows, cols = [], [] 23 | @screen.getmaxyx rows, cols 24 | [rows.first, cols.first] 25 | end 26 | 27 | def rows 28 | rows_and_cols.first 29 | end 30 | 31 | def cols 32 | rows_and_cols.last 33 | end 34 | end 35 | 36 | begin 37 | window = Ncurses.initscr 38 | Ncurses.cbreak 39 | 40 | window.insch ' '[0] 41 | screen = MyScreen.new window do 42 | fill 43 | window.refresh 44 | text = "Stock Market Swells! DOW tops 15,000" 45 | text.reverse.each_byte do |ch| 46 | window.move 5, 5 47 | window.insch ch 48 | window.refresh 49 | Ncurses.napms 100 50 | end 51 | end 52 | 53 | Ncurses.getch 54 | ensure 55 | Ncurses.endwin 56 | end -------------------------------------------------------------------------------- /menubar.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | class MyScreen 4 | def initialize(ncurses_screen, *menu_items) 5 | @screen = ncurses_screen 6 | @menu_items = menu_items 7 | end 8 | 9 | def draw_menu(select_item) 10 | @screen.clear 11 | @screen.addstr "Main Menu" 12 | @menu_items.each_with_index do |label, i| 13 | if label == select_item 14 | @selected_item = label 15 | @screen.attron(Ncurses::A_REVERSE) 16 | end 17 | @screen.mvaddstr(i+2, 5, label) 18 | @screen.attroff(Ncurses::A_REVERSE) 19 | end 20 | @screen.mvaddstr(@menu_items.size*2+2, 5, "Press the up/down arrow keys to change selection") 21 | @screen.refresh 22 | end 23 | 24 | def select_previous 25 | index2select = @menu_items.index(@selected_item) - 1 26 | index2select = @menu_items.size - 1 if index2select < 0 27 | draw_menu @menu_items[index2select] 28 | end 29 | 30 | def select_next 31 | index2select = @menu_items.index(@selected_item) + 1 32 | index2select = 0 if index2select >= @menu_items.size 33 | draw_menu @menu_items[index2select] 34 | end 35 | end 36 | 37 | begin 38 | window = Ncurses.initscr 39 | Ncurses.cbreak 40 | Ncurses.keypad(window, true) 41 | 42 | screen = MyScreen.new(window, 43 | "Answer E-Mail", 44 | "Off to the web", 45 | "Word processing", 46 | "Financial management", 47 | "Shutdown") 48 | screen.draw_menu("Answer E-Mail") 49 | 50 | while ch=window.getch 51 | case ch 52 | when Ncurses::KEY_UP 53 | screen.select_previous 54 | when Ncurses::KEY_DOWN 55 | screen.select_next 56 | end 57 | end 58 | ensure 59 | Ncurses.endwin 60 | end -------------------------------------------------------------------------------- /quad1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | Ncurses.initscr 5 | Ncurses.cbreak 6 | Ncurses.start_color 7 | 8 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 9 | Ncurses.init_pair 2, Ncurses::COLOR_GREEN, Ncurses::COLOR_WHITE 10 | Ncurses.init_pair 3, Ncurses::COLOR_RED, Ncurses::COLOR_GREEN 11 | Ncurses.init_pair 4, Ncurses::COLOR_BLACK, Ncurses::COLOR_YELLOW 12 | 13 | # calculate window sizes and locations 14 | rows, cols = [], [] 15 | Ncurses.getmaxyx Ncurses.stdscr, rows, cols 16 | maxx = cols.first 17 | maxy = rows.first 18 | halfx = maxx / 2 19 | halfy = maxy / 2 20 | Ncurses.refresh 21 | 22 | # create 4 windows to take up the screen 23 | window1 = Ncurses.newwin halfy, halfx, 0, 0 24 | window1.bkgd Ncurses.COLOR_PAIR(1) 25 | window2 = Ncurses.newwin halfy, halfx, 0, halfx 26 | window2.bkgd Ncurses.COLOR_PAIR(2) 27 | window3 = Ncurses.newwin halfy, halfx, halfy, 0 28 | window3.bkgd Ncurses.COLOR_PAIR(3) 29 | window4 = Ncurses.newwin halfy, halfx, halfy, halfx 30 | window4.bkgd Ncurses.COLOR_PAIR(4) 31 | if !window1 32 | Ncurses.addstr("Unable to allocate memory") 33 | Ncurses.refresh 34 | end 35 | 36 | # write to each window 37 | Ncurses.mvwaddstr window1, 0, 0, "This is window A\n" 38 | window1.refresh 39 | Ncurses.mvwaddstr window2, 0, 0, "This is window B\n" 40 | window2.refresh 41 | window3.mvaddstr 0, 0, "This is window C\n" 42 | window3.refresh 43 | window4.mvaddstr 0, 0, "This is window D\n" 44 | window4.refresh 45 | Ncurses.getch 46 | 47 | # let's add a new window to the middle of the screen 48 | window5 = Ncurses.newwin(halfy, halfx, halfy/2, halfx/2) 49 | window5.mvaddstr 0, 0, "This is the middle window! Press enter to delete it!" 50 | window5.refresh 51 | Ncurses.getch 52 | 53 | # now's let's delete the middle window ( 54 | window5.delwin 55 | # refresh the other windows so it dissappears 56 | windows = [window1, window2, window3, window4] 57 | windows.each_with_index do |window, i| 58 | window.touchwin 59 | window.refresh 60 | end 61 | 62 | # Let's do something crazy with the 4 windows 63 | Thread.new do 64 | loop do 65 | Ncurses.stdscr.touchwin 66 | Ncurses.stdscr.refresh 67 | windows.reverse.each_with_index do |window, i| 68 | window.addstr(".") 69 | window.touchwin 70 | window.refresh 71 | Ncurses.napms 100 72 | end 73 | end 74 | end 75 | 76 | Ncurses.getch 77 | ensure 78 | Ncurses.endwin 79 | end -------------------------------------------------------------------------------- /quad3.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | Ncurses.initscr 5 | Ncurses.cbreak 6 | Ncurses.start_color 7 | 8 | Ncurses.init_pair 1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE 9 | Ncurses.init_pair 2, Ncurses::COLOR_WHITE, Ncurses::COLOR_GREEN 10 | Ncurses.init_pair 3, Ncurses::COLOR_WHITE, Ncurses::COLOR_YELLOW 11 | Ncurses.init_pair 4, Ncurses::COLOR_WHITE, Ncurses::COLOR_RED 12 | 13 | cols, rows = [], [] 14 | Ncurses.stdscr.getmaxyx rows, cols 15 | cols, rows = cols.first, rows.first 16 | maxx, maxy, halfx, halfy = cols, rows, cols/2, rows/2 17 | Ncurses.refresh 18 | 19 | windows = [ 20 | Ncurses.newwin(halfy, halfx, 0, 0), 21 | Ncurses.newwin(halfy, halfx, 0, halfx), 22 | Ncurses.newwin(halfy, halfx, halfy, 0), 23 | Ncurses.newwin(halfy, halfx, halfy, halfx) 24 | ].each_with_index do |w, i| 25 | w.bkgd Ncurses.COLOR_PAIR(i+1) 26 | w.addstr "This is window #{i+1}\n" 27 | w.refresh 28 | end 29 | 30 | loop do 31 | ch = windows.first.getch 32 | windows[1..-1].each { |window| window.addch ch ; window.refresh } 33 | end 34 | 35 | Ncurses.getch 36 | ensure 37 | Ncurses.endwin 38 | end -------------------------------------------------------------------------------- /screensize.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | lines = [] 8 | columns = [] 9 | window.getmaxyx(lines, columns) 10 | window.printw("Your window has %d rows and %d columns\n", lines.first, columns.last) 11 | window.printw("Your window has %d rows and %d columns\n", Ncurses.LINES(), Ncurses.COLS()) 12 | 13 | window.refresh 14 | window.getch 15 | 16 | ensure 17 | Ncurses.endwin 18 | end -------------------------------------------------------------------------------- /string1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.addstr("What is your first name? ") 8 | window.refresh 9 | 10 | # +getstr+ requires that you pass in a string to store 11 | # the result into. getstr will simply push the result 12 | # onto the string, so if you pass in "" and type in "foo" 13 | # you'll get "foo". If you pass in "bar" and type in "foo" 14 | # you'll get "foobar" 15 | first_name = "" 16 | window.getstr first_name 17 | 18 | window.addstr("What is your middle initial? ") 19 | window.refresh 20 | # +getnstr+ is like +getstr+ but it takes an additinal 21 | # numeric argument to tell it who many characters 22 | # to allow. ncurses won't let the user enter 23 | # more than the number of characters allowed. 24 | middle_name = "" 25 | window.getnstr middle_name, 1 26 | 27 | window.addstr("What is your last name? ") 28 | window.refresh 29 | last_name = "" 30 | window.getstr last_name 31 | window.printw("Pleased to meet you, %s %s %s!", first_name, middle_name, last_name) 32 | window.refresh 33 | 34 | window.getch 35 | ensure 36 | Ncurses.endwin 37 | end -------------------------------------------------------------------------------- /twinkle.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # +attron+ turns an attribute on whereas +attrset+ 8 | # will clear any previously turned on attributes 9 | window.attron(Ncurses::A_BOLD) 10 | window.addstr("Twinkle, twinkle little star\n") 11 | window.attron(Ncurses::A_REVERSE) 12 | window.addstr("How I wonder what you are.\n") 13 | window.attroff(Ncurses::A_BOLD) 14 | window.addstr("Up above the world so high.\n") 15 | window.addstr("Like a diamond in the sky.\n") 16 | window.attrset(Ncurses::A_NORMAL) 17 | window.addstr("Twinkle, twinkle little star\n") 18 | window.attron(Ncurses::A_UNDERLINE) 19 | window.addstr("How I wonder what you are.\n") 20 | 21 | # you can turn on multiple attributes by OR'ing them 22 | window.attron(Ncurses::A_BOLD | Ncurses::A_REVERSE | Ncurses::A_UNDERLINE) 23 | window.addstr("The end!\n") 24 | 25 | # you can turn off multiple attributes by OR'ing them 26 | window.attroff(Ncurses::A_BOLD | Ncurses::A_REVERSE | Ncurses::A_UNDERLINE) 27 | window.addstr("No really, the end!\n") 28 | 29 | window.refresh 30 | window.getch 31 | ensure 32 | Ncurses.endwin 33 | end -------------------------------------------------------------------------------- /twowin1.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window1 = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window1.instance_eval do 8 | addstr("This is the original window, Ncurses.stdscr\n") 9 | refresh 10 | getch 11 | end 12 | 13 | # +newwin+ constructs a new window, passing in all zeroes creates 14 | # a window that is the same size at Ncurses.stdscr (ie: the terminal size) 15 | # +newwin+ takes the arguments: rows, cols, y, x 16 | window2 = Ncurses.newwin(20,20,5,10) 17 | # window2 = Ncurses.newwin(0, 0, 0, 0) creates a new window the same size window 18 | # as stdscr which is equal to window1 in this example 19 | Ncurses.waddstr(window2, "This is the new window created!\n") 20 | 21 | # unless window2 is refreshed no text will be displayed for that window 22 | # window2.refresh is the same as calling Ncurses.wrefresh(window2) 23 | window2.refresh 24 | window1.getch 25 | ensure 26 | Ncurses.endwin 27 | end -------------------------------------------------------------------------------- /typewriter.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.addstr("Add a few lines of text:") 8 | window.addstr("Press ~ to quit\n") 9 | window.refresh 10 | 11 | while((ch=window.getch) != "~"[0]) 12 | end 13 | 14 | window.getch 15 | ensure 16 | Ncurses.endwin 17 | end -------------------------------------------------------------------------------- /whereami.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # this example doesn't let you hit return to go to a new line 8 | window.addstr "Type some text; '~' to end:\n" 9 | window.refresh 10 | while((ch=window.getch)!= '~'[0]) 11 | end 12 | 13 | # +getyx+ gets the position of the cursor on the screen 14 | row = [] 15 | col = [] 16 | window.getyx(row, col) 17 | window.printw("\n\nThe cursor was at row %d and column %d.\n", row.first, col.first) 18 | window.refresh 19 | 20 | window.getch 21 | ensure 22 | Ncurses.endwin 23 | end -------------------------------------------------------------------------------- /whoru.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # This example asks for your name and password, but sadly 8 | # displays the password you type on the screen. =( 9 | # Se whoru2.rb example. 10 | 11 | name, password = "", "" 12 | window.mvprintw(3, 10, "Enter your name: ") 13 | window.refresh 14 | window.getnstr(name, 45) 15 | window.mvprintw(5, 10, "Enter your password: ") 16 | window.refresh 17 | window.getnstr(password, 8) 18 | ensure 19 | Ncurses.endwin 20 | end -------------------------------------------------------------------------------- /whoru2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | # This example asks for your name and password, and 8 | # correctly hides the password 9 | 10 | name, password = "", "" 11 | window.mvprintw(3, 10, "Enter your name: ") 12 | window.refresh 13 | window.getnstr(name, 45) 14 | window.mvprintw(5, 10, "Enter your password: ") 15 | window.refresh 16 | Ncurses.noecho 17 | window.getnstr(password, 8) 18 | Ncurses.echo 19 | 20 | window.mvprintw(7, 10, "Your name is %s", name) 21 | window.mvprintw(8, 10, "Your password is %s", password) 22 | window.refresh 23 | window.getch 24 | ensure 25 | Ncurses.endwin 26 | end -------------------------------------------------------------------------------- /window_scrolling.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | rows, cols = [], [] 8 | window.getmaxyx rows, cols 9 | rows, cols = rows.first, cols.first 10 | 11 | window.addstr "Hit enter to see text being added with scrolling disabled" 12 | window.refresh 13 | window.getch 14 | 15 | text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." 16 | 17 | window.scrollok(false) # this is the default setting 18 | text.split(/ /).each do |word| 19 | window.addstr "#{word}\n" 20 | window.refresh 21 | Ncurses.napms 25 22 | end 23 | window.clear 24 | 25 | window.addstr "Hit enter to see text being added with scrolling enabled" 26 | window.refresh 27 | window.getch 28 | 29 | window.scrollok(true) 30 | text.split(/ /).each do |word| 31 | window.addstr "#{word}\n" 32 | window.refresh 33 | Ncurses.napms 25 34 | end 35 | window.clear 36 | 37 | window.addstr "Hit enter to see text being added with scrolling disabled in a new window" 38 | window.refresh 39 | window.getch 40 | window2 = Ncurses.newwin(20, 20, 20, 20) 41 | text.split(/ /).each do |word| 42 | window2.addstr "#{word}\n" 43 | window2.refresh 44 | Ncurses.napms 25 45 | end 46 | 47 | 48 | window.addstr "Hit enter to see text being added with scrolling enabled in a new window" 49 | window.refresh 50 | window.getch 51 | window2.scrollok true 52 | text.split(/ /).each do |word| 53 | window2.addstr "#{word}\n" 54 | window2.refresh 55 | Ncurses.napms 25 56 | end 57 | 58 | ensure 59 | Ncurses.endwin 60 | end -------------------------------------------------------------------------------- /window_scrolling2.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | rows, cols = [], [] 8 | window.getmaxyx rows, cols 9 | rows, cols = rows.first, cols.first 10 | 11 | window.scrollok true 12 | 13 | # +scroll+ is used to manually scroll one line at a time 14 | 10.times do |i| 15 | window.addstr("line #{i}\n") 16 | end 17 | window.refresh 18 | window.getch 19 | 20 | window.scroll 21 | window.refresh 22 | window.getch 23 | 24 | window.scroll 25 | window.refresh 26 | window.getch 27 | 28 | window.scroll 29 | window.refresh 30 | window.getch 31 | 32 | # +scrl(n) is used to manually scroll by n lines at a time 33 | window.scrl 5 34 | window.addstr "the cursor position hasn't moved this whole time" 35 | window.refresh 36 | window.getch 37 | 38 | 39 | # +scrl(n) can take negative numbers to scroll down, although 40 | # the content that was previously scrolled up is not remembered 41 | window.scrl -5 42 | window.refresh 43 | window.getch 44 | 45 | window.getch 46 | ensure 47 | Ncurses.endwin 48 | end -------------------------------------------------------------------------------- /yoda.rb: -------------------------------------------------------------------------------- 1 | require 'ncurses' 2 | 3 | begin 4 | window = Ncurses.initscr 5 | Ncurses.cbreak 6 | 7 | window.instance_eval do 8 | # printw is same as printf function 9 | printw "Yoda is %d years old\n", 874 10 | printw "He has collected %d years\n", 65 11 | printw "of Social Security" 12 | end 13 | window.refresh 14 | 15 | window.getch 16 | ensure 17 | Ncurses.endwin 18 | end --------------------------------------------------------------------------------