-
Notifications
You must be signed in to change notification settings - Fork 0
/
ob_rmenu_folder.rb
executable file
·87 lines (73 loc) · 2.02 KB
/
ob_rmenu_folder.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/ruby
require 'pathname'
def main(args)
recursive = false
folder = '/'
args.each do |arg|
if arg == '-r'
recursive = true
elsif Pathname.new(arg).directory?
folder = arg
end
end
puts '<openbox_pipe_menu>'
get_folder_items(folder, recursive, true)
puts '</openbox_pipe_menu>'
end
def get_folders(dir)
get_items(dir).select do |item| item.directory? end
end
def get_files(dir)
get_items(dir).select do |item| item.file? end
end
def get_items(dir)
return Pathname.new(dir).children.select do |c| c.readable? and not c.symlink? end.sort_by do |c| c.to_s.downcase end
end
def get_item_name(item)
return to_xml(Pathname.new(item).basename.to_s)
end
def to_xml(string)
return string.gsub('&', '&').gsub('"', '"')
end
def get_folder_label(item, name, recursive)
if recursive
puts '<menu id="%s" label="%s">' % [item, name]
puts get_item_label(item, 'open')
get_folder_items(item, recursive)
puts '</menu>'
else
puts get_item_label(item, name)
end
end
def get_item_label(item, text)
return "<item label=\"#{text}\"><action name=\"Execute\"><execute>xdg-open \"#{to_xml(item.to_s)}\"</execute></action></item>"
end
def get_folder_items(dir, recursive, categories = false)
all_folders = get_folders(dir)
#puts '<separator label="%s (%d)"/>' % [ get_item_name(dir), all_folders.length]
if all_folders.length > 50
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.chars.each do |c|
folders = all_folders.select do |item| get_item_name(item).start_with?(c, c.downcase) end
if not folders.empty?
puts "<menu id=\"#{dir.to_s}-#{c}\" label=\"#{c} (#{folders.length}) \">"
folders.each do |item|
name = get_item_name(item)
if(name.start_with?(c, c.downcase))
get_folder_label(item, name, recursive)
end
end
puts '</menu>'
end
end
else
all_folders.each do |item|
name = get_item_name(item)
get_folder_label(item, name, recursive)
end
get_files(dir).each do |item|
name = get_item_name(item)
puts get_item_label(item, name)
end
end
end
main(ARGV)