~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-en/browse1

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
# browse --
 
4
# This script generates a directory browser, which lists the working 
 
5
# directory and allow you to open files or subdirectories by 
 
6
# double-clicking. 
 
7
 
 
8
require 'tk'
 
9
 
 
10
# Create a scrollbar on the right side of the main window and a listbox 
 
11
# on the left side.
 
12
 
 
13
listbox = TkListbox.new(nil, 'relief'=>'sunken', 
 
14
                        'width'=>20, 'height'=>20, 'setgrid'=>'yes') {|l|
 
15
  TkScrollbar.new(nil, 'command'=>proc{|*args| l.yview *args}) {|s|
 
16
    pack('side'=>'right', 'fill'=>'y')
 
17
    l.yscrollcommand(proc{|first,last| s.set(first,last)})
 
18
  }
 
19
 
 
20
  pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
 
21
}
 
22
 
 
23
root = TkRoot.new
 
24
root.minsize(1,1)
 
25
 
 
26
# The procedure below is invoked to open a browser on a given file;  if the 
 
27
# file is a directory then another instance of this program is invoked; if 
 
28
# the file is a regular file then the Mx editor is invoked to display 
 
29
# the file. 
 
30
 
 
31
def browse (dir, file)
 
32
  file = dir + File::Separator + file if dir != '.'
 
33
  type = File.ftype(file)
 
34
  if type == 'directory'
 
35
    system($0 + ' ' + file + ' &')
 
36
  else
 
37
    if type == 'file'
 
38
      if ENV['EDITOR']
 
39
        system(ENV['EDITOR'] + ' ' + file + ' &')
 
40
      else
 
41
        system('xedit ' + file + ' &')
 
42
      end
 
43
    else
 
44
      STDOUT.print "\"#{file}\" isn't a directory or regular file"
 
45
    end
 
46
  end
 
47
end
 
48
 
 
49
# Fill the listbox with a list of all the files in the directory (run 
 
50
# the "ls" command to get that information).
 
51
 
 
52
dir = ARGV[0] ?  ARGV[0] : '.'
 
53
open("|ls -a #{dir}", 'r'){|fid| fid.readlines}.each{|fname|
 
54
  listbox.insert('end', fname.chomp)
 
55
}
 
56
 
 
57
# Set up bindings for the browser.
 
58
 
 
59
Tk.bind_all('Control-c', proc{root.destroy})
 
60
listbox.bind('Double-Button-1', 
 
61
             proc{TkSelection.get.each{|f| browse dir, f}})
 
62
 
 
63
Tk.mainloop