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 8require 'tk' 9 10# Create a scrollbar on the right side of the main window and a listbox 11# on the left side. 12 13listbox = 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 23root = TkRoot.new 24root.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 31def 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 47end 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 52dir = ARGV[0] ? ARGV[0] : '.' 53open("|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 59Tk.bind_all('Control-c', proc{root.destroy}) 60listbox.bind('Double-Button-1', 61 proc{TkSelection.get.each{|f| browse dir, f}}) 62 63Tk.mainloop 64