polvomenu 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # title: Polvo terminal command
3
+ # os: all
4
+ #
5
+ # This is very cool program.
6
+ require 'polvomenu'
7
+
8
+ # TODO
9
+ # Este binario deve extendido com flags, usage etc
10
+ # menu.render '.',{ 'title' => 'polvo menu' }
11
+
12
+ rootdirs = ARGV
13
+ menu = Polvo::Menu.new(rootdirs)
14
+ menu.render
15
+
@@ -0,0 +1,13 @@
1
+ module Polvo
2
+ VERSION = "0.0.1"
3
+ end
4
+
5
+ require 'rubygems'
6
+ require 'pp'
7
+ require 'colorize'
8
+
9
+ require_relative 'polvo/printer'
10
+ require_relative 'polvo/menu'
11
+
12
+ #menu = Polvo::Menu.new ["/Users/regedor/teste/rootdir1", "/Users/regedor/teste/rootdir2"]
13
+ #menu.render '.',{ 'title' => 'polvo menu' }
@@ -0,0 +1,117 @@
1
+ class Polvo::Menu
2
+ attr_accessor :editor
3
+ def initialize(rootdirs, options={})
4
+ self.editor = options[:editor] || ENV['EDITOR'] || 'vim'
5
+ @rootdirs = rootdirs
6
+ end
7
+
8
+ def render(cur_dir = '.', options={})
9
+ items_info = self.calc_menu(cur_dir)
10
+ show_menu items_info, options
11
+ end
12
+
13
+ def calc_menu(cur_dir, options={})
14
+ items_info = Hash.new
15
+ previous_type = 'dir'
16
+ @rootdirs.sort.each do |rootdir|
17
+ next unless File.exists? "#{rootdir}/#{cur_dir}"
18
+
19
+ Dir.foreach("#{rootdir}/#{cur_dir}") do |item|
20
+ next if item == '.' or item == '..' or item == 'info.menu'
21
+ path = "#{cur_dir}/#{item}"
22
+ items_info[path] = if File.directory? "#{rootdir}/#{path}"
23
+ get_dir_info(rootdir,path)
24
+ else
25
+ get_script_info(rootdir,path)
26
+ end
27
+ end
28
+ end
29
+ return items_info
30
+ end
31
+
32
+ private
33
+ def exec_item(item, options={})
34
+ Polvo::Printer.clear
35
+ path = "#{item['rootdir']}/#{item['path']}"
36
+ if File.directory?(path)
37
+ return "Empty directory!" if Dir.entries(path).sort == ['.','..','info.menu']
38
+ return "Empty directory!" if Dir.entries(path).sort == ['.','..']
39
+ options['title'] = item['title']
40
+ self.render item['path'], options
41
+ else
42
+ system(path)
43
+ Polvo::Printer.wait
44
+ end
45
+ return nil
46
+ end
47
+
48
+ def show_menu(items_info,options={})
49
+ menu_opts = Array.new
50
+ a = items_info.keys.sort
51
+ a.each do |item|
52
+ title = items_info[item]['title']
53
+ path = items_info[item]['path']
54
+ rootdir = items_info[item]['rootdir']
55
+ menu_opts.push("#{title}\t"+"#{rootdir}/#{path}".magenta)
56
+ end
57
+
58
+ choice = Polvo::Printer.menu(menu_opts,options)
59
+ options.delete 'warn'
60
+ Polvo::Printer.clear
61
+
62
+ return true if choice == ''
63
+ return false if choice == '0'
64
+ if choice_valid?(choice,items_info.length+1)
65
+ int_choice = Integer(choice)
66
+ warn = exec_item(items_info[a[int_choice-1]],options)
67
+ options['warn'] = warn unless warn.nil?
68
+ else
69
+ options['warn'] = "'#{choice}' is not a valid option!"
70
+ end
71
+ show_menu items_info, options
72
+ options.delete 'warn'
73
+ return
74
+ end
75
+
76
+ def choice_valid?(choice,max)
77
+ int_choice = Integer(choice) rescue
78
+ if choice == ''
79
+ return true
80
+ else
81
+ #Polvo::Printer.warn("'#{choice}' is not a valid option!")
82
+ return false
83
+ end
84
+ unless int_choice < max and int_choice >= 0
85
+ #Polvo::Printer.warn("'#{choice}' is not a valid option!")
86
+ return false
87
+ end
88
+ return true
89
+ end
90
+
91
+
92
+ def get_dir_info(rootdir,dir)
93
+ if File.exist? "#{rootdir}/#{dir}/exec.bash"
94
+ return get_script_info rootdir,"#{dir}/exec.bash"
95
+ elsif File.exist? "#{rootdir}/#{dir}/info.menu"
96
+ info = get_script_info rootdir,"#{dir}/info.menu"
97
+ info['path'] = dir
98
+ info['type'] = 'dir'
99
+ return info
100
+ else
101
+ return { 'title' => File.basename(dir), 'type' => 'dir', 'path' => dir, 'rootdir' => rootdir }
102
+ end
103
+ end
104
+
105
+ def get_script_info(rootdir,file)
106
+ filestr = IO.read("#{rootdir}/#{file}")
107
+ if filestr =~ /^#\stitle:\s*([^\n]*)\s*\n/
108
+ title = $1 || ''
109
+ end
110
+ if filestr =~ /^#\sos:\s*([^\n]*)\s*\n/
111
+ os = $1 || 'all'
112
+ end
113
+ #if filestr =~ /^#\sos:\s*([^\n]*)\s*\n/
114
+ return { 'title' => title, 'os' => os, 'type' => 'script','path' => file, 'rootdir' => rootdir}
115
+ end
116
+
117
+ end
@@ -0,0 +1,78 @@
1
+ module Polvo::Printer
2
+ def self.h1(str)
3
+ printf "\n === "
4
+ printf str.upcase
5
+ printf " ===\n\n"
6
+ end
7
+
8
+ def self.h2(str)
9
+ self.h1(str)
10
+ end
11
+
12
+ def self.h3(str)
13
+ self.h1(str)
14
+ end
15
+
16
+
17
+ def self.h4(str)
18
+ self.h1(str)
19
+ end
20
+
21
+ def self.h5(str)
22
+ self.h1(str)
23
+ end
24
+
25
+ def self.p(str)
26
+ print "\n",str,"\n\n"
27
+ end
28
+
29
+ def self.error(str)
30
+ str = '*** '+str
31
+ puts str.red
32
+ end
33
+
34
+ def self.warn(str)
35
+ str = '*** '+str
36
+ puts str.yellow
37
+ end
38
+
39
+ def self.ok(str)
40
+ str = '*** '+str
41
+ puts str.green
42
+ end
43
+
44
+ def self.wait(str='Press ENTER to continue.')
45
+ puts
46
+ self.warn(str)
47
+ STDIN.gets
48
+ end
49
+
50
+ def self.debug(str)
51
+ puts str.magenta
52
+ end
53
+
54
+ def self.ask(str)
55
+ printf "\n#{str}"
56
+ return STDIN.gets.chomp
57
+ puts
58
+ end
59
+
60
+ def self.clear
61
+ puts
62
+ #system('clear')
63
+ end
64
+
65
+ def self.menu(items,options = {})
66
+ question = options['question'] || 'Choice: '
67
+ self.clear unless options['noclear']
68
+ self.h1(options['title']) if options['title']
69
+ i = 0
70
+ items.each do |item|
71
+ opt = (sprintf "%5d",i+1).gsub!(/\s(\d)/,'[\1')
72
+ puts "#{opt}] #{item}"
73
+ i+=1
74
+ end
75
+ self.warn(options['warn']) if options['warn']
76
+ return self.ask(question)
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polvomenu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Miguel Regedor
9
+ - André Santos
10
+ - Group Buddies
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-04-17 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: ! 'Directory-based command-line menu '
17
+ email: regedor@groupbuddies.com
18
+ executables:
19
+ - polvomenu
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/polvomenu
24
+ - lib/polvo/menu.rb
25
+ - lib/polvo/printer.rb
26
+ - lib/polvo.rb
27
+ homepage: http://rubygems.org/gems/polvomenu
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.17
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Directory-based command-line menu
51
+ test_files: []