Skip to content

Instantly share code, notes, and snippets.

@carlosagp
Created May 3, 2022 18:40
Show Gist options
  • Save carlosagp/b0225fc3ab7c63e1fcec7bba31832ba8 to your computer and use it in GitHub Desktop.
Save carlosagp/b0225fc3ab7c63e1fcec7bba31832ba8 to your computer and use it in GitHub Desktop.
Copy and Paste from the rails terminal
# frozen_string_literal: true
# In {rails_app}/config/initializers/terminal_utils.rb
Rails.configuration.to_prepare do
require "terminal/utils"
module Rails::ConsoleMethods
include Terminal::Utils
end
end
# frozen_string_literal: true
# In {rails_app}/lib/terminal/utils.rb
require "rbconfig"
module Terminal
module Utils
def pbcopy(input)
str = input.to_s
# IO.popen('pbcopy', 'w') { |f| f << str } if pbcopy_command
`echo #{str} | #{pbcopy_command}` if pbcopy_command
str
end
def pbpaste
`#{pbpaste_command}`
end
def current_os
@current_os ||= (
host_os = RbConfig::CONFIG['host_os']
case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
end
)
end
def pbcopy_command
return "tr -d '\n' | pbcopy" if current_os == :macosx
return "tr -d '\n' | xsel -ib" if current_os == :linux
end
def pbpaste_command
return "pbpaste" if current_os == :macosx
return "xsel -op" if current_os == :linux
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment