OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / ext / tk / sample / tkbrowse.rb
1 #!/usr/bin/env ruby
2 #
3 # This script generates a directory browser, which lists the working
4 # directory and allows you to open files or subdirectories by
5 # double-clicking.
6
7 # Create a scrollbar on the right side of the main window and a listbox
8 # on the left side.
9
10 require "tkscrollbox"
11
12 # The procedure below is invoked to open a browser on a given file;  if the
13 # file is a directory then another instance of this program is invoked; if
14 # the file is a regular file then the Mx editor is invoked to display
15 # the file.
16
17 $dirlist = {}
18
19 def browsedir (dir)
20   if $dirlist.key? dir
21     $dirlist[dir]
22   else
23     top = if $dirlist.size > 0 then TkToplevel.new else nil end
24     list = TkScrollbox.new(top) {
25       relief 'raised'
26       width 20
27       height 20
28       setgrid 'yes'
29       pack
30     }
31     list.insert 'end', *`ls #{dir}`.split
32
33     # Set up bindings for the browser.
34
35     list.focus
36     list.bind "Control-q", proc{exit}
37     list.bind "Control-c", proc{exit}
38     list.bind "Control-p", proc{
39       print "selection <", TkSelection.get, ">\n"
40     }
41
42     list.bind "Double-Button-1", proc{
43       for i in TkSelection.get.split
44         print "clicked ", i, "\n"
45         browse dir, i
46       end
47     }
48     $dirlist[dir] = list
49   end
50 end
51
52 def browse (dir, file)
53   file="#{dir}/#{file}"
54   if File.directory? file
55     browsedir(file)
56   else
57     if File.file? file
58       if ENV['EDITOR']
59         system format("%s %s&", ENV['EDITOR'], file)
60       else
61         system "xedit #{file}&"
62       end
63     else
64       STDERR.print "\"#{file}\" isn't a directory or regular file"
65     end
66   end
67 end
68
69 # Fill the listbox with a list of all the files in the directory (run
70 # the "ls" command to get that information).
71
72 if ARGV.length>0 
73   dir = ARGV[0]
74 else
75   dir="."
76 end
77
78 browsedir(dir)
79 Tk.mainloop