Permalink
Browse files

merging black nights

  • Loading branch information...
regedor committed Apr 6, 2012
2 parents 6a8e2bf + aabefdd commit 34f83556aca007e79cad5e8dfd75b289b138c4f9
Showing with 194 additions and 128 deletions.
  1. +6 −6 Rakefile
  2. +2 −2 bin/polvomenu
  3. +140 −0 lib/polvo/io.rb
  4. +25 −13 lib/polvo/menu.rb
  5. +0 −94 lib/polvo/printer.rb
  6. +2 −2 lib/polvomenu.rb
  7. +1 −1 polvomenu.gemspec
  8. +18 −10 spec/polvo/{printer_spec.rb → io_spec.rb}
View
@@ -11,7 +11,7 @@ end
desc "Deletes built gem files"
task :clean do
- system "rm polvomenu-*.gem"
+ system "rm -f polvomenu-*.gem"
end
task :build => :clean do
@@ -31,16 +31,16 @@ end
namespace :install do
task :local => :build do
- system "gem uninstall polvomenu"
+ system "gem uninstall -x polvomenu"
system "sudo gem install polvo-menu-#{Polvo::Menu::VERSION}.gem"
end
desc "Cleans all the gem files, creates a new one, installs it "
task :remote do
- `rm polvomenu-*.gem `
- `gem build polvomenu.gemspec`
- `gem uninstall polvomenu`
- `gem install polvomenu-*.gem`
+ `rm polvomenu-*.gem `
+ `gem build polvomenu.gemspec`
+ `gem uninstall polvomenu`
+ `gem install polvomenu-*.gem`
end
end
View
@@ -10,6 +10,6 @@ require 'polvomenu'
# menu.render '.',{ 'title' => 'polvo menu' }
rootdirs = ARGV
-menu = Polvo::Menu.new(rootdirs)
-menu.render
+menu = Polvo::Menu.new rootdirs
+menu.render '.', :extra_answers => ['','0']
View
@@ -0,0 +1,140 @@
+module Polvo::IO
+ class << self
+
+ # --------------------------------------------
+ # Simple methods for outputing semantic text
+ # --------------------------------------------
+ def h1(str)
+ printf "\n === #{str.upcase} ===\n\n"
+ end
+
+ def h2(str)
+ self.h1(str)
+ end
+
+ def h3(str)
+ self.h1(str)
+ end
+
+ def h4(str)
+ self.h1(str)
+ end
+
+ def h5(str)
+ self.h1(str)
+ end
+
+ def p(str)
+ print str,"\n\n"
+ end
+
+ def notice(str)
+ puts "*** #{str}".green
+ end
+
+ def warning(str)
+ puts "*** #{str}".yellow
+ end
+
+ def error(str)
+ puts "*** #{str}".red
+ end
+
+
+ # ---------------------------------------------
+ # Simple methods that expect user interaction
+ # ---------------------------------------------
+ def wait(str='Press ENTER to continue.')
+ puts
+ self.warning(str)
+ STDIN.gets
+ end
+
+ def confirm(str="Continue")
+ printf "\n#{str} [y/n] ".yellow
+ input = STDIN.gets.chomp
+ return( %w{y Y yes YES}.any? {|v| v == input} )
+ puts
+ end
+
+ def ask(str)
+ printf "\n#{str}"
+ return STDIN.gets.chomp
+ puts
+ end
+
+ def menu(items,options = {})
+ # Clear screen, show title and drescription
+ question = options['question'] || 'Choice: '
+ self.clear unless options['noclear']
+ self.h1(options['title']) if options['title']
+ self.p(options['description']) if options['description']
+
+ # Print menu options
+ i = 0
+ items.each do |item|
+ if item.class == 'Hash'
+ opt = sprintf("%5s",item[:answer]).gsub!(/\s(\w)/,'[\1')
+ puts "#{opt}] #{item[:label]}"
+ else
+ opt = (sprintf "%5d",i+1).gsub!(/\s(\d)/,'[\1')
+ puts "#{opt}] #{item}"
+ i+=1
+ end
+ end
+
+ # Show warn (if any) and read user choice
+ self.warning(options['warn']) if options['warn']
+ options.delete 'warn'
+ choice = self.ask(question)
+
+ # Parse user choice
+ if option = parse_choice(choice,items.count+1,options)
+ return option
+ else
+ options['warn'] = "'#{choice}' is not a valid option!"
+ return self.menu(items,options)
+ end
+ end
+
+ def parse_choice(choice, max, options = {})
+ # Normal mode (accepts only integers > 0 and <= max, ex: '3')
+ if choice =~ /^\d+$/
+ int_choice = Integer(choice)
+ return int_choice if int_choice > 0 and int_choice <= max
+ end
+
+ # Extended mode (accepts integers followed by anything, ex: '3e file')
+ if options[:extended_options]
+ if choice =~ /^(\d+)(.*?)$/
+ int_choice = '\1'
+ args = '\2'
+ return { :option => '\1', :args => '\2' } if int_choice > 0 and int_choice <= max
+ end
+ end
+
+ # Extra answers (ex: '', '0', 'weird option')
+ if options[:extra_answers]
+ return choice if options[:extra_answers].include?(choice)
+ end
+
+ return false
+ end
+
+ # --------------------------------------
+ # More useful methods
+ # --------------------------------------
+ def clear
+ if ENV['POLVO_DEBUG']
+ puts "\n\n\n\n"
+ else
+ system('clear')
+ end
+ end
+
+ def debug(str)
+ puts str.magenta
+ end
+
+ end
+end
View
@@ -2,12 +2,12 @@ class Polvo::Menu
attr_accessor :editor
def initialize(rootdirs, options={})
self.editor = options[:editor] || ENV['EDITOR'] || 'vim'
- @rootdirs = rootdirs
+ @rootdirs = rootdirs.collect! { |d| d.sub(/\/*$/,'') }
end
def render(cur_dir = '.', options={})
- items_info = self.calc_menu(cur_dir)
- show_menu items_info, options
+ items_info = self.calc_menu cur_dir
+ return show_menu items_info, options
end
def calc_menu(cur_dir, options={})
@@ -31,16 +31,16 @@ def calc_menu(cur_dir, options={})
private
def exec_item(item, options={})
- Polvo::Printer.clear
+ Polvo::IO.clear
path = "#{item['rootdir']}/#{item['path']}"
if File.directory?(path)
return "Empty directory!" if Dir.entries(path).sort == ['.','..','info.menu']
return "Empty directory!" if Dir.entries(path).sort == ['.','..']
options['title'] = item['title']
- self.render item['path'], options
+ exit unless self.render item['path'], options
else
system(path)
- Polvo::Printer.wait
+ Polvo::IO.wait
end
return nil
end
@@ -55,20 +55,21 @@ def show_menu(items_info,options={})
menu_opts.push("#{title}\t"+"#{rootdir}/#{path}".magenta)
end
- choice = Polvo::Printer.menu(menu_opts,options)
+ choice = Polvo::IO.menu(menu_opts,options)
options.delete 'warn'
- Polvo::Printer.clear
+ Polvo::IO.clear
return true if choice == ''
return false if choice == '0'
+
if choice_valid?(choice,items_info.length+1)
int_choice = Integer(choice)
warn = exec_item(items_info[a[int_choice-1]],options)
options['warn'] = warn unless warn.nil?
else
options['warn'] = "'#{choice}' is not a valid option!"
end
- show_menu items_info, options
+ res = show_menu items_info, options
options.delete 'warn'
return
end
@@ -78,11 +79,11 @@ def choice_valid?(choice,max)
if choice == ''
return true
else
- #Polvo::Printer.warn("'#{choice}' is not a valid option!")
+ #Polvo::IO.warn("'#{choice}' is not a valid option!")
return false
end
unless int_choice < max and int_choice >= 0
- #Polvo::Printer.warn("'#{choice}' is not a valid option!")
+ #Polvo::IO.warn("'#{choice}' is not a valid option!")
return false
end
return true
@@ -98,7 +99,12 @@ def get_dir_info(rootdir,dir)
info['type'] = 'dir'
return info
else
- return { 'title' => File.basename(dir), 'type' => 'dir', 'path' => dir, 'rootdir' => rootdir }
+ return {
+ 'title' => File.basename(dir),
+ 'type' => 'dir',
+ 'path' => dir,
+ 'rootdir' => rootdir
+ }
end
end
@@ -111,7 +117,13 @@ def get_script_info(rootdir,file)
os = $1 || 'all'
end
#if filestr =~ /^#\sos:\s*([^\n]*)\s*\n/
- return { 'title' => title, 'os' => os, 'type' => 'script','path' => file, 'rootdir' => rootdir}
+ return {
+ 'title' => title,
+ 'os' => os,
+ 'type' => 'script',
+ 'path' => file,
+ 'rootdir' => rootdir
+ }
end
end
View
@@ -1,94 +0,0 @@
-module Polvo::Printer
- class << self
-
- # --------------------------------------------
- # Simple methods for outputing semantic text
- # --------------------------------------------
- def h1(str)
- printf "\n === #{str.upcase} ===\n\n"
- end
-
- def h2(str)
- self.h1(str)
- end
-
- def h3(str)
- self.h1(str)
- end
-
- def h4(str)
- self.h1(str)
- end
-
- def h5(str)
- self.h1(str)
- end
-
- def p(str)
- print str,"\n\n"
- end
-
- def notice(str)
- puts "*** #{str}".green
- end
-
- def warning(str)
- puts "*** #{str}".yellow
- end
-
- def error(str)
- puts "*** #{str}".red
- end
-
-
- # ---------------------------------------------
- # Simple methods that expect user interaction
- # ---------------------------------------------
- def wait(str='Press ENTER to continue.')
- puts
- self.warning(str)
- STDIN.gets
- end
-
- def confirm(str="Continue")
- printf "\n#{str} yes/no? ".yellow
- input = STDIN.gets.chomp
- return( %w{y Y yes YES}.any? {|v| v == input} )
- puts
- end
-
- def ask(str)
- printf "\n#{str}"
- return STDIN.gets.chomp
- puts
- end
-
- def menu(items,options = {})
- question = options['question'] || 'Choice: '
- self.clear unless options['noclear']
- self.h1(options['title']) if options['title']
- self.p(options['description']) if options['title']
- i = 0
- items.each do |item|
- opt = (sprintf "%5d",i+1).gsub!(/\s(\d)/,'[\1')
- puts "#{opt}] #{item}"
- i+=1
- end
- self.warn(options['warn']) if options['warn']
- return self.ask(question)
- end
-
-
- # --------------------------------------
- # More useful methods
- # --------------------------------------
- def clear
- system('clear')
- end
-
- def debug(str)
- puts str.magenta
- end
-
- end
-end
View
@@ -12,7 +12,7 @@ def require_relative(path)
require_relative 'polvo'
require_relative 'polvo/menu/version'
-require_relative 'polvo/printer'
+require_relative 'polvo/io'
require_relative 'polvo/menu'
-OUT = Polvo::Printer
+OUT = Polvo::IO
View
@@ -4,7 +4,7 @@ require 'polvomenu'
Gem::Specification.new do |s|
# Metadata
s.name = 'polvomenu'
- s.version = Polvo::VERSION
+ s.version = Polvo::Menu::VERSION
s.date = '2012-04-17'
s.authors = ["Group Buddies"]
s.email = 'regedor@groupbuddies.com'
Oops, something went wrong.

0 comments on commit 34f8355

Please sign in to comment.