diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index 90bd9c0f..41c408b6 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -65,6 +65,10 @@ class Pry end end + def use_ansi_codes? + ENV['TERM'] != "dumb" + end + def colorize_code(code) if Pry.color CodeRay.scan(code, :ruby).term diff --git a/lib/pry/indent.rb b/lib/pry/indent.rb index 6f70a334..343b9adc 100644 --- a/lib/pry/indent.rb +++ b/lib/pry/indent.rb @@ -9,7 +9,7 @@ class Pry # will be indented or un-indented by correctly. # class Indent - # Array containing all the indentation levels. + # String containing the spaces to be inserted before the next line. attr_reader :indent_level # The amount of spaces to insert for each indent level. @@ -169,19 +169,18 @@ class Pry (last_token =~ /^[)\]}\/]$/ || STATEMENT_END_TOKENS.include?(last_kind)) end - # Fix the indentation for closing tags (notably 'end'). This method currently - # does nothing on Windows, since `tput` isn't supported. + # Fix the indentation for closing tags (notably 'end'). # # @param [String] full_line The full line of input, including the prompt. # def correct_indentation(full_line) - if !Kernel.const_defined?(:Win32) - # The whitespace is used to "clear" the current line so existing - # characters don't show up. - spaces = ' ' * full_line.length + rws, cols = Readline.get_screen_size + lines = full_line.length / cols + move_up = "\e[#{lines+1}F" + kill_line = "\e[0K" + move_down = "\e[#{lines}E" - $stdout.write(`tput sc` + `tput cuu1` + full_line + spaces + `tput rc`) - end + $stdout.print(move_up, full_line, kill_line, move_down) end end end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 383bf2d0..0505163e 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -185,7 +185,7 @@ class Pry config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST config.hooks = DEFAULT_HOOKS config.input_stack = [] - config.color = true + config.color = Pry::Helpers::BaseHelpers.use_ansi_codes? config.pager = true config.system = DEFAULT_SYSTEM config.editor = default_editor_for_platform diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 17bd1ad4..9de71962 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -310,7 +310,10 @@ class Pry if !@command_processor.valid_command?(val, target) && Pry.config.auto_indent && input == Readline val = @indent.indent(val) - @indent.correct_indentation(current_prompt + val) + + if Pry::Helpers::BaseHelpers.use_ansi_codes? + @indent.correct_indentation(current_prompt + val) + end end Pry.history << val.dup