diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-05-29 09:32:14 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-05-29 09:32:14 +0000 |
commit | cfe64537f8c34e003b49a5c265600787555ae467 (patch) | |
tree | 2970ecd3579a4d25d78db47c4d7bc67a16e31601 /sample | |
parent | a8382af43a39aa1e442861240506559157921288 (diff) |
*** empty log message ***
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rw-r--r-- | sample/freq.rb | 2 | ||||
-rw-r--r-- | sample/tkbrowse.rb | 82 |
2 files changed, 47 insertions, 37 deletions
diff --git a/sample/freq.rb b/sample/freq.rb index d951591735..16bf1794be 100644 --- a/sample/freq.rb +++ b/sample/freq.rb @@ -4,7 +4,7 @@ freq = {} while gets while sub!(/\w+/, '') word = $& - freq[word] +=1 + freq[word] = freq.fetch(word, 0)+1 end end diff --git a/sample/tkbrowse.rb b/sample/tkbrowse.rb index d127996173..882f0a489b 100644 --- a/sample/tkbrowse.rb +++ b/sample/tkbrowse.rb @@ -9,34 +9,59 @@ require "tkscrollbox" -list = TkScrollbox.new { - relief 'raised' - width 20 - height 20 - setgrid 'yes' - pack -} - # The procedure below is invoked to open a browser on a given file; if the # file is a directory then another instance of this program is invoked; if # the file is a regular file then the Mx editor is invoked to display # the file. +$dirlist = {} + +def browsedir (dir) + if $dirlist.key? dir + $dirlist[dir] + else + top = if $dirlist.size > 0 then TkToplevel.new else nil end + list = TkScrollbox.new(top) { + relief 'raised' + width 20 + height 20 + setgrid 'yes' + pack + } + list.insert 'end', *`ls #{dir}`.split + + # Set up bindings for the browser. + + list.focus + list.bind "Control-q", proc{exit} + list.bind "Control-c", proc{exit} + list.bind "Control-p", proc{ + print "selection <", TkSelection.get, ">\n" + } + + list.bind "Double-Button-1", proc{ + for i in TkSelection.get.split + print "clicked ", i, "\n" + browse dir, i + end + } + $dirlist[dir] = list + end +end + def browse (dir, file) - if dir != "." - file="#{dir}/#{file}" - if File.directory? file - system "browse #{file} &" - else - if File.file? file - if ENV['EDITOR'] - system format("%s %s&", ENV['EDITOR'], file) - else - sysmte "xedit #{file}&" - end + file="#{dir}/#{file}" + if File.directory? file + browsedir(file) + else + if File.file? file + if ENV['EDITOR'] + system format("%s %s&", ENV['EDITOR'], file) else - STDERR.print "\"#{file}\" isn't a directory or regular file" + system "xedit #{file}&" end + else + STDERR.print "\"#{file}\" isn't a directory or regular file" end end end @@ -49,21 +74,6 @@ if ARGV.length>0 else dir="." end -list.insert 'end', *`ls #{dir}`.split -# Set up bindings for the browser. - -list.focus -list.bind "Control-q", proc{exit} -list.bind "Control-c", proc{exit} -list.bind "Control-p", proc{ - print "selection <", TkSelection.get, ">\n" -} - -list.bind "Double-Button-1", proc{ - for i in TkSelection.get.split - print "clicked ", i, "\n" - browse dir, i - end -} +browsedir(dir) Tk.mainloop |