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

Support (and recommend) ansicon [Fixes #606]

This commit is contained in:
Conrad Irwin 2012-06-23 15:06:32 -07:00
parent bce6c99f2f
commit acb2964b3c
5 changed files with 21 additions and 8 deletions

View file

@ -188,11 +188,14 @@ if Pry::Helpers::BaseHelpers.jruby?
end
end
if Pry::Helpers::BaseHelpers.windows?
if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
begin
require 'win32console'
# The mswin and mingw versions of pry require win32console, so this should
# only fail on jruby (where win32console doesn't work).
# Instead we'll recommend ansicon, which does.
rescue LoadError
warn "You should: `gem install win32console` for better auto-indent and color support."
warn "For a better pry experience, please use ansicon: http://adoxa.3eeweb.com/ansicon/"
end
end

View file

@ -77,7 +77,7 @@ end
\____/ \________________________|
EOS
if defined?(Win32::Console)
if windows_ansi?
move_up = proc { |n| "\e[#{n}F" }
else
move_up = proc { |n| "\e[#{n}A\e[0G" }

View file

@ -70,7 +70,7 @@ class Pry
end
def use_ansi_codes?
defined?(Win32::Console) || ENV['TERM'] && ENV['TERM'] != "dumb"
windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
end
def colorize_code(code)
@ -100,6 +100,11 @@ class Pry
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end
# are we able to use ansi on windows?
def windows_ansi?
defined?(Win32::Console) || ENV['ANSICON']
end
# are we on Jruby platform?
def jruby?
RbConfig::CONFIG['ruby_install_name'] == 'jruby'

View file

@ -296,7 +296,7 @@ class Pry
cols = cols.to_i
lines = cols != 0 ? (full_line.length / cols + 1) : 1
if defined?(Win32::Console)
if Pry::Helpers::BaseHelpers.windows_ansi?
move_up = "\e[#{lines}F"
move_down = "\e[#{lines}E"
else
@ -320,7 +320,12 @@ class Pry
# Otherwise try to use the environment (this may be out of date due
# to window resizing, but it's better than nothing).
[ENV["ROWS"], ENV["COLUMNS"]]
[ENV["ROWS"], ENV["COLUMNS"],
# If the user is running within ansicon, then use the screen size
# that it reports (same caveats apply as with ROWS and COLUMNS)
ENV['ANSICON'] =~ /\((.*)x(.*)\)/ && [$2, $1]
]
].detect do |(_, cols)|
cols.to_i > 0
end

View file

@ -138,7 +138,7 @@ class Pry
# Clear the line before starting Pry. This fixes the issue discussed here:
# https://github.com/pry/pry/issues/566
if Pry.config.auto_indent
Kernel.print defined?(Win32::Console) ? "\e[0F" : "\e[0G"
Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
end
# Enter the matrix
@ -250,7 +250,7 @@ class Pry
config.should_trap_interrupts = Helpers::BaseHelpers.jruby?
config.disable_auto_reload = false
config.command_prefix = ""
config.auto_indent = true
config.auto_indent = Helpers::BaseHelpers.use_ansi_codes?
config.correct_indent = true
config.collision_warning = false