1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* sample/biorhythm.rb: follow Ruby 1.9 libraries.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
gotoyuzo 2007-12-24 21:21:16 +00:00
parent a79ce52e08
commit a560576158
2 changed files with 45 additions and 76 deletions

View file

@ -1,3 +1,7 @@
Tue Dec 25 06:19:04 2007 GOTOU Yuuzou <gotoyuzo@notwork.org>
* sample/biorhythm.rb: follow Ruby 1.9 libraries.
Tue Dec 25 06:15:01 2007 Koichi Sasada <ko1@atdot.net>
* vm.c: add dummy toplevel frame.

View file

@ -27,21 +27,8 @@
include Math
require "date.rb"
require "parsearg.rb"
require "parsedate.rb"
def usage()
print "Usage:\n"
print "biorhythm.rb [options]\n"
print " options...\n"
print " -D YYYYMMDD(birthday) : use default values.\n"
print " --sdate | --date YYYYMMDD : use system date; use specified date.\n"
print " --birthday YYYYMMDD : specifies your birthday.\n"
print " -v | -g : show values or graph.\n"
print " --days DAYS : graph range (only in effect for graphs).\n"
print " --help : help\n"
end
$USAGE = 'usage'
require "optparse"
require "optparse/date"
def printHeader(y, m, d, p, w)
print "\n>>> Biorhythm <<<\n"
@ -58,60 +45,52 @@ def getPosition(z)
return phys, emot, geist
end
def parsedate(s)
ParseDate::parsedate(s).values_at(0, 1, 2)
end
def name_of_week(date)
Date::DAYNAMES[date.wday]
def prompt(msg)
$stderr.print msg
return gets.chomp
end
#
# main program
#
parseArgs(0, nil, "vg", "D:", "sdate", "date:", "birthday:", "days:")
if $OPT_D
dd = Date.today
bd = Date.new(*parsedate($OPT_D))
ausgabeart = "g"
else
if $OPT_birthday
bd = Date.new(*parsedate($OPT_birthday))
else
STDERR.print("Birthday (YYYYMMDD) : ")
unless (si = STDIN.gets.chop).empty?
bd = Date.new(*parsedate(si))
options = {
:graph => true,
:date => Date.today,
:days => 9,
}
ARGV.options do |opts|
opts.on("-b", "--birthday=DATE", Date, "specify your birthday"){|v|
options[:birthday] = v
}
opts.on("--date=DATE", Date, "specify date to show"){|v|
options[:date] = v
}
opts.on("-g", "--show-graph", TrueClass, "show graph (default)"){|v|
options[:graph] = v
}
opts.on("-v", "--show-values", TrueClass, "show values"){|v|
options[:graph] = !v
}
opts.on("--days=DAYS", Integer, "graph range (only in effect for graph)"){|v|
options[:days] = v - 1
}
opts.on_tail("-h", "--help", "show this message"){puts opts; exit}
begin
opts.parse!
rescue => ex
puts "Error: #{ex.message}"
puts opts
exit
end
end
if !bd
STDERR.print "BAD Input Birthday!!\n"
exit()
end
if $OPT_sdate
dd = Date.today
elsif $OPT_date
dd = Date.new(*parsedate($OPT_date))
else
STDERR.print("Date [<RETURN> for Systemdate] (YYYYMMDD) : ")
unless (si = STDIN.gets.chop).empty?
dd = Date.new(*parsedate(si))
end
end
dd ||= Date.today
bd = options[:birthday] || Date.parse(prompt("Your birthday (YYYYMMDD): "))
dd = options[:date] || Date.today
ausgabeart = options[:graph] ? "g" : "v"
display_period = options[:days]
if $OPT_v
ausgabeart = "v"
elsif $OPT_g
ausgabeart = "g"
else
STDERR.print("Values for today or Graph (v/g) [default g] : ")
ausgabeart = STDIN.gets.chop
end
end
if ausgabeart == "v"
printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
printHeader(bd.year, bd.month, bd.day, dd - bd, bd.strftime("%a"))
print "\n"
phys, emot, geist = getPosition(dd - bd)
@ -121,21 +100,7 @@ if ausgabeart == "v"
printf "Mental: %d%%\n", geist
print "\n"
else
if $OPT_days
display_period = $OPT_days.to_i
elsif $OPT_D
display_period = 9
else
STDERR.printf("Graph for how many days [default 10] : ")
display_period = STDIN.gets.chop
if display_period.empty?
display_period = 9
else
display_period = display_period.to_i - 1
end
end
printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
printHeader(bd.year, bd.month, bd.day, dd - bd, bd.strftime("%a"))
print " P=physical, E=emotional, M=mental\n"
print " -------------------------+-------------------------\n"
print " Bad Condition | Good Condition\n"