Skip to content

Commit

Permalink
Add copy command
Browse files Browse the repository at this point in the history
Closes #753
  • Loading branch information
Prajjwal committed Jan 14, 2025
1 parent 8241ec9 commit 1b409ba
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
14 changes: 10 additions & 4 deletions lib/irb/color_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ColorPrinter < ::PP
METHOD_INSPECT = Object.instance_method(:inspect)

class << self
def pp(obj, out = $>, width = screen_width)
q = ColorPrinter.new(out, width)
def pp(obj, out = $>, width = screen_width, colorize: true)
q = ColorPrinter.new(out, width, colorize: colorize)
q.guard_inspect_key {q.pp obj}
q.flush
out << "\n"
Expand All @@ -24,6 +24,12 @@ def screen_width
end
end

def initialize(out, width, colorize: true)
@colorize = colorize

super(out, width)
end

def pp(obj)
if String === obj
# Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
Expand All @@ -46,9 +52,9 @@ def text(str, width = nil)
when ',', '=>', '[', ']', '{', '}', '..', '...', /\A@\w+\z/
super(str, width)
when /\A#</, '=', '>'
super(Color.colorize(str, [:GREEN]), width)
super(@colorize ? Color.colorize(str, [:GREEN]) : str, width)
else
super(Color.colorize_code(str, ignore_error: true), width)
super(@colorize ? Color.colorize_code(str, ignore_error: true) : str, width)
end
end
end
Expand Down
53 changes: 53 additions & 0 deletions lib/irb/command/copy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module IRB
module Command
class Copy < Base
category "Workspace"
description "Copy command output to clipboard"

help_message(<<~HELP)
Usage: copy (command)
HELP

def execute(arg)
value = irb_context.workspace.binding.eval(arg)
output = irb_context.inspect_method.inspect_value(value, colorize: false)

if clipboard_available?
copy_to_clipboard(output)
else
warn "System clipboard not found"
end
rescue StandardError => e
warn "Error: #{e}"
end

private

def copy_to_clipboard(text)
IO.popen(clipboard_program, 'w') do |io|
io.write(text)
end

raise IOError.new("Copying to clipboard failed") unless $? == 0

puts "Copied to system clipboard"
end

def clipboard_program
@clipboard_program = "pbcopy" if executable?("pbcopy")
@clipboard_program = "xclip -selection clipboard" if executable?("xclip")
@clipboard_program
end

def executable?(command)
system("which #{command} > /dev/null 2>&1")
end

def clipboard_available?
!!clipboard_program
end
end
end
end
2 changes: 2 additions & 0 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ def irb_path=(path)
attr_reader :use_autocomplete
# A copy of the default <code>IRB.conf[:INSPECT_MODE]</code>
attr_reader :inspect_mode
# Inspector for the current context
attr_reader :inspect_method

# A copy of the default <code>IRB.conf[:PROMPT_MODE]</code>
attr_reader :prompt_mode
Expand Down
2 changes: 2 additions & 0 deletions lib/irb/default_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative "command/chws"
require_relative "command/context"
require_relative "command/continue"
require_relative "command/copy"
require_relative "command/debug"
require_relative "command/delete"
require_relative "command/disable_irb"
Expand Down Expand Up @@ -249,6 +250,7 @@ def load_command(command)
)

register(:cd, Command::CD)
register(:copy, Command::Copy)
end

ExtendCommand = Command
Expand Down
12 changes: 6 additions & 6 deletions lib/irb/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def init
end

# Proc to call when the input is evaluated and output in irb.
def inspect_value(v)
@inspect.call(v)
def inspect_value(v, colorize: true)
@inspect.call(v, colorize: colorize)
rescue => e
puts "An error occurred when inspecting the object: #{e.inspect}"

Expand All @@ -110,11 +110,11 @@ def inspect_value(v)
end

Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
Inspector.def_inspector([:p, :inspect]){|v|
Color.colorize_code(v.inspect, colorable: Color.colorable? && Color.inspect_colorable?(v))
Inspector.def_inspector([:p, :inspect]){|v, colorize: true|
Color.colorize_code(v.inspect, colorable: colorize && Color.colorable? && Color.inspect_colorable?(v))
}
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require_relative "color_printer"}){|v|
IRB::ColorPrinter.pp(v, +'').chomp
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require_relative "color_printer"}){|v, colorize: true|
IRB::ColorPrinter.pp(v, +'', colorize: colorize).chomp
}
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
begin
Expand Down
13 changes: 13 additions & 0 deletions test/irb/test_color_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def test_color_printer
end
end

def test_colorization_disabled
{
1 => "1\n",
"a\nb" => %["a\\nb"\n],
IRBTestColorPrinter.new('test') => "#<struct TestIRB::ColorPrinterTest::IRBTestColorPrinter a=\"test\">\n",
Ripper::Lexer.new('1').scan => "[#<Ripper::Lexer::Elem: on_int@1:0 END token: \"1\">]\n",
Class.new{define_method(:pretty_print){|q| q.text("[__FILE__, __LINE__, __ENCODING__]")}}.new => "[__FILE__, __LINE__, __ENCODING__]\n",
}.each do |object, result|
actual = with_term { IRB::ColorPrinter.pp(object, '', colorize: false) }
assert_equal(result, actual, "Case: IRB::ColorPrinter.pp(#{object.inspect}, '')")
end
end

private

def with_term
Expand Down

0 comments on commit 1b409ba

Please sign in to comment.