Class: CmdDirWidget

Inherits:
Gtk::Frame
  • Object
show all
Defined in:
lib/unixcmd/dirview.rb

Instance Method Summary collapse

Constructor Details

#initializeCmdDirWidget

Returns a new instance of CmdDirWidget.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/unixcmd/dirview.rb', line 13

def initialize
    super

    model = Gtk::ListStore.new String, String, String, String, String, String, Gdk::Pixbuf, Integer 
    @view = Gtk::TreeView.new model
    
    Gtk::VScrollbar.new @view.vadjustment

    @view.signal_connect('row-activated') { |view, path, column| open(path) }

    scrollwnd = Gtk::ScrolledWindow.new
    scrollwnd.set_policy Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC

    scrollwnd.add @view

    add_child Gtk::Builder.new, scrollwnd

    checker = Gtk::CellRendererToggle.new
    checker.signal_connect('toggled') { |s, path| iter = @view.model.get_iter(path); iter[7] = ~iter[7]; }

    @view.signal_connect('key-press-event') { |s, event| key_pressed(event) }

    cols = [ Gtk::TreeViewColumn.new('', checker, :active => 7 ),
             Gtk::TreeViewColumn.new('Name'),
             Gtk::TreeViewColumn.new('Ext',  Gtk::CellRendererText.new, :text => 2),
             Gtk::TreeViewColumn.new('Size', Gtk::CellRendererText.new, :text => 3),
             Gtk::TreeViewColumn.new('Date', Gtk::CellRendererText.new, :text => 4),
             Gtk::TreeViewColumn.new('Attr', Gtk::CellRendererText.new, :text => 5) ]

    renderer = Gtk::CellRendererPixbuf.new
    cols[1].pack_start renderer, false
    cols[1].add_attribute renderer, 'pixbuf', 6

    renderer = Gtk::CellRendererText.new
    cols[1].pack_start renderer, false
    cols[1].add_attribute renderer, 'text', 1

    (1..cols.count).each do |i|
        cols[i-1].sort_column_id = i
    end

    cols.drop(2).each { |col| col.expand = false }
    cols[0].expand = false 
    cols[1].expand = true

    cols.each { |col| @view.append_column col }

    @view.selection.mode = Gtk::SELECTION_MULTIPLE

    model.set_sort_column_id 2

    self.focus_chain = [ @view ]

    grab_focus

    chdir Pathname.new '~'
end

Instance Method Details

#backObject



74
# File 'lib/unixcmd/dirview.rb', line 74

def back() chdir @path + '..' end

#chdir(path) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/unixcmd/dirview.rb', line 132

def chdir(path)
    old_path = @path == nil ? Pathname.new('') : @path 
    @path = path

    set_label @path.to_s

    reload

    if path == old_path.parent
        dirname = old_path.basename

        @view.model.each do |model, path, iter|
            if iter.get_value(0) == dirname.to_s
                @view.selection.select_path path
                @view.set_cursor path, nil, false
                break
            end
        end

    end

    if @view.selection.count_selected_rows == 0
        @view.selection.select_iter @view.model.iter_first
        @view.set_cursor @view.model.iter_first.path, nil, false
    end

    grab_focus

    signal_emit 'dir-changed'
end

#expanded_pathObject



72
# File 'lib/unixcmd/dirview.rb', line 72

def expanded_path() @path.expand_path end

#key_pressed(event) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/unixcmd/dirview.rb', line 163

def key_pressed(event)
    if event.keyval == Gdk::Keyval::GDK_Insert
        @view.columns[0].visible = true

        last = nil

        @view.selection.selected_each do |model, path, iter|
            @view.selection.unselect_iter iter
            iter[7] = ~iter[7] if iter[0] != '..'
            last = iter
        end

        @view.selection.select_iter last if last && last.next!

        return true
    elsif event.keyval == Gdk::Keyval::GDK_space
        return true
    end
end

#open(path) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/unixcmd/dirview.rb', line 120

def open(path)
    iter = @view.model.get_iter(path)

    file = iter.get_value 0

    if (expanded_path + Pathname.new(file)).directory?
        chdir @path + Pathname.new(file)
    else
        Launchy.open (expanded_path + Pathname.new(file)).to_s
    end
end

#pathObject



71
# File 'lib/unixcmd/dirview.rb', line 71

def path() @path end

#reloadObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/unixcmd/dirview.rb', line 76

def reload
    model = @view.model

    model.clear

    @view.columns[0].set_visible false

    files = expanded_path.entries.sort 

    first_row = nil

    files.each do |file|
        full_path = expanded_path + file
        file_icon_path = MIME::Types.get_icon_path full_path

        next if file.to_s == '.'
        next if file.to_s == '..' && expanded_path.root?
        next if file.to_s[0] == '.' && file.to_s != '..' 

        row = model.append

        first_row = row if first_row.nil?

        row[0] = file.to_s

        if full_path.directory?
            row[1] = file.to_s + '/'
            row[3] = '<DIR>'
        else
            row[1] = file.basename('.*').to_s
            row[3] = full_path.exist? ? full_path.size.to_i.to_szstr : 0.to_szstr
        end

        row[2] = file.extname
        row[4] = Time.at(full_path.mtime).strftime '%x %R' if full_path.exist?
        row[5] = full_path.stat.mode.to_rwx if full_path.exist?
        row[6] = Gdk::Pixbuf.new file_icon_path.to_s
        row[7] = 0
    end

    @view.selection.select_iter first_row
    @view.scroll_to_cell first_row.path, @view.columns[1], false, 0.0, 0.0
end

#signal_do_dir_changedObject



187
# File 'lib/unixcmd/dirview.rb', line 187

def signal_do_dir_changed() nil end

#viewObject



11
# File 'lib/unixcmd/dirview.rb', line 11

def view() @view end