summaryrefslogtreecommitdiff
path: root/trunk/ext/tk/sample/cd_timer.rb
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
commit0dc342de848a642ecce8db697b8fecd83a63e117 (patch)
tree2b7ed4724aff1f86073e4740134bda9c4aac1a39 /trunk/ext/tk/sample/cd_timer.rb
parentef70cf7138ab8034b5b806f466e4b484b24f0f88 (diff)
added tag v1_9_0_4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_9_0_4@18845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'trunk/ext/tk/sample/cd_timer.rb')
-rw-r--r--trunk/ext/tk/sample/cd_timer.rb81
1 files changed, 81 insertions, 0 deletions
diff --git a/trunk/ext/tk/sample/cd_timer.rb b/trunk/ext/tk/sample/cd_timer.rb
new file mode 100644
index 0000000000..9154e89bfe
--- /dev/null
+++ b/trunk/ext/tk/sample/cd_timer.rb
@@ -0,0 +1,81 @@
+#!/usr/bin/env ruby
+#
+# countdown timer
+# usage: cd_timer min [, min ... ]
+# ( e.g. cd_timer 0.5 1 3 5 10 )
+#
+require 'tk'
+
+if ARGV.empty?
+ $stderr.puts 'Error:: No time arguments for counting down'
+ exit(1)
+end
+
+width = 10
+
+TkButton.new(:text=>'exit',
+ :command=>proc{exit}).pack(:side=>:bottom, :fill=>:x)
+
+b = TkButton.new(:text=>'start').pack(:side=>:top, :fill=>:x)
+
+f = TkFrame.new(:relief=>:ridge, :borderwidth=>2).pack(:fill=>:x)
+TkLabel.new(f, :relief=>:flat, :pady=>3,
+ :background=>'black', :foreground=>'white',
+ :text=>' elapsed: ').pack(:fill=>:x, :side=>:left, :expand=>true)
+now = TkLabel.new(f, :width=>width, :relief=>:flat, :pady=>3, :anchor=>:w,
+ :background=>'black', :foreground=>'white',
+ :text=>'%4d:%02d.00' % [0, 0]).pack(:side=>:right)
+
+timers = [ TkRTTimer.new(10){|tm|
+ t = (tm.return_value || 0) + 1
+ s, u = t.divmod(100)
+ m, s = s.divmod(60)
+ now.text('%4d:%02d.%02d' % [m, s, u])
+ t
+ }.set_start_proc(0, proc{
+ now.text('%4d:%02d.00' % [0,0])
+ now.foreground('white')
+ 0
+ })
+]
+
+ARGV.collect{|arg| (Float(arg) * 60).to_i}.sort.each_with_index{|time, idx|
+ f = TkFrame.new(:relief=>:ridge, :borderwidth=>2).pack(:fill=>:x)
+ TkLabel.new(f, :relief=>:flat, :pady=>3,
+ :text=>' %4d:%02d --> ' % (time.divmod(60))).pack(:side=>:left)
+ l = TkLabel.new(f, :width=>width, :relief=>:flat, :pady=>3, :anchor=>:w,
+ :text=>'%4d:%02d' % (time.divmod(60))).pack(:side=>:right)
+ timers << TkRTTimer.new(1000){|tm|
+ t = (tm.return_value || time) - 1
+ if t < 0
+ l.text('%4d:%02d' % ((-t).divmod(60)))
+ else
+ l.text('%4d:%02d' % (t.divmod(60)))
+ end
+ if t.zero?
+ l.foreground('red')
+ idx.times{Tk.bell}
+ end
+ t
+ }.set_start_proc(0, proc{
+ l.text('%4d:%02d' % (time.divmod(60)))
+ l.foreground('black')
+ time
+ })
+}
+
+mode = :start
+b.command(proc{
+ if mode == :start
+ timers.each{|timer| timer.restart}
+ b.text('reset')
+ mode = :reset
+ else
+ timers.each{|timer| timer.stop.reset}
+ b.text('start')
+ mode = :start
+ end
+ })
+
+Tk.mainloop
+