diff options
Diffstat (limited to 'sample')
-rwxr-xr-x | sample/hello.rb | 27 | ||||
-rwxr-xr-x | sample/mouse.rb | 32 | ||||
-rwxr-xr-x | sample/rain.rb | 96 | ||||
-rwxr-xr-x | sample/view.rb | 61 | ||||
-rwxr-xr-x | sample/view2.rb | 85 |
5 files changed, 135 insertions, 166 deletions
diff --git a/sample/hello.rb b/sample/hello.rb index 6308a17..6ef31cd 100755 --- a/sample/hello.rb +++ b/sample/hello.rb @@ -1,13 +1,14 @@ #!/usr/bin/env ruby require "curses" -include Curses def show_message(message) - width = message.length + 6 - win = Window.new(5, width, - (lines - 5) / 2, (cols - width) / 2) - win.box('|', '-') + height = 5 + width = message.length + 6 + top = (Curses.lines - height) / 2 + left = (Curses.cols - width) / 2 + win = Curses::Window.new(height, width, top, left) + win.box("|", "-") win.setpos(2, 3) win.addstr(message) win.refresh @@ -15,16 +16,14 @@ def show_message(message) win.close end -init_screen +Curses.init_screen begin - crmode -# show_message("Hit any key") - setpos((lines - 5) / 2, (cols - 10) / 2) - addstr("Hit any key") - refresh - getch + Curses.crmode + Curses.setpos((Curses.lines - 1) / 2, (Curses.cols - 11) / 2) + Curses.addstr("Hit any key") + Curses.refresh + Curses.getch show_message("Hello, World!") - refresh ensure - close_screen + Curses.close_screen end diff --git a/sample/mouse.rb b/sample/mouse.rb index 49d4802..92c2b96 100755 --- a/sample/mouse.rb +++ b/sample/mouse.rb @@ -5,13 +5,13 @@ include Curses def show_message(*msgs) message = msgs.join - width = message.length + 6 - win = Window.new(5, width, - (lines - 5) / 2, (cols - width) / 2) + height, width = 5, message.length + 6 + top, left = (lines - height) / 2, (cols - width) / 2 + win = Window.new(height, width, top, left) win.keypad = true - win.attron(color_pair(COLOR_RED)){ - win.box(?|, ?-, ?+) - } + win.attron(color_pair(COLOR_RED)) do + win.box("|", "-", "+") + end win.setpos(2, 3) win.addstr(message) win.refresh @@ -21,28 +21,28 @@ end init_screen start_color -init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_WHITE) -init_pair(COLOR_RED,COLOR_RED,COLOR_WHITE) +init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_WHITE) +init_pair(COLOR_RED, COLOR_RED, COLOR_WHITE) crmode noecho stdscr.keypad(true) begin mousemask(BUTTON1_CLICKED|BUTTON2_CLICKED|BUTTON3_CLICKED|BUTTON4_CLICKED) - setpos((lines - 5) / 2, (cols - 10) / 2) - attron(color_pair(COLOR_BLUE)|A_BOLD){ + setpos((lines - 1) / 2, (cols - 5) / 2) + attron(color_pair(COLOR_BLUE)|A_BOLD) do addstr("click") - } + end refresh - while( true ) + loop do c = getch case c when KEY_MOUSE m = getmouse - if( m ) - show_message("getch = #{c.inspect}, ", - "mouse event = #{'0x%x' % m.bstate}, ", - "axis = (#{m.x},#{m.y},#{m.z})") + if m + show_message("getch = #{c.inspect}, ", + "mouse event = #{'0x%x' % m.bstate}, ", + "axis = (#{m.x},#{m.y},#{m.z})") end break end diff --git a/sample/rain.rb b/sample/rain.rb index a5f8fc5..6ed5246 100755 --- a/sample/rain.rb +++ b/sample/rain.rb @@ -1,76 +1,72 @@ #!/usr/bin/env ruby -# rain for a curses test require "curses" -include Curses -def onsig(sig) - close_screen - exit sig +def onsig(signal) + Curses.close_screen + exit signal end -def ranf - rand(32767).to_f / 32767 +def place_string(y, x, string) + Curses.setpos(y, x) + Curses.addstr(string) end -# main # -for i in %w[HUP INT QUIT TERM] - if trap(i, "SIG_IGN") != 0 then # 0 for SIG_IGN - trap(i) {|sig| onsig(sig) } +def cycle_index(index) + (index + 1) % 5 +end + +%w[HUP INT QUIT TERM].each do |sig| + unless trap(sig, "IGNORE") == "IGNORE" # previous handler + trap(sig) {|s| onsig(s) } end end -init_screen -nl -noecho +Curses.init_screen +Curses.nl +Curses.noecho +Curses.curs_set 0 srand -xpos = {} -ypos = {} -r = lines - 4 -c = cols - 4 -for i in 0 .. 4 - xpos[i] = (c * ranf).to_i + 2 - ypos[i] = (r * ranf).to_i + 2 +xpos, ypos = {}, {} +x_range = 2..(Curses.cols - 3) +y_range = 2..(Curses.lines - 3) +(0..4).each do |i| + xpos[i], ypos[i] = rand(x_range), rand(y_range) end i = 0 -while TRUE - x = (c * ranf).to_i + 2 - y = (r * ranf).to_i + 2 +loop do + x, y = rand(x_range), rand(y_range) + place_string(y, x, ".") - setpos(y, x); addstr(".") + place_string(ypos[i], xpos[i], "o") - setpos(ypos[i], xpos[i]); addstr("o") + i = cycle_index(i) + place_string(ypos[i], xpos[i], "O") - i = if i == 0 then 4 else i - 1 end - setpos(ypos[i], xpos[i]); addstr("O") + i = cycle_index(i) + place_string(ypos[i] - 1, xpos[i], "-") + place_string(ypos[i], xpos[i] - 1, "|.|") + place_string(ypos[i] + 1, xpos[i], "-") - i = if i == 0 then 4 else i - 1 end - setpos(ypos[i] - 1, xpos[i]); addstr("-") - setpos(ypos[i], xpos[i] - 1); addstr("|.|") - setpos(ypos[i] + 1, xpos[i]); addstr("-") + i = cycle_index(i) + place_string(ypos[i] - 2, xpos[i], "-") + place_string(ypos[i] - 1, xpos[i] - 1, "/ \\") + place_string(ypos[i], xpos[i] - 2, "| O |") + place_string(ypos[i] + 1, xpos[i] - 1, "\\ /") + place_string(ypos[i] + 2, xpos[i], "-") - i = if i == 0 then 4 else i - 1 end - setpos(ypos[i] - 2, xpos[i]); addstr("-") - setpos(ypos[i] - 1, xpos[i] - 1); addstr("/ \\") - setpos(ypos[i], xpos[i] - 2); addstr("| O |") - setpos(ypos[i] + 1, xpos[i] - 1); addstr("\\ /") - setpos(ypos[i] + 2, xpos[i]); addstr("-") + i = cycle_index(i) + place_string(ypos[i] - 2, xpos[i], " ") + place_string(ypos[i] - 1, xpos[i] - 1, " ") + place_string(ypos[i], xpos[i] - 2, " ") + place_string(ypos[i] + 1, xpos[i] - 1, " ") + place_string(ypos[i] + 2, xpos[i], " ") - i = if i == 0 then 4 else i - 1 end - setpos(ypos[i] - 2, xpos[i]); addstr(" ") - setpos(ypos[i] - 1, xpos[i] - 1); addstr(" ") - setpos(ypos[i], xpos[i] - 2); addstr(" ") - setpos(ypos[i] + 1, xpos[i] - 1); addstr(" ") - setpos(ypos[i] + 2, xpos[i]); addstr(" ") + xpos[i], ypos[i] = x, y - - xpos[i] = x - ypos[i] = y - refresh + Curses.refresh sleep(0.5) end - -# end of main diff --git a/sample/view.rb b/sample/view.rb index 300b3b9..66e861a 100755 --- a/sample/view.rb +++ b/sample/view.rb @@ -3,54 +3,41 @@ require "curses" include Curses -# -# main -# - -if ARGV.size != 1 then - printf("usage: view file\n"); +unless ARGV.size == 1 + puts "usage: #{$0} file" exit end + begin - fp = open(ARGV[0], "r") + data_lines = File.readlines(ARGV[0]) rescue - raise "cannot open file: #{ARGV[1]}" + raise "cannot open file: #{ARGV[0]}" end -# signal(SIGINT, finish) - init_screen -#keypad(stdscr, TRUE) +#keypad(stdscr, true) nonl cbreak noecho -#scrollok(stdscr, TRUE) - -# slurp the file -data_lines = [] -fp.each_line { |l| - data_lines.push(l) -} -fp.close - +#scrollok(stdscr, true) lptr = 0 -while TRUE - i = 0 - while i < lines +loop do + lines.times do |i| setpos(i, 0) #clrtoeol - addstr(data_lines[lptr + i] || '') - i += 1 + addstr(data_lines[lptr + i] || "") end refresh - explicit = FALSE + explicit = false n = 0 - while TRUE + c = nil + loop do c = getch if c =~ /[0-9]/ n = 10 * n + c.to_i + explicit = true else break end @@ -61,31 +48,29 @@ while TRUE case c when "n" #when KEY_DOWN i = 0 - while i < n - if lptr + lines < data_lines.size then - lptr += 1 + n.times do + if lptr + lines < data_lines.size + lptr += 1 else - break + break end i += 1 end #wscrl(i) - when "p" #when KEY_UP i = 0 - while i < n - if lptr > 0 then - lptr -= 1 + n.times do + if lptr > 0 + lptr -= 1 else - break + break end i += 1 end #wscrl(-i) - when "q" break end - end + close_screen diff --git a/sample/view2.rb b/sample/view2.rb index c29c0ce..0f3a49d 100755 --- a/sample/view2.rb +++ b/sample/view2.rb @@ -3,10 +3,10 @@ require "curses" -# A curses based file viewer +# A curses based file viewer. class FileViewer - # Create a new fileviewer, and view the file. + # Create a new FileViewer and view the file. def initialize(filename) @data_lines = [] @screen = nil @@ -16,10 +16,8 @@ class FileViewer interact end - # Perform the curses setup + # Perform the curses setup. def init_curses - # signal(SIGINT, finish) - Curses.init_screen Curses.nonl Curses.cbreak @@ -28,37 +26,32 @@ class FileViewer @screen = Curses.stdscr @screen.scrollok(true) - #$screen.keypad(true) + #@screen.keypad(true) end - # Load the file into memory, and put - # the first part on the curses display. + # Load the file into memory and + # put the first part on the curses display. def load_file(filename) - fp = open(filename, "r") do |fp| - # slurp the file - fp.each_line { |l| - @data_lines.push(l.chop) - } - end + @data_lines = File.readlines(filename).map(&:chomp) @top = 0 - @data_lines[0..@screen.maxy-1].each_with_index{|line, idx| + @data_lines[0..@screen.maxy-1].each_with_index do |line, idx| @screen.setpos(idx, 0) @screen.addstr(line) - } - @screen.setpos(0,0) + end + @screen.setpos(0, 0) @screen.refresh rescue raise "cannot open file '#{filename}' for reading" end - # Scroll the display up by one line + # Scroll the display up by one line. def scroll_up - if( @top > 0 ) + if @top > 0 @screen.scrl(-1) @top -= 1 str = @data_lines[@top] - if( str ) + if str @screen.setpos(0, 0) @screen.addstr(str) end @@ -68,13 +61,13 @@ class FileViewer end end - # Scroll the display down by one line + # Scroll the display down by one line. def scroll_down - if( @top + @screen.maxy < @data_lines.length ) + if @top + @screen.maxy < @data_lines.length @screen.scrl(1) @top += 1 str = @data_lines[@top + @screen.maxy - 1] - if( str ) + if str @screen.setpos(@screen.maxy - 1, 0) @screen.addstr(str) end @@ -85,53 +78,49 @@ class FileViewer end # Allow the user to interact with the display. - # This uses EMACS-like keybindings, and also + # This uses Emacs-like keybindings, and also # vi-like keybindings as well, except that left # and right move to the beginning and end of the # file, respectively. def interact - while true + loop do result = true c = Curses.getch case c - when Curses::KEY_DOWN, Curses::KEY_CTRL_N, ?j + when Curses::KEY_DOWN, Curses::KEY_CTRL_N, "j" result = scroll_down - when Curses::KEY_UP, Curses::KEY_CTRL_P, ?k + when Curses::KEY_UP, Curses::KEY_CTRL_P, "k" result = scroll_up - when Curses::KEY_NPAGE, ?\s # white space - for i in 0..(@screen.maxy - 2) - if( ! scroll_down ) - if( i == 0 ) - result = false - end + when Curses::KEY_NPAGE, " " + (@screen.maxy - 1).times do |i| + if !scroll_down && i == 0 + result = false break end end when Curses::KEY_PPAGE - for i in 0..(@screen.maxy - 2) - if( ! scroll_up ) - if( i == 0 ) - result = false - end + (@screen.maxy - 1).times do |i| + if !scroll_up && i == 0 + result = false break end end - when Curses::KEY_LEFT, Curses::KEY_CTRL_T, ?h - while( scroll_up ) + when Curses::KEY_LEFT, Curses::KEY_CTRL_T, "h" + while scroll_up end - when Curses::KEY_RIGHT, Curses::KEY_CTRL_B, ?l - while( scroll_down ) + when Curses::KEY_RIGHT, Curses::KEY_CTRL_B, "l" + while scroll_down end - when ?q + when "q" break else - @screen.setpos(0,0) + @screen.setpos(0, 0) @screen.addstr("[unknown key `#{Curses.keyname(c)}'=#{c}] ") end - if( !result ) + if !result Curses.beep end - @screen.setpos(0,0) + @screen.setpos(0, 0) end Curses.close_screen end @@ -140,8 +129,8 @@ end # If we are being run as a main program... if __FILE__ == $0 - if ARGV.size != 1 then - printf("usage: #{$0} file\n"); + unless ARGV.size == 1 + puts "usage: #{$0} file" exit end |