summaryrefslogtreecommitdiff
path: root/sample/curses
diff options
context:
space:
mode:
Diffstat (limited to 'sample/curses')
-rw-r--r--sample/curses/hello.rb27
-rw-r--r--sample/curses/mouse.rb52
-rw-r--r--sample/curses/rain.rb74
-rw-r--r--sample/curses/view.rb91
-rw-r--r--sample/curses/view2.rb149
5 files changed, 393 insertions, 0 deletions
diff --git a/sample/curses/hello.rb b/sample/curses/hello.rb
new file mode 100644
index 00000000000..a630fb999b8
--- /dev/null
+++ b/sample/curses/hello.rb
@@ -0,0 +1,27 @@
+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
diff --git a/sample/curses/mouse.rb b/sample/curses/mouse.rb
new file mode 100644
index 00000000000..cc4beeb83da
--- /dev/null
+++ b/sample/curses/mouse.rb
@@ -0,0 +1,52 @@
+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
diff --git a/sample/curses/rain.rb b/sample/curses/rain.rb
new file mode 100644
index 00000000000..845da2f5224
--- /dev/null
+++ b/sample/curses/rain.rb
@@ -0,0 +1,74 @@
+# 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
diff --git a/sample/curses/view.rb b/sample/curses/view.rb
new file mode 100644
index 00000000000..bc54aeb9afa
--- /dev/null
+++ b/sample/curses/view.rb
@@ -0,0 +1,91 @@
+#!/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
diff --git a/sample/curses/view2.rb b/sample/curses/view2.rb
new file mode 100644
index 00000000000..037771a2264
--- /dev/null
+++ b/sample/curses/view2.rb
@@ -0,0 +1,149 @@
+#!/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..@screen.maxy-1].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