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

use jist for gisting

This commit is contained in:
Conrad Irwin 2012-06-23 12:51:37 -07:00
parent b92a589eab
commit a79152c907

View file

@ -37,7 +37,7 @@ class Pry
end end
alias_command "file-mode", "shell-mode" alias_command "file-mode", "shell-mode"
create_command "gist", "Gist a method or expression history to github.", :requires_gem => "gist" do create_command "gist", "Gist a method or expression history to github.", :requires_gem => "jist" do
include Pry::Helpers::DocumentationHelpers include Pry::Helpers::DocumentationHelpers
@ -60,7 +60,7 @@ class Pry
attr_accessor :code_type attr_accessor :code_type
def setup def setup
require 'gist' require 'jist'
self.content = "" self.content = ""
self.code_type = :ruby self.code_type = :ruby
end end
@ -147,30 +147,25 @@ class Pry
# copy content to clipboard instead (only used with --clip flag) # copy content to clipboard instead (only used with --clip flag)
def perform_clipboard def perform_clipboard
Gist.copy(self.content) copy(self.content)
output.puts "Copied content to clipboard!" output.puts "Copied content to clipboard!"
end end
def perform_gist def perform_gist
type_map = { :ruby => "rb", :c => "c", :plain => "plain" } type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
# prevent Gist from exiting the session on error
begin
extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[self.code_type]}" extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[self.code_type]}"
if opts.present?(:lines) if opts.present?(:lines)
self.content = restrict_to_lines(content, opts[:l]) self.content = restrict_to_lines(content, opts[:l])
end end
link = Gist.write([:extension => extname, response = Jist.gist(self.content, :filename => extname,
:input => self.content], :public => !!opts[:p])
!opts[:p])
rescue SystemExit
end
if link if response
Gist.copy(link) copy(response['html_url'])
output.puts "Gist created at #{link} and added to clipboard." output.puts "Gist created at #{response['html_url']} and added to clipboard."
end end
end end
@ -197,6 +192,29 @@ class Pry
end end
content content
end 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 end
alias_command "clipit", "gist --clip" alias_command "clipit", "gist --clip"