Skip to content

Commit

Permalink
upgraded gist and save-file
Browse files Browse the repository at this point in the history
gist now supports:
--hist for gisting readline history
-k for gisting classes
-o for gisting evaluation results
A newline is also inserted after each element of the gist

save-file now supports:
--k for classes
-o for evaluation results
(--hist isn't requires as that's suppored by 'hist' command itself)
  • Loading branch information
banister committed May 25, 2012
1 parent 5b5313b commit f2f421b
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions lib/pry/default_commands/input_and_output.rb
Expand Up @@ -37,7 +37,10 @@ module DefaultCommands
end
alias_command "file-mode", "shell-mode"

create_command "gist", "Gist a method or expression history to github.", :requires_gem => "gist", :shellwords => false do
create_command "gist", "Gist a method or expression history to github.", :requires_gem => "gist" do

include Pry::Helpers::DocumentationHelpers

banner <<-USAGE
Usage: gist [OPTIONS] [METH]
Gist method (doc or source) or input expression to github.
Expand All @@ -49,6 +52,8 @@ module DefaultCommands
e.g: gist -m hello_world --lines 2..-2
USAGE

command_options :shellwords => false

attr_accessor :content
attr_accessor :code_type

Expand All @@ -61,24 +66,47 @@ def setup
def options(opt)
opt.on :m, :method, "Gist a method's source.", true do |meth_name|
meth = get_method_or_raise(meth_name, target, {})
self.content << meth.source
self.content << meth.source << "\n"
self.code_type = meth.source_type
end
opt.on :d, :doc, "Gist a method's documentation.", true do |meth_name|
meth = get_method_or_raise(meth_name, target, {})
text.no_color do
self.content << process_comment_markup(meth.doc, self.code_type)
self.content << process_comment_markup(meth.doc, self.code_type) << "\n"
end
self.code_type = :plain
end
opt.on :c, :command, "Gist a command's source.", true do |command_name|
command = find_command(command_name)
block = Pry::Method.new(find_command(command_name).block)
self.content << block.source
self.content << block.source << "\n"
end
opt.on :k, :class, "Gist a class's source.", true do |class_name|
mod = Pry::WrappedModule.from_str(class_name, target)
self.content << mod.source << "\n"
end
opt.on :hist, "Gist a range of Readline history lines.", :optional => true, :as => Range, :default => -20..-1 do |range|
h = Pry.history.to_a
self.content << h[one_index_range(convert_to_range(range))].join("\n") << "\n"
end

opt.on :f, :file, "Gist a file.", true do |file|
self.content << File.read(File.expand_path(file))
self.content << File.read(File.expand_path(file)) << "\n"
end
opt.on :o, :out, "Gist entries from Pry's output result history. Takes an index or range.", :optional => true,
:as => Range, :default => -5..-1 do |range|
range = convert_to_range(range)

output_results = []
range.each do |v|
self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
end

self.content << "\n"
end
# opt.on :s, :string, "Gist a string", true do |string|
# self.content << string << "\n"
# end
opt.on :p, :public, "Create a public gist (default: false)", :default => false
opt.on :l, :lines, "Only gist a subset of lines.", :optional => true, :as => Range, :default => 1..-1
opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional => true,
Expand Down Expand Up @@ -170,11 +198,23 @@ def setup
self.content = ""
end

def convert_to_range(n)
if !n.is_a?(Range)
(n..n)
else
n
end
end

def options(opt)
opt.on :m, :method, "Save a method's source.", true do |meth_name|
meth = get_method_or_raise(meth_name, target, {})
self.content << meth.source
end
opt.on :k, :class, "Save a class's source.", true do |class_name|
mod = Pry::WrappedModule.from_str(class_name, target)
self.content << mod.source
end
opt.on :c, :command, "Save a command's source.", true do |command_name|
command = find_command(command_name)
block = Pry::Method.new(find_command(command_name).block)
Expand All @@ -184,6 +224,17 @@ def options(opt)
self.content << File.read(File.expand_path(file))
end
opt.on :l, :lines, "Only save a subset of lines.", :optional => true, :as => Range, :default => 1..-1
opt.on :o, :out, "Save entries from Pry's output result history. Takes an index or range.", :optional => true,
:as => Range, :default => -5..-1 do |range|
range = convert_to_range(range)

output_results = []
range.each do |v|
self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
end

self.content << "\n"
end
opt.on :i, :in, "Save entries from Pry's input expression history. Takes an index or range.", :optional => true,
:as => Range, :default => -5..-1 do |range|
input_expressions = _pry_.input_array[range] || []
Expand Down

0 comments on commit f2f421b

Please sign in to comment.