Sha256: b3d5d47648a1c3d7f042be388dd8794cb70c45142fde6dfcc12c2f0b0423e0f8

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

require 'ffi-tk'
require 'find'

class FileTree < Tk::Tile::Treeview
  KB = 1024.0
  MB = KB * KB
  GB = MB * KB

  def populate_roots
    ## Create the tree and set it up
    configure columns: %w[fullpath type size], displaycolumns: %w[size]
    heading('#0', text: 'Directory Structure')
    heading('size', text: 'File Size')
    column('size', stretch: 0, width: 70)
    bind('<<TreeviewOpen>>'){|event|
      begin
        populate_tree(focus_item)
      rescue Exception => ex
        p ex
      end

      true
    }

    Tk.execute('file', 'volumes').sort.each do |volume|
      node = insert(nil, :end, text: volume, values: [volume, 'directory'])
      populate_tree(node)
    end
  end

  def populate_tree(node)
    return if node.set(:type) != 'directory'

    path = node.set(:fullpath)
    delete(*node.children.to_a)

    Dir.glob File.join(path, '*') do |path|
      name, type = File.basename(path), File.ftype(path)

      item = insert(node, :end, text: name, values: [path, type])

      if type == 'directory'
        ## Make it so that this node is openable
        dir_item = item.insert(0, text: 'dummy')
        dir_item.options(text: name)
      elsif type == 'file'
        size = File.size(path)

        if size >= GB
          size = '%.1f GB' % [size / GB]
        elsif size >= MB
          size = '%.1f MB' % [size / MB]
        elsif size >= KB
          size = '%.1f KB' % [size / KB]
        else
          size = '%.1f bytes' % [size]
        end

        item.set(:size, size)
      end
    end
  end
end

tree = FileTree.new
tree.bind('<Escape>'){ Tk.exit }
tree.pack expand: true, fill: :both
tree.focus
tree.populate_roots

Tk.mainloop

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffi-tk-2010.08.23 example/tile/treeview.rb
ffi-tk-2010.08 example/tile/treeview.rb
ffi-tk-2010.06 example/tile/treeview.rb
ffi-tk-2010.03 example/tile/treeview.rb
ffi-tk-2010.02 example/tile/treeview.rb
ffi-tk-2010.01 example/tile/treeview.rb
ffi-tk-2010.01.02 example/tile/treeview.rb
ffi-tk-2009.12.14 example/tile/treeview.rb
ffi-tk-2009.11.29 example/tile/treeview.rb