1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

New 'gems' command, as well as 'colorize' and 'highlight' helpers

This commit is contained in:
epitron 2011-04-22 01:12:32 -04:00
parent d264f3fd8f
commit e5fdef3335
2 changed files with 79 additions and 4 deletions

View file

@ -2,7 +2,7 @@ class Pry
class CommandBase
module CommandBaseHelpers
private
private
def gem_installed?(gem_name)
require 'rubygems'
@ -47,7 +47,7 @@ class Pry
# 1 => bright
# 0 => normal
#
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
[ :black, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
define_method "bright_#{color}" do |str|
Pry.color ? "\033[1;#{30+i}m#{str}\033[0m" : str
end
@ -58,11 +58,66 @@ class Pry
end
alias_method :magenta, :purple
alias_method :bright_magenta, :bright_purple
alias_method :grey, :bright_black
alias_method :gray, :bright_black
def bold(text)
Pry.color ? "\e[1m#{text}\e[0m" : text
end
#
# Colorize a string that has "color tags".
#
# Examples:
# puts colorize("<light_green><magenta>*</magenta> Hey mom! I am <light_blue>SO</light_blue> colored right now.</light_green>")
#
def colorize(string)
stack = []
# split the string into tags and literal strings
tokens = string.split(/(<\/?[\w\d_]+>)/)
tokens.delete_if { |token| token.size == 0 }
result = ""
tokens.each do |token|
# token is an opening tag!
if /<([\w\d_]+)>/ =~ token and respond_to?($1) #valid_tag?($1)
stack.push $1
# token is a closing tag!
elsif /<\/([\w\d_]+)>/ =~ token and respond_to?($1) # valid_tag?($1)
# if this color is on the stack somwehere...
if pos = stack.rindex($1)
# close the tag by removing it from the stack
stack.delete_at pos
else
raise "Error: tried to close an unopened color tag -- #{token}"
end
# token is a literal string!
else
color = (stack.last || "white")
#color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
result << send(color, token) # colorize the result
end
end
result
end
def highlight(string, regexp, highlight_color=:bright_yellow)
highlighted = string.gsub(regexp) { |match| "<#{highlight_color}>#{match}</#{highlight_color}>" }
end
# formatting
def heading(text)
text = "#{text}\n--"
@ -98,7 +153,7 @@ class Pry
simple_pager(text)
rescue Errno::EPIPE
end
#
# Create scrollable output via less!
#
@ -115,7 +170,7 @@ class Pry
# * Allow colour
# * Don't wrap lines longer than the screen
# * Quit immediately (without paging) if there's less than one screen of text.
#
#
# You can change these options by passing a hash to `lesspipe`, like so:
#
# lesspipe(:wrap=>false) { |less| less.puts essay.to_s }

View file

@ -269,6 +269,26 @@ e.g: gist -d my_method
end # gems
end
command "gems", "List/search installed gems. (Optional parameter: a regexp to limit the search)" do |arg|
gems = Gem.source_index.gems.values.group_by(&:name)
if arg
query = Regexp.new(arg, Regexp::IGNORECASE)
gems = gems.select { |gemname, specs| gemname =~ query }
end
gems.each do |gemname, specs|
versions = specs.map(&:version).sort.reverse.map(&:to_s)
versions = ["<bright_green>#{versions.first}</bright_green>"] +
versions[1..-1].map{|v| "<green>#{v}</green>" }
gemname = highlight(gemname, query) if query
result = "<white>#{gemname} <grey>(#{versions.join ', '})</grey>"
output.puts colorize(result)
end
end
command "whereami", "Show the code context for the session. Shows AROUND lines around the invocation line. AROUND defaults to 5 lines. " do |num|
file = target.eval('__FILE__')
line_num = target.eval('__LINE__')