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

gist: fix filenames from REPL + refactor + test

I glommed together too much on this commit. ☹
This commit is contained in:
☈king 2012-12-06 22:46:01 -06:00 committed by rking@sharpsaw.org
parent 9e6269c1da
commit 27bbc4383a
2 changed files with 134 additions and 54 deletions

View file

@ -1,4 +1,31 @@
class Pry
module Helpers
module Clipboard
# Copy a string to the clipboard.
#
# @param [String] content
#
# @copyright Copyright (c) 2008 Chris Wanstrath (MIT)
# @see https://github.com/defunkt/gist/blob/master/lib/gist.rb#L178
def self.copy(content)
cmd = case true
when system("type pbcopy > /dev/null 2>&1")
:pbcopy
when system("type xclip > /dev/null 2>&1")
:xclip
when system("type putclip > /dev/null 2>&1")
:putclip
end
if cmd
IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
end
content
end
end
end
Pry::Commands.create_command "gist" do
include Pry::Helpers::DocumentationHelpers
@ -17,43 +44,41 @@ class Pry
e.g: gist -i 1..10 # gist the input expressions from 1 to 10
e.g: gist -k show-method # gist the command show-method
e.g: gist -c Pry # gist the Pry class
e.g: gist -m hello_world --lines 2..-2 # gist from lines 2 to the second-last of the hello_world method
e.g: gist -m my_method --lines 2..-2 # gist from lines 2 to the second-last of the hello_world method
e.g: gist -m my_method --clip # Copy my_method source to clipboard, do not gist it.
USAGE
attr_accessor :content
attr_accessor :filename
attr_accessor :content, :filename
def setup
require 'jist'
self.content = ""
self.filename = "a.rb"
@content = ''
end
def from_pry_api api_obj
@content << api_obj.source << "\n"
@filename = api_obj.source
end
def options(opt)
ext ='ruby'
opt.on :login, "Authenticate the jist gem with GitHub"
opt.on :m, :method, "Gist a method's source.", :argument => true do |meth_name|
meth = get_method_or_raise(meth_name, target, {})
self.content << meth.source << "\n"
self.filename = meth.source_file
end
opt.on :d, :doc, "Gist a method's documentation.", :argument => true do |meth_name|
meth = get_method_or_raise(meth_name, target, {})
text.no_color do
self.content << process_comment_markup(meth.doc) << "\n"
@content << process_comment_markup(meth.doc) << "\n"
end
self.filename = meth.source_file + ".doc"
@filename = meth.source_file + ".doc"
end
opt.on :m, :method, "Gist a method's source.", :argument => true do |meth_name|
from_pry_api get_method_or_raise(meth_name, target, {})
end
opt.on :k, :command, "Gist a command's source.", :argument => true do |command_name|
command = find_command(command_name)
block = Pry::Method.new(command.block)
self.content << block.source << "\n"
self.filename = block.source_file
from_pry_api Pry::Method.new(command.block)
end
opt.on :c, :class, "Gist a class or module's source.", :argument => true do |class_name|
mod = Pry::WrappedModule.from_str(class_name, target)
self.content << mod.source << "\n"
self.filename = mod.source_file
from_pry_api Pry::WrappedModule.from_str(class_name, target)
end
opt.on :var, "Gist a variable's content.", :argument => true do |variable_name|
begin
@ -62,26 +87,26 @@ class Pry
raise CommandError, "Gist failed: Invalid variable name: #{variable_name}"
end
self.content << Pry.config.gist.inspecter.call(obj) << "\n"
@content << Pry.config.gist.inspecter.call(obj) << "\n"
end
opt.on :hist, "Gist a range of Readline history lines.", :optional_argument => 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"
@content << h[one_index_range(convert_to_range(range))].join("\n") << "\n"
end
opt.on :f, :file, "Gist a file.", :argument => true do |file|
self.content << File.read(File.expand_path(file)) << "\n"
self.filename = file
@content << File.read(File.expand_path(file)) << "\n"
@filename = file
end
opt.on :o, :out, "Gist entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
:as => Range, :default => -1 do |range|
range = convert_to_range(range)
range.each do |v|
self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
@content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
end
self.content << "\n"
@content << "\n"
end
opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it.", :default => false
opt.on :p, :public, "Create a public gist (default: false)", :default => false
@ -93,9 +118,9 @@ class Pry
Array(input_expressions).each_with_index do |code, index|
corrected_index = index + range.first
if code && code != ""
self.content << code
@content << code
if code !~ /;\Z/
self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
@content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
end
end
end
@ -105,7 +130,7 @@ class Pry
def process
return Jist.login! if opts.present?(:login)
if self.content =~ /\A\s*\z/
if @content =~ /\A\s*\z/
raise CommandError, "Found no code to gist."
end
@ -118,21 +143,33 @@ class Pry
# copy content to clipboard instead (only used with --clip flag)
def perform_clipboard
copy(self.content)
Pry::Helpers::Clipboard.copy(@content)
output.puts "Copied content to clipboard!"
end
def perform_gist
if opts.present?(:lines)
self.content = restrict_to_lines(content, opts[:l])
@content = restrict_to_lines(content, opts[:l])
end
response = Jist.gist(content, :filename => File.basename(filename),
response = Jist.gist(content, :filename => filename_or_fake,
:public => !!opts[:p])
if response
copy(response['html_url'])
output.puts "Gist created at #{response['html_url']} and added to clipboard."
url = response['html_url']
Pry::Helpers::Clipboard.copy(url)
output.puts 'Gist created at URL, which is now in the clipboard: ', url
end
end
def filename_or_fake
case @filename
when nil
'anonymous.rb' # not sure what triggers this condition
when '(pry)'
'repl.rb'
else
File.basename(@filename)
end
end
@ -156,28 +193,6 @@ class Pry
content
end
# Copy a string to the clipboard.
#
# @param [String] content
#
# @copyright Copyright (c) 2008 Chris Wanstrath (MIT)
# @see https://github.com/defunkt/gist/blob/master/lib/gist.rb#L178
def copy(content)
cmd = case true
when system("type pbcopy > /dev/null 2>&1")
:pbcopy
when system("type xclip > /dev/null 2>&1")
:xclip
when system("type putclip > /dev/null 2>&1")
:putclip
end
if cmd
IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
end
content
end
end
Pry::Commands.alias_command "clipit", "gist --clip"

View file

@ -0,0 +1,65 @@
require 'helper'
describe 'gist' do
# In absence of normal mocking, just monkeysmash these with no undoing after.
module Jist
class << self
def login!; $jist_logged_in = true end
def gist(*args)
$jist_gisted = args
{'html_url' => 'http://gist.blahblah'}
end
end
end
it 'nominally logs in' do
pry_eval 'gist --login'
$jist_logged_in.should.not.be.nil
end
module Pry::Helpers::Clipboard
def self.copy(content); $clipped_content = content end
end
EXAMPLE_REPL_METHOD = <<-EOT
# docdoc
def my_method
# line 1
'line 2'
line 3
Line.four
end
EOT
INVOCATIONS = {
:method => ['gist -m my_method' ],
:doc => ['gist -d my_method' ],
:input => ['a = 1', 'b = 2', 'gist -i 1..2' ],
:kommand => ['gist -k show-method' ],
:class => ['gist -c Pry' ],
:jist => ['jist -c Pry'],
:lines => ['gist -m my_method --lines 2..-2' ],
:cliponly => ['gist -m my_method --clip' ],
:clipit => ['clipit -m my_method' ],
}
run_case = proc {|sym| pry_eval *([EXAMPLE_REPL_METHOD] + INVOCATIONS[sym]) }
it 'deduces filenames' do
INVOCATIONS.keys.each do |e|
run_case.call(e)
if $jist_gisted
text, args = $jist_gisted
args[:filename].should.not == '(pry)'
end
$clipped_content.should.not.be.nil
$clipped_content = $jist_gisted = nil
end
end
it 'equates aliae' do
run_case.call(:clipit).should == run_case.call(:cliponly)
run_case.call(:jist).should == run_case.call(:class)
end
end