Adding 'yadr' command for managing plugins (wip)
This commit is contained in:
198
bin/yadr/lib/git-style-binaries-0.1.11/lib/ext/colorize.rb
Normal file
198
bin/yadr/lib/git-style-binaries-0.1.11/lib/ext/colorize.rb
Normal file
@@ -0,0 +1,198 @@
|
||||
#
|
||||
# Colorize String class extension.
|
||||
#
|
||||
class String
|
||||
|
||||
#
|
||||
# Version string
|
||||
#
|
||||
COLORIZE_VERSION = '0.5.6'
|
||||
|
||||
#
|
||||
# Colors Hash
|
||||
#
|
||||
COLORS = {
|
||||
:black => 0,
|
||||
:red => 1,
|
||||
:green => 2,
|
||||
:yellow => 3,
|
||||
:blue => 4,
|
||||
:magenta => 5,
|
||||
:cyan => 6,
|
||||
:white => 7,
|
||||
:default => 9,
|
||||
|
||||
:light_black => 10,
|
||||
:light_red => 11,
|
||||
:light_green => 12,
|
||||
:light_yellow => 13,
|
||||
:light_blue => 14,
|
||||
:light_magenta => 15,
|
||||
:light_cyan => 16,
|
||||
:light_white => 17
|
||||
}
|
||||
|
||||
#
|
||||
# Modes Hash
|
||||
#
|
||||
MODES = {
|
||||
:default => 0, # Turn off all attributes
|
||||
#:bright => 1, # Set bright mode
|
||||
:underline => 4, # Set underline mode
|
||||
:blink => 5, # Set blink mode
|
||||
:swap => 7, # Exchange foreground and background colors
|
||||
:hide => 8 # Hide text (foreground color would be the same as background)
|
||||
}
|
||||
|
||||
protected
|
||||
|
||||
#
|
||||
# Set color values in new string intance
|
||||
#
|
||||
def set_color_parameters( params )
|
||||
if (params.instance_of?(Hash))
|
||||
@color = params[:color]
|
||||
@background = params[:background]
|
||||
@mode = params[:mode]
|
||||
@uncolorized = params[:uncolorized]
|
||||
self
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
#
|
||||
# Change color of string
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# puts "This is blue".colorize( :blue )
|
||||
# puts "This is light blue".colorize( :light_blue )
|
||||
# puts "This is also blue".colorize( :color => :blue )
|
||||
# puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
|
||||
# puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
|
||||
# puts "This is blue text on red".blue.on_red
|
||||
# puts "This is red on blue".colorize( :red ).on_blue
|
||||
# puts "This is red on blue and underline".colorize( :red ).on_blue.underline
|
||||
# puts "This is blue text on red".blue.on_red.blink
|
||||
#
|
||||
def colorize( params )
|
||||
|
||||
unless STDOUT.use_color
|
||||
return self unless STDOUT.isatty
|
||||
end
|
||||
return self if ENV['NO_COLOR']
|
||||
|
||||
begin
|
||||
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
|
||||
rescue LoadError
|
||||
raise 'You must gem install win32console to use color on Windows'
|
||||
end
|
||||
|
||||
color_parameters = {}
|
||||
|
||||
if (params.instance_of?(Hash))
|
||||
color_parameters[:color] = COLORS[params[:color]]
|
||||
color_parameters[:background] = COLORS[params[:background]]
|
||||
color_parameters[:mode] = MODES[params[:mode]]
|
||||
elsif (params.instance_of?(Symbol))
|
||||
color_parameters[:color] = COLORS[params]
|
||||
end
|
||||
|
||||
color_parameters[:color] ||= @color || 9
|
||||
color_parameters[:background] ||= @background || 9
|
||||
color_parameters[:mode] ||= @mode || 0
|
||||
|
||||
color_parameters[:uncolorized] ||= @uncolorized || self.dup
|
||||
|
||||
# calculate bright mode
|
||||
color_parameters[:color] += 50 if color_parameters[:color] > 10
|
||||
|
||||
color_parameters[:background] += 50 if color_parameters[:background] > 10
|
||||
|
||||
return "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Return uncolorized string
|
||||
#
|
||||
def uncolorize
|
||||
return @uncolorized || self
|
||||
end
|
||||
|
||||
#
|
||||
# Return true if sting is colorized
|
||||
#
|
||||
def colorized?
|
||||
return !@uncolorized.nil?
|
||||
end
|
||||
|
||||
#
|
||||
# Make some color and on_color methods
|
||||
#
|
||||
COLORS.each_key do | key |
|
||||
eval <<-"end_eval"
|
||||
def #{key.to_s}
|
||||
return self.colorize( :color => :#{key.to_s} )
|
||||
end
|
||||
def on_#{key.to_s}
|
||||
return self.colorize( :background => :#{key.to_s} )
|
||||
end
|
||||
end_eval
|
||||
end
|
||||
|
||||
#
|
||||
# Methods for modes
|
||||
#
|
||||
MODES.each_key do | key |
|
||||
eval <<-"end_eval"
|
||||
def #{key.to_s}
|
||||
return self.colorize( :mode => :#{key.to_s} )
|
||||
end
|
||||
end_eval
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
#
|
||||
# Return array of available modes used by colorize method
|
||||
#
|
||||
def modes
|
||||
keys = []
|
||||
MODES.each_key do | key |
|
||||
keys << key
|
||||
end
|
||||
keys
|
||||
end
|
||||
|
||||
#
|
||||
# Return array of available colors used by colorize method
|
||||
#
|
||||
def colors
|
||||
keys = []
|
||||
COLORS.each_key do | key |
|
||||
keys << key
|
||||
end
|
||||
keys
|
||||
end
|
||||
|
||||
#
|
||||
# Display color matrix with color names.
|
||||
#
|
||||
def color_matrix( txt = "[X]" )
|
||||
size = String.colors.length
|
||||
String.colors.each do | color |
|
||||
String.colors.each do | back |
|
||||
print txt.colorize( :color => color, :background => back )
|
||||
end
|
||||
puts " < #{color}"
|
||||
end
|
||||
String.colors.reverse.each_with_index do | back, index |
|
||||
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
16
bin/yadr/lib/git-style-binaries-0.1.11/lib/ext/core.rb
Normal file
16
bin/yadr/lib/git-style-binaries-0.1.11/lib/ext/core.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class Object
|
||||
def returning(value)
|
||||
yield(value)
|
||||
value
|
||||
end unless Object.respond_to?(:returning)
|
||||
end
|
||||
|
||||
class Symbol
|
||||
def to_proc
|
||||
Proc.new { |*args| args.shift.__send__(self, *args) }
|
||||
end
|
||||
end
|
||||
|
||||
class IO
|
||||
attr_accessor :use_color
|
||||
end
|
||||
@@ -0,0 +1,88 @@
|
||||
$:.unshift(File.dirname(__FILE__))
|
||||
require 'rubygems'
|
||||
|
||||
# Load the vendor gems
|
||||
$:.unshift(File.dirname(__FILE__) + "/../vendor/gems")
|
||||
%w(trollop).each do |library|
|
||||
begin
|
||||
require "#{library}/lib/#{library}"
|
||||
rescue LoadError
|
||||
begin
|
||||
require 'trollop'
|
||||
rescue LoadError
|
||||
puts "There was an error loading #{library}. Try running 'gem install #{library}' to correct the problem"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'ext/core'
|
||||
require 'ext/colorize'
|
||||
require 'git-style-binary/autorunner'
|
||||
Dir[File.dirname(__FILE__) + "/git-style-binary/helpers/*.rb"].each {|f| require f}
|
||||
|
||||
module GitStyleBinary
|
||||
|
||||
class << self
|
||||
include Helpers::NameResolver
|
||||
attr_accessor :current_command
|
||||
attr_accessor :primary_command
|
||||
attr_writer :known_commands
|
||||
|
||||
# If set to false GitStyleBinary will not automatically run at exit.
|
||||
attr_writer :run
|
||||
|
||||
# Automatically run at exit?
|
||||
def run?
|
||||
@run ||= false
|
||||
end
|
||||
|
||||
def parser
|
||||
@p ||= Parser.new
|
||||
end
|
||||
|
||||
def known_commands
|
||||
@known_commands ||= {}
|
||||
end
|
||||
|
||||
def load_primary
|
||||
unless @loaded_primary
|
||||
@loaded_primary = true
|
||||
primary_file = File.join(binary_directory, basename)
|
||||
load primary_file
|
||||
|
||||
if !GitStyleBinary.primary_command # you still dont have a primary load a default
|
||||
GitStyleBinary.primary do
|
||||
run do |command|
|
||||
educate
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def load_subcommand
|
||||
unless @loaded_subcommand
|
||||
@loaded_subcommand = true
|
||||
cmd_file = GitStyleBinary.binary_filename_for(GitStyleBinary.current_command_name)
|
||||
load cmd_file
|
||||
end
|
||||
end
|
||||
|
||||
def load_command_file(name, file)
|
||||
self.name_of_command_being_loaded = name
|
||||
load file
|
||||
self.name_of_command_being_loaded = nil
|
||||
end
|
||||
|
||||
# UGLY eek
|
||||
attr_accessor :name_of_command_being_loaded
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
at_exit do
|
||||
unless $! || GitStyleBinary.run?
|
||||
command = GitStyleBinary::AutoRunner.run
|
||||
exit 0
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
require 'git-style-binary/parser'
|
||||
|
||||
module GitStyleBinary
|
||||
class AutoRunner
|
||||
|
||||
def self.run(argv=ARGV)
|
||||
r = new
|
||||
r.run
|
||||
end
|
||||
|
||||
def run
|
||||
unless GitStyleBinary.run?
|
||||
if !GitStyleBinary.current_command
|
||||
GitStyleBinary.load_primary
|
||||
end
|
||||
GitStyleBinary.current_command.run
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,204 @@
|
||||
require 'git-style-binary'
|
||||
|
||||
module GitStyleBinary
|
||||
def self.command(&block)
|
||||
returning Command.new(:constraints => [block]) do |c|
|
||||
c.name ||= (GitStyleBinary.name_of_command_being_loaded || GitStyleBinary.current_command_name)
|
||||
GitStyleBinary.known_commands[c.name] = c
|
||||
|
||||
if !GitStyleBinary.current_command || GitStyleBinary.current_command.is_primary?
|
||||
GitStyleBinary.current_command = c
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.primary(&block)
|
||||
returning Primary.new(:constraints => [block]) do |c|
|
||||
c.name ||= (GitStyleBinary.name_of_command_being_loaded || GitStyleBinary.current_command_name)
|
||||
GitStyleBinary.known_commands[c.name] = c
|
||||
|
||||
GitStyleBinary.primary_command = c unless GitStyleBinary.primary_command
|
||||
GitStyleBinary.current_command = c unless GitStyleBinary.current_command
|
||||
end
|
||||
end
|
||||
|
||||
class Command
|
||||
class << self
|
||||
def defaults
|
||||
lambda do
|
||||
name_desc "#{command.full_name}\#{command.short_desc ? ' - ' + command.short_desc : ''}" # eval jit
|
||||
version_string = defined?(VERSION) ? VERSION : "0.0.1"
|
||||
version "#{version_string} (c) #{Time.now.year}"
|
||||
banner <<-EOS
|
||||
#{"SYNOPSIS".colorize(:red)}
|
||||
#{command.full_name.colorize(:light_blue)} #{all_options_string}
|
||||
|
||||
#{"SUBCOMMANDS".colorize(:red)}
|
||||
\#{GitStyleBinary.pretty_known_subcommands.join("\n ")}
|
||||
|
||||
See '#{command.full_name} help COMMAND' for more information on a specific command.
|
||||
EOS
|
||||
|
||||
opt :verbose, "verbose", :default => false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :constraints
|
||||
attr_reader :opts
|
||||
attr_accessor :name
|
||||
|
||||
def initialize(o={})
|
||||
o.each do |k,v|
|
||||
eval "@#{k.to_s}= v"
|
||||
end
|
||||
end
|
||||
|
||||
def parser
|
||||
@parser ||= begin
|
||||
p = Parser.new
|
||||
p.command = self
|
||||
p
|
||||
end
|
||||
end
|
||||
|
||||
def constraints
|
||||
@constraints ||= []
|
||||
end
|
||||
|
||||
def run
|
||||
GitStyleBinary.load_primary unless is_primary?
|
||||
GitStyleBinary.load_subcommand if is_primary? && running_subcommand?
|
||||
load_all_parser_constraints
|
||||
@opts = process_args_with_subcmd
|
||||
call_parser_run_block
|
||||
self
|
||||
end
|
||||
|
||||
def running_subcommand?
|
||||
GitStyleBinary.valid_subcommand?(GitStyleBinary.current_command_name)
|
||||
end
|
||||
|
||||
def load_all_parser_constraints
|
||||
@loaded_all_parser_constraints ||= begin
|
||||
load_parser_default_constraints
|
||||
load_parser_primary_constraints
|
||||
load_parser_local_constraints
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def load_parser_default_constraints
|
||||
parser.consume_all([self.class.defaults])
|
||||
end
|
||||
|
||||
def load_parser_primary_constraints
|
||||
parser.consume_all(GitStyleBinary.primary_command.constraints)
|
||||
end
|
||||
|
||||
def load_parser_local_constraints
|
||||
cur = GitStyleBinary.current_command # see, why isn't 'this' current_command?
|
||||
|
||||
unless self.is_primary? && cur == self
|
||||
# TODO TODO - the key lies in this function. figure out when you hav emore engergy
|
||||
# soo UGLY. see #process_parser! unify with that method
|
||||
# parser.consume_all(constraints) rescue ArgumentError
|
||||
parser.consume_all(cur.constraints)
|
||||
end
|
||||
end
|
||||
|
||||
def call_parser_run_block
|
||||
runs = GitStyleBinary.current_command.parser.runs
|
||||
|
||||
parser.run_callbacks(:before_run, self)
|
||||
parser.runs.last.call(self) # ... not too happy with this
|
||||
parser.run_callbacks(:after_run, self)
|
||||
end
|
||||
|
||||
def process_args_with_subcmd(args = ARGV, *a, &b)
|
||||
cmd = GitStyleBinary.current_command_name
|
||||
vals = process_args(args, *a, &b)
|
||||
parser.leftovers.shift if parser.leftovers[0] == cmd
|
||||
vals
|
||||
end
|
||||
|
||||
# TOOooootally ugly! why? bc load_parser_local_constraints doesn't work
|
||||
# when loading the indivdual commands because it depends on
|
||||
# #current_command. This really sucks and is UGLY.
|
||||
# the todo is to put in 'load_all_parser_constraints' and this works
|
||||
def process_parser!
|
||||
# load_all_parser_constraints
|
||||
|
||||
load_parser_default_constraints
|
||||
load_parser_primary_constraints
|
||||
# load_parser_local_constraints
|
||||
parser.consume_all(constraints)
|
||||
|
||||
# hack
|
||||
parser.consume {
|
||||
opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
|
||||
opt :help, "Show this message" unless @specs[:help] || @long["help"]
|
||||
resolve_default_short_options
|
||||
} # hack
|
||||
end
|
||||
|
||||
def process_args(args = ARGV, *a, &b)
|
||||
p = parser
|
||||
begin
|
||||
vals = p.parse args
|
||||
args.clear
|
||||
p.leftovers.each { |l| args << l }
|
||||
vals # ugly todo
|
||||
rescue Trollop::CommandlineError => e
|
||||
$stderr.puts "Error: #{e.message}."
|
||||
$stderr.puts "Try --help for help."
|
||||
exit(-1)
|
||||
rescue Trollop::HelpNeeded
|
||||
p.educate
|
||||
exit
|
||||
rescue Trollop::VersionNeeded
|
||||
puts p.version
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
def is_primary?
|
||||
false
|
||||
end
|
||||
|
||||
def argv
|
||||
parser.leftovers
|
||||
end
|
||||
|
||||
def short_desc
|
||||
parser.short_desc
|
||||
end
|
||||
|
||||
def full_name
|
||||
# ugly, should be is_primary?
|
||||
GitStyleBinary.primary_name == name ? GitStyleBinary.primary_name : GitStyleBinary.primary_name + "-" + name
|
||||
end
|
||||
|
||||
def die arg, msg=nil
|
||||
p = parser # create local copy
|
||||
Trollop.instance_eval { @p = p }
|
||||
Trollop::die(arg, msg)
|
||||
end
|
||||
|
||||
# Helper to return the option
|
||||
def [](k)
|
||||
opts[k]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Primary < Command
|
||||
def is_primary?
|
||||
true
|
||||
end
|
||||
def primary
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
module GitStyleBinary
|
||||
module Commands
|
||||
class Help
|
||||
# not loving this syntax, but works for now
|
||||
GitStyleBinary.command do
|
||||
short_desc "get help for a specific command"
|
||||
run do |command|
|
||||
|
||||
# this is slightly ugly b/c it has to muck around in the internals to
|
||||
# get information about commands other than itself. This isn't a
|
||||
# typical case
|
||||
self.class.send :define_method, :educate_about_command do |name|
|
||||
load_all_commands
|
||||
if GitStyleBinary.known_commands.has_key?(name)
|
||||
cmd = GitStyleBinary.known_commands[name]
|
||||
cmd.process_parser!
|
||||
cmd.parser.educate
|
||||
else
|
||||
puts "Unknown command '#{name}'"
|
||||
end
|
||||
end
|
||||
|
||||
if command.argv.size > 0
|
||||
command.argv.first == "help" ? educate : educate_about_command(command.argv.first)
|
||||
else
|
||||
educate
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,78 @@
|
||||
module GitStyleBinary
|
||||
module Helpers
|
||||
module NameResolver
|
||||
|
||||
def basename(filename=zero)
|
||||
File.basename(filename).match(/(.*?)(\-|$)/).captures.first
|
||||
end
|
||||
alias_method :primary_name, :basename
|
||||
|
||||
# checks the bin directory for all files starting with +basename+ and
|
||||
# returns an array of strings specifying the subcommands
|
||||
def subcommand_names(filename=zero)
|
||||
subfiles = Dir[File.join(binary_directory, basename + "-*")]
|
||||
cmds = subfiles.collect{|file| File.basename(file).sub(/^#{basename}-/, '')}.sort
|
||||
cmds += built_in_command_names
|
||||
cmds.uniq
|
||||
end
|
||||
|
||||
def binary_directory(filename=zero)
|
||||
File.dirname(filename)
|
||||
end
|
||||
|
||||
def built_in_commands_directory
|
||||
File.dirname(__FILE__) + "/../commands"
|
||||
end
|
||||
|
||||
def built_in_command_names
|
||||
Dir[built_in_commands_directory + "/*.rb"].collect{|f| File.basename(f.sub(/\.rb$/,''))}
|
||||
end
|
||||
|
||||
def list_subcommands(filename=zero)
|
||||
subcommand_names(filename).join(", ")
|
||||
end
|
||||
|
||||
# load first from users binary directory. then load built-in commands if
|
||||
# available
|
||||
def binary_filename_for(name)
|
||||
user_file = File.join(binary_directory, "#{basename}-#{name}")
|
||||
return user_file if File.exists?(user_file)
|
||||
built_in = File.join(built_in_commands_directory, "#{name}.rb")
|
||||
return built_in if File.exists?(built_in)
|
||||
user_file
|
||||
end
|
||||
|
||||
def current_command_name(filename=zero,argv=ARGV)
|
||||
current = File.basename(zero)
|
||||
first_arg = ARGV[0]
|
||||
return first_arg if valid_subcommand?(first_arg)
|
||||
return basename if basename == current
|
||||
current.sub(/^#{basename}-/, '')
|
||||
end
|
||||
|
||||
# returns the command name with the prefix if needed
|
||||
def full_current_command_name(filename=zero,argv=ARGV)
|
||||
cur = current_command_name(filename, argv)
|
||||
subcmd = cur == basename(filename) ? false : true # is this a subcmd?
|
||||
"%s%s%s" % [basename(filename), subcmd ? "-" : "", subcmd ? current_command_name(filename, argv) : ""]
|
||||
end
|
||||
|
||||
def valid_subcommand?(name)
|
||||
subcommand_names.include?(name)
|
||||
end
|
||||
|
||||
def zero
|
||||
$0
|
||||
end
|
||||
|
||||
def pretty_known_subcommands(theme=:long)
|
||||
GitStyleBinary.known_commands.collect do |k,cmd|
|
||||
next if k == basename
|
||||
cmd.process_parser!
|
||||
("%-s%s%-10s" % [basename, '-', k]).colorize(:light_blue) + ("%s " % [theme == :long ? "\n" : ""]) + ("%s" % [cmd.short_desc]) + "\n"
|
||||
end.compact.sort
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,37 @@
|
||||
module GitStyleBinary
|
||||
module Helpers
|
||||
module Pager
|
||||
|
||||
# by Nathan Weizenbaum - http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
|
||||
def run_pager
|
||||
return if RUBY_PLATFORM =~ /win32/
|
||||
return unless STDOUT.tty?
|
||||
STDOUT.use_color = true
|
||||
|
||||
read, write = IO.pipe
|
||||
|
||||
unless Kernel.fork # Child process
|
||||
STDOUT.reopen(write)
|
||||
STDERR.reopen(write) if STDERR.tty?
|
||||
read.close
|
||||
write.close
|
||||
return
|
||||
end
|
||||
|
||||
# Parent process, become pager
|
||||
STDIN.reopen(read)
|
||||
read.close
|
||||
write.close
|
||||
|
||||
ENV['LESS'] = 'FSRX' # Don't page if the input is short enough
|
||||
|
||||
Kernel.select [STDIN] # Wait until we have input before we start the pager
|
||||
pager = ENV['PAGER'] || 'less -erXF'
|
||||
exec pager rescue exec "/bin/sh", "-c", pager
|
||||
end
|
||||
|
||||
module_function :run_pager
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,223 @@
|
||||
module GitStyleBinary
|
||||
class Parser < Trollop::Parser
|
||||
attr_reader :runs, :callbacks
|
||||
attr_reader :short_desc
|
||||
attr_accessor :command
|
||||
|
||||
def initialize *a, &b
|
||||
super
|
||||
@runs = []
|
||||
setup_callbacks
|
||||
end
|
||||
|
||||
def setup_callbacks
|
||||
@callbacks = {}
|
||||
%w(run).each do |event|
|
||||
%w(before after).each do |time|
|
||||
@callbacks["#{time}_#{event}".to_sym] = []
|
||||
instance_eval "def #{time}_#{event}(&block);@callbacks[:#{time}_#{event}] << block;end"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run_callbacks(at, from)
|
||||
@callbacks[at].each {|c| c.call(from) }
|
||||
end
|
||||
|
||||
def banner s=nil; @banner = s if s; @banner end
|
||||
def short_desc s=nil; @short_desc = s if s; @short_desc end
|
||||
def name_desc s=nil; @name_desc = s if s; @name_desc end
|
||||
|
||||
# Set the theme. Valid values are +:short+ or +:long+. Default +:long+
|
||||
attr_writer :theme
|
||||
|
||||
def theme
|
||||
@theme ||= :long
|
||||
end
|
||||
|
||||
## Adds text to the help display.
|
||||
def text s; @order << [:text, s] end
|
||||
|
||||
def spec_names
|
||||
@specs.collect{|name, spec| spec[:long]}
|
||||
end
|
||||
|
||||
# should probably be somewhere else
|
||||
def load_all_commands
|
||||
GitStyleBinary.subcommand_names.each do |name|
|
||||
cmd_file = GitStyleBinary.binary_filename_for(name)
|
||||
GitStyleBinary.load_command_file(name, cmd_file)
|
||||
end
|
||||
end
|
||||
|
||||
## Print the help message to 'stream'.
|
||||
def educate(stream=$stdout)
|
||||
load_all_commands
|
||||
width # just calculate it now; otherwise we have to be careful not to
|
||||
# call this unless the cursor's at the beginning of a line.
|
||||
GitStyleBinary::Helpers::Pager.run_pager
|
||||
self.send("educate_#{theme}", stream)
|
||||
end
|
||||
|
||||
def educate_long(stream=$stdout)
|
||||
left = {}
|
||||
|
||||
@specs.each do |name, spec|
|
||||
left[name] =
|
||||
((spec[:short] ? "-#{spec[:short]}, " : "") +
|
||||
"--#{spec[:long]}" +
|
||||
case spec[:type]
|
||||
when :flag; ""
|
||||
when :int; "=<i>"
|
||||
when :ints; "=<i+>"
|
||||
when :string; "=<s>"
|
||||
when :strings; "=<s+>"
|
||||
when :float; "=<f>"
|
||||
when :floats; "=<f+>"
|
||||
end).colorize(:red)
|
||||
end
|
||||
|
||||
leftcol_width = left.values.map { |s| s.length }.max || 0
|
||||
rightcol_start = leftcol_width + 6 # spaces
|
||||
leftcol_start = 6
|
||||
leftcol_spaces = " " * leftcol_start
|
||||
|
||||
unless @order.size > 0 && @order.first.first == :text
|
||||
|
||||
if @name_desc
|
||||
stream.puts "NAME".colorize(:red)
|
||||
stream.puts "#{leftcol_spaces}"+ colorize_known_words(eval(%Q["#{@name_desc}"])) + "\n"
|
||||
stream.puts
|
||||
end
|
||||
|
||||
if @version
|
||||
stream.puts "VERSION".colorize(:red)
|
||||
stream.puts "#{leftcol_spaces}#@version\n"
|
||||
end
|
||||
|
||||
stream.puts
|
||||
|
||||
banner = colorize_known_words_array(wrap(eval(%Q["#{@banner}"]) + "\n", :prefix => leftcol_start)) if @banner # lazy banner
|
||||
stream.puts banner
|
||||
|
||||
stream.puts
|
||||
stream.puts "OPTIONS".colorize(:red)
|
||||
else
|
||||
stream.puts "#@banner\n" if @banner
|
||||
end
|
||||
|
||||
@order.each do |what, opt|
|
||||
if what == :text
|
||||
stream.puts wrap(opt)
|
||||
next
|
||||
end
|
||||
|
||||
spec = @specs[opt]
|
||||
stream.printf " %-#{leftcol_width}s\n", left[opt]
|
||||
desc = spec[:desc] +
|
||||
if spec[:default]
|
||||
if spec[:desc] =~ /\.$/
|
||||
" (Default: #{spec[:default]})"
|
||||
else
|
||||
" (default: #{spec[:default]})"
|
||||
end
|
||||
else
|
||||
""
|
||||
end
|
||||
stream.puts wrap(" %s" % [desc], :prefix => leftcol_start, :width => width - rightcol_start - 1 )
|
||||
stream.puts
|
||||
stream.puts
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def educate_short(stream=$stdout)
|
||||
left = {}
|
||||
|
||||
@specs.each do |name, spec|
|
||||
left[name] = "--#{spec[:long]}" +
|
||||
(spec[:short] ? ", -#{spec[:short]}" : "") +
|
||||
case spec[:type]
|
||||
when :flag; ""
|
||||
when :int; " <i>"
|
||||
when :ints; " <i+>"
|
||||
when :string; " <s>"
|
||||
when :strings; " <s+>"
|
||||
when :float; " <f>"
|
||||
when :floats; " <f+>"
|
||||
end
|
||||
end
|
||||
|
||||
leftcol_width = left.values.map { |s| s.length }.max || 0
|
||||
rightcol_start = leftcol_width + 6 # spaces
|
||||
leftcol_start = 0
|
||||
|
||||
unless @order.size > 0 && @order.first.first == :text
|
||||
stream.puts "#@version\n" if @version
|
||||
stream.puts colorize_known_words_array(wrap(eval(%Q["#{@banner}"]) + "\n", :prefix => leftcol_start)) if @banner # jit banner
|
||||
stream.puts "Options:"
|
||||
else
|
||||
stream.puts "#@banner\n" if @banner
|
||||
end
|
||||
|
||||
@order.each do |what, opt|
|
||||
if what == :text
|
||||
stream.puts wrap(opt)
|
||||
next
|
||||
end
|
||||
|
||||
spec = @specs[opt]
|
||||
stream.printf " %#{leftcol_width}s: ", left[opt]
|
||||
desc = spec[:desc] +
|
||||
if spec[:default]
|
||||
if spec[:desc] =~ /\.$/
|
||||
" (Default: #{spec[:default]})"
|
||||
else
|
||||
" (default: #{spec[:default]})"
|
||||
end
|
||||
else
|
||||
""
|
||||
end
|
||||
stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
def colorize_known_words_array(txts)
|
||||
txts.collect{|txt| colorize_known_words(txt)}
|
||||
end
|
||||
|
||||
def colorize_known_words(txt)
|
||||
txt = txt.gsub(/^([A-Z]+\s*)$/, '\1'.colorize(:red)) # all caps words on their own line
|
||||
txt = txt.gsub(/\b(#{bin_name})\b/, '\1'.colorize(:light_blue)) # the current command name
|
||||
txt = txt.gsub(/\[([^\s]+)\]/, "[".colorize(:magenta) + '\1'.colorize(:green) + "]".colorize(:magenta)) # synopsis options
|
||||
end
|
||||
|
||||
def consume(&block)
|
||||
cloaker(&block).bind(self).call
|
||||
end
|
||||
|
||||
def consume_all(blocks)
|
||||
blocks.each {|b| consume(&b)}
|
||||
end
|
||||
|
||||
def bin_name
|
||||
GitStyleBinary.full_current_command_name
|
||||
end
|
||||
|
||||
def all_options_string
|
||||
# '#{spec_names.collect(&:to_s).collect{|name| "[".colorize(:magenta) + "--" + name + "]".colorize(:magenta)}.join(" ")} COMMAND [ARGS]'
|
||||
'#{spec_names.collect(&:to_s).collect{|name| "[" + "--" + name + "]"}.join(" ")} COMMAND [ARGS]'
|
||||
end
|
||||
|
||||
def run(&block)
|
||||
@runs << block
|
||||
end
|
||||
|
||||
def action(name = :action, &block)
|
||||
block.call(self) if block
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user