ruby-changes:30189
From: zzak <ko1@a...>
Date: Mon, 29 Jul 2013 22:26:06 +0900 (JST)
Subject: [ruby-changes:30189] zzak:r42241 (trunk): * ext/curses/curses.c: [DOC] Update location of samples
zzak 2013-07-29 22:25:46 +0900 (Mon, 29 Jul 2013) New Revision: 42241 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42241 Log: * ext/curses/curses.c: [DOC] Update location of samples * samples/curses/*: Move Curses samples and refactor from mixin The samples are included in rdoc for module and use of mixin is confusing Added directories: trunk/sample/curses/ Added files: trunk/sample/curses/hello.rb trunk/sample/curses/mouse.rb trunk/sample/curses/rain.rb trunk/sample/curses/view.rb trunk/sample/curses/view2.rb Removed files: trunk/ext/curses/hello.rb trunk/ext/curses/mouse.rb trunk/ext/curses/rain.rb trunk/ext/curses/view.rb trunk/ext/curses/view2.rb Modified files: trunk/ChangeLog trunk/ext/curses/curses.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42240) +++ ChangeLog (revision 42241) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Jul 29 22:25:20 2013 Zachary Scott <e@z...> + + * ext/curses/curses.c: [DOC] Update location of samples + * samples/curses/*: Move Curses samples and refactor from mixin + The samples are included in rdoc for module and use of mixin is + confusing + Mon Jul 29 22:16:11 2013 Tanaka Akira <akr@f...> * bignum.c (LOG2_KARATSUBA_BIG2STR_DIGITS): Renamed from Index: sample/curses/hello.rb =================================================================== --- sample/curses/hello.rb (revision 0) +++ sample/curses/hello.rb (revision 42241) @@ -0,0 +1,27 @@ https://github.com/ruby/ruby/blob/trunk/sample/curses/hello.rb#L1 +require "curses" + +def show_message(message) + width = message.length + 6 + win = Curses::Window.new(5, width, + (Curses.lines - 5) / 2, (Curses.cols - width) / 2) + win.box('|', '-') + win.setpos(2, 3) + win.addstr(message) + win.refresh + win.getch + win.close +end + +Curses.init_screen +begin + Curses.crmode +# show_message("Hit any key") + Curses.setpos((Curses.lines - 5) / 2, (Curses.cols - 10) / 2) + Curses.addstr("Hit any key") + Curses.refresh + char = Curses.getch + show_message("You typed: #{char}") + Curses.refresh +ensure + Curses.close_screen +end Index: sample/curses/mouse.rb =================================================================== --- sample/curses/mouse.rb (revision 0) +++ sample/curses/mouse.rb (revision 42241) @@ -0,0 +1,52 @@ https://github.com/ruby/ruby/blob/trunk/sample/curses/mouse.rb#L1 +require "curses" + +def show_message(*msgs) + message = msgs.join + width = message.length + 6 + win = Curses::Window.new(5, width, + (Curses.lines - 5) / 2, (Curses.cols - width) / 2) + win.keypad = true + win.attron(Curses.color_pair(Curses::COLOR_RED)){ + win.box(?|, ?-, ?+) + } + win.setpos(2, 3) + win.addstr(message) + win.refresh + win.getch + win.close +end + +Curses.init_screen +Curses.start_color +Curses.init_pair(Curses::COLOR_BLUE, Curses::COLOR_BLUE, Curses::COLOR_WHITE) +Curses.init_pair(Curses::COLOR_RED, Curses::COLOR_RED, Curses::COLOR_WHITE) +Curses.crmode +Curses.noecho +Curses.stdscr.keypad(true) + +begin + Curses.mousemask( + Curses::BUTTON1_CLICKED|Curses::BUTTON2_CLICKED|Curses::BUTTON3_CLICKED|Curses::BUTTON4_CLICKED + ) + Curses.setpos((Curses.lines - 5) / 2, (Curses.cols - 10) / 2) + Curses.attron(Curses.color_pair(Curses::COLOR_BLUE)|Curses::A_BOLD){ + Curses.addstr("click") + } + Curses.refresh + while( true ) + c = Curses.getch + case c + when Curses::KEY_MOUSE + m = Curses::getmouse + if( m ) + show_message("getch = #{c.inspect}, ", + "mouse event = #{'0x%x' % m.bstate}, ", + "axis = (#{m.x},#{m.y},#{m.z})") + end + break + end + end + Curses.refresh +ensure + Curses.close_screen +end Index: sample/curses/rain.rb =================================================================== --- sample/curses/rain.rb (revision 0) +++ sample/curses/rain.rb (revision 42241) @@ -0,0 +1,74 @@ https://github.com/ruby/ruby/blob/trunk/sample/curses/rain.rb#L1 +# rain for a curses test + +require "curses" + +def onsig(sig) + Curses.close_screen + exit sig +end + +def ranf + rand(32767).to_f / 32767 +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) } + end +end + +Curses.init_screen +Curses.nl +Curses.noecho +srand + +xpos = {} +ypos = {} +r = Curses.lines - 4 +c = Curses.cols - 4 +for i in 0 .. 4 + xpos[i] = (c * ranf).to_i + 2 + ypos[i] = (r * ranf).to_i + 2 +end + +i = 0 +while TRUE + x = (c * ranf).to_i + 2 + y = (r * ranf).to_i + 2 + + + Curses.setpos(y, x); Curses.addstr(".") + + Curses.setpos(ypos[i], xpos[i]); Curses.addstr("o") + + i = if i == 0 then 4 else i - 1 end + Curses.setpos(ypos[i], xpos[i]); Curses.addstr("O") + + i = if i == 0 then 4 else i - 1 end + Curses.setpos(ypos[i] - 1, xpos[i]); Curses.addstr("-") + Curses.setpos(ypos[i], xpos[i] - 1); Curses.addstr("|.|") + Curses.setpos(ypos[i] + 1, xpos[i]); Curses.addstr("-") + + i = if i == 0 then 4 else i - 1 end + Curses.setpos(ypos[i] - 2, xpos[i]); Curses.addstr("-") + Curses.setpos(ypos[i] - 1, xpos[i] - 1); Curses.addstr("/ \\") + Curses.setpos(ypos[i], xpos[i] - 2); Curses.addstr("| O |") + Curses.setpos(ypos[i] + 1, xpos[i] - 1); Curses.addstr("\\ /") + Curses.setpos(ypos[i] + 2, xpos[i]); Curses.addstr("-") + + i = if i == 0 then 4 else i - 1 end + Curses.setpos(ypos[i] - 2, xpos[i]); Curses.addstr(" ") + Curses.setpos(ypos[i] - 1, xpos[i] - 1); Curses.addstr(" ") + Curses.setpos(ypos[i], xpos[i] - 2); Curses.addstr(" ") + Curses.setpos(ypos[i] + 1, xpos[i] - 1); Curses.addstr(" ") + Curses.setpos(ypos[i] + 2, xpos[i]); Curses.addstr(" ") + + + xpos[i] = x + ypos[i] = y + Curses.refresh + sleep(0.5) +end + +# end of main Index: sample/curses/view.rb =================================================================== --- sample/curses/view.rb (revision 0) +++ sample/curses/view.rb (revision 42241) @@ -0,0 +1,91 @@ https://github.com/ruby/ruby/blob/trunk/sample/curses/view.rb#L1 +#!/usr/local/bin/ruby + +require "curses" +include Curses + +# +# main +# + +if ARGV.size != 1 then + printf("usage: view file\n"); + exit +end +begin + fp = open(ARGV[0], "r") +rescue + raise "cannot open file: #{ARGV[1]}" +end + +# signal(SIGINT, finish) + +init_screen +#keypad(stdscr, TRUE) +nonl +cbreak +noecho +#scrollok(stdscr, TRUE) + +# slurp the file +data_lines = [] +fp.each_line { |l| + data_lines.push(l) +} +fp.close + + +lptr = 0 +while TRUE + i = 0 + while i < lines + setpos(i, 0) + #clrtoeol + addstr(data_lines[lptr + i] || '') + i += 1 + end + refresh + + explicit = FALSE + n = 0 + while TRUE + c = getch + if c =~ /[0-9]/ + n = 10 * n + c.to_i + else + break + end + end + + n = 1 if !explicit && n == 0 + + case c + when "n" #when KEY_DOWN + i = 0 + while i < n + if lptr + lines < data_lines.size then + lptr += 1 + else + break + end + i += 1 + end + #wscrl(i) + + when "p" #when KEY_UP + i = 0 + while i < n + if lptr > 0 then + lptr -= 1 + else + break + end + i += 1 + end + #wscrl(-i) + + when "q" + break + end + +end +close_screen Index: sample/curses/view2.rb =================================================================== --- sample/curses/view2.rb (revision 0) +++ sample/curses/view2.rb (revision 42241) @@ -0,0 +1,149 @@ https://github.com/ruby/ruby/blob/trunk/sample/curses/view2.rb#L1 +#!/usr/local/bin/ruby + +require "curses" + + +# A curses based file viewer +class FileViewer + + # Create a new fileviewer, and view the file. + def initialize(filename) + @data_lines = [] + @screen = nil + @top = nil + init_curses + load_file(filename) + interact + end + + # Perform the curses setup + def init_curses + # signal(SIGINT, finish) + + Curses.init_screen + Curses.nonl + Curses.cbreak + Curses.noecho + + @screen = Curses.stdscr + + @screen.scrollok(true) + #$screen.keypad(true) + end + + # 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 + @top = 0 + @data_lines[0..@s...].each_with_index{|line, idx| + @screen.setpos(idx, 0) + @screen.addstr(line) + } + @screen.setpos(0,0) + @screen.refresh + rescue + raise "cannot open file '#{filename}' for reading" + end + + + # Scroll the display up by one line + def scroll_up + if( @top > 0 ) + @screen.scrl(-1) + @top -= 1 + str = @data_lines[@top] + if( str ) + @screen.setpos(0, 0) + @screen.addstr(str) + end + return true + else + return false + end + end + + # Scroll the display down by one line + def scroll_down + if( @top + @screen.maxy < @data_lines.length ) + @screen.scrl(1) + @top += 1 + str = @data_lines[@top + @screen.maxy - 1] + if( str ) + @screen.setpos(@screen.maxy - 1, 0) + @screen.addstr(str) + end + return true + else + return false + end + end + + # Allow the user to interact with the display. + # 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 + result = true + c = Curses.getch + case c + when Curses::KEY_DOWN, Curses::KEY_CTRL_N, ?j + result = scroll_down + 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 + break + end + end + when Curses::KEY_PPAGE + for i in 0..(@screen.maxy - 2) + if( ! scroll_up ) + if( i == 0 ) + result = false + end + break + end + end + 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 ) + end + when ?q + break + else + @screen.setpos(0,0) + @screen.addstr("[unknown key `#{Curses.keyname(c)}'=#{c}] ") + end + if( !result ) + Curses.beep + end + @screen.setpos(0,0) + end + Curses.close_screen + end +end + + +# If we are being run as a main program... +if __FILE__ == $0 + if ARGV.size != 1 then + printf("usage: #{$0} file\n"); + exit + end + + viewer = FileViewer.new(ARGV[0]) +end Index: ext/curses/hello.rb =================================================================== --- ext/curses/hello.rb (revision 42240) +++ ext/curses/hello.rb (revision 42241) @@ -1,30 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/curses/hello.rb#L0 -#!/usr/local/bin/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('|', '-') - win.setpos(2, 3) - win.addstr(message) - win.refresh - win.getch - win.close -end - -init_screen -begin - crmode -# show_message("Hit any key") - setpos((lines - 5) / 2, (cols - 10) / 2) - addstr("Hit any key") - refresh - getch - show_message("Hello, World!") - refresh -ensure - close_screen -end Index: ext/curses/mouse.rb =================================================================== --- ext/curses/mouse.rb (revision 42240) +++ ext/curses/mouse.rb (revision 42241) @@ -1,53 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/curses/mouse.rb#L0 -#!/usr/local/bin/ruby - -require "curses" -include Curses - -def show_message(*msgs) - message = msgs.join - width = message.length + 6 - win = Window.new(5, width, - (lines - 5) / 2, (cols - width) / 2) - win.keypad = true - win.attron(color_pair(COLOR_RED)){ - win.box(?|, ?-, ?+) - } - win.setpos(2, 3) - win.addstr(message) - win.refresh - win.getch - win.close -end - -init_screen -start_color -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){ - addstr("click") - } - refresh - while( true ) - 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})") - end - break - end - end - refresh -ensure - close_screen -end Index: ext/curses/rain.rb =================================================================== --- ext/curses/rain.rb (revision 42240) +++ ext/curses/rain.rb (revision 42241) @@ -1,76 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/curses/rain.rb#L0 -#!/usr/local/bin/ruby -# rain for a curses test - -require "curses" -include Curses - -def onsig(sig) - close_screen - exit sig -end - -def ranf - rand(32767).to_f / 32767 -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) } - end -end - -init_screen -nl -noecho -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 -end - -i = 0 -while TRUE - x = (c * ranf).to_i + 2 - y = (r * ranf).to_i + 2 - - - setpos(y, x); addstr(".") - - setpos(ypos[i], xpos[i]); addstr("o") - - i = if i == 0 then 4 else i - 1 end - setpos(ypos[i], xpos[i]); addstr("O") - - 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 = 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 = 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] = x - ypos[i] = y - refresh - sleep(0.5) -end - -# end of main Index: ext/curses/view.rb =================================================================== --- ext/curses/view.rb (revision 42240) +++ ext/curses/view.rb (revision 42241) @@ -1,91 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/curses/view.rb#L0 -#!/usr/local/bin/ruby - -require "curses" -include Curses - -# -# main -# - -if ARGV.size != 1 then - printf("usage: view file\n"); - exit -end -begin - fp = open(ARGV[0], "r") -rescue - raise "cannot open file: #{ARGV[1]}" -end - -# signal(SIGINT, finish) - -init_screen -#keypad(stdscr, TRUE) -nonl -cbreak -noecho -#scrollok(stdscr, TRUE) - -# slurp the file -data_lines = [] -fp.each_line { |l| - data_lines.push(l) -} -fp.close - - -lptr = 0 -while TRUE - i = 0 - while i < lines - setpos(i, 0) - #clrtoeol - addstr(data_lines[lptr + i] || '') - i += 1 - end - refresh - - explicit = FALSE - n = 0 - while TRUE - c = getch - if c =~ /[0-9]/ - n = 10 * n + c.to_i - else - break - end - end - - n = 1 if !explicit && n == 0 - - case c - when "n" #when KEY_DOWN - i = 0 - while i < n - if lptr + lines < data_lines.size then - lptr += 1 - else - break - end - i += 1 - end - #wscrl(i) - - when "p" #when KEY_UP - i = 0 - while i < n - if lptr > 0 then - lptr -= 1 - else - break - end - i += 1 - end - #wscrl(-i) - - when "q" - break - end - -end -close_screen Index: ext/curses/view2.rb =================================================================== --- ext/curses/view2.rb (revision 42240) +++ ext/curses/view2.rb (revision 42241) @@ -1,149 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/curses/view2.rb#L0 -#!/usr/local/bin/ruby - -require "curses" - - -# A curses based file viewer -class FileViewer - - # Create a new fileviewer, and view the file. - def initialize(filename) - @data_lines = [] - @screen = nil - @top = nil - init_curses - load_file(filename) - interact - end - - # Perform the curses setup - def init_curses - # signal(SIGINT, finish) - - Curses.init_screen - Curses.nonl - Curses.cbreak - Curses.noecho - - @screen = Curses.stdscr - - @screen.scrollok(true) - #$screen.keypad(true) - end - - # 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 - @top = 0 - @data_lines[0..@s...].each_with_index{|line, idx| - @screen.setpos(idx, 0) - @screen.addstr(line) - } - @screen.setpos(0,0) - @screen.refresh - rescue - raise "cannot open file '#{filename}' for reading" - end - - - # Scroll the display up by one line - def scroll_up - if( @top > 0 ) - @screen.scrl(-1) - @top -= 1 - str = @data_lines[@top] - if( str ) - @screen.setpos(0, 0) - @screen.addstr(str) - end - return true - else - return false - end - end - - # Scroll the display down by one line - def scroll_down - if( @top + @screen.maxy < @data_lines.length ) - @screen.scrl(1) - @top += 1 - str = @data_lines[@top + @screen.maxy - 1] - if( str ) - @screen.setpos(@screen.maxy - 1, 0) - @screen.addstr(str) - end - return true - else - return false - end - end - - # Allow the user to interact with the display. - # 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 - result = true - c = Curses.getch - case c - when Curses::KEY_DOWN, Curses::KEY_CTRL_N, ?j - result = scroll_down - 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 - break - end - end - when Curses::KEY_PPAGE - for i in 0..(@screen.maxy - 2) - if( ! scroll_up ) - if( i == 0 ) - result = false - end - break - end - end - 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 ) - end - when ?q - break - else - @screen.setpos(0,0) - @screen.addstr("[unknown key `#{Curses.keyname(c)}'=#{c}] ") - end - if( !result ) - Curses.beep - end - @screen.setpos(0,0) - end - Curses.close_screen - end -end - - -# If we are being run as a main program... -if __FILE__ == $0 - if ARGV.size != 1 then - printf("usage: #{$0} file\n"); - exit - end - - viewer = FileViewer.new(ARGV[0]) -end Index: ext/curses/curses.c =================================================================== --- ext/curses/curses.c (revision 42240) +++ ext/curses/curses.c (revision 42241) @@ -2643,11 +2643,11 @@ pad_noutrefresh(VALUE obj, VALUE pminrow https://github.com/ruby/ruby/blob/trunk/ext/curses/curses.c#L2643 * == Examples * * * hello.rb - * :include: hello.rb + * :include: sample/curses/hello.rb * * * * rain.rb - * :include: rain.rb + * :include: sample/curses/rain.rb * * */ @@ -2680,7 +2680,7 @@ Init_curses(void) https://github.com/ruby/ruby/blob/trunk/ext/curses/curses.c#L2680 * == Example * * * mouse.rb - * :include: mouse.rb + * :include: sample/curses/mouse.rb * */ cMouseEvent = rb_define_class_under(mCurses,"MouseEv (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/