#!/usr/bin/ruby # **************************************************************************** # * VT100 escape sequence * # **************************************************************************** module VT100 def cls; print "\e[2J"; end def move(x, y); printf("\e[%d;%dH",x, y); end def bold; print "\e[1m"; end def reset; print "\e[0m"; end def sc; print "\e7"; end # * store cursor * def rc; print "\e8"; end # * restore cursor * def d_right; print "\e[0K"; end # * delete right side of the cursor * end include VT100 # **************************************************************************** # * Menu class * # **************************************************************************** class Menu def initialize( table, y0, x0 ) @table = table @y0 = y0 @x0 = x0 end def display cls @table.each do |line| move(@y0 + line["y"], @x0 + line["x"]) print line["item"] end end def keyin(first, last) sc while( true ) key_in = gets if key_in =~ /^[0-9]/ n = key_in.to_i if (n >= first && n <= last) return n end end rc d_right end end end # **************************************************************************** # * menu table * # **************************************************************************** menu_table = [ {"y" => 0, "x" => 5, "item" => "Main Menu" }, {"y" => 3, "x" => 0, "item" => "1 Computer (press 1)" }, {"y" => 5, "x" => 0, "item" => "2 Logic (press 2)" }, {"y" => 7, "x" => 0, "item" => "3 Others (press 3)" }, {"y" => 9, "x" => 0, "item" => "4 Exit (press 4)" }, {"y" => 11, "x" => 5, "item" => "key in please : " } ] # **************************************************************************** # * main() * # **************************************************************************** def task( n ) rc print "item No. #{n} is selected" $stdout.flush sleep(1) end menu = Menu.new( menu_table, 5, 25 ) while true menu.display item_number = menu.keyin( 1, 4 ) case item_number when 1 task(1) when 2 task(2) when 3 task(3) when 4 cls; move(1, 1) exit end end