[ruby/irb] Handle long inspect and control character in prompt
string (https://github.com/ruby/irb/pull/528) * Handle long inspect and control characters in prompt string * Add constants for prompt truncate length, omission and replace pattern * Simply compare string instead of regexp in prompt truncation test
This commit is contained in:
parent
da6ac30d1e
commit
556439613a
17
lib/irb.rb
17
lib/irb.rb
@ -463,6 +463,10 @@ module IRB
|
|||||||
# be parsed as :assign and echo will be suppressed, but the latter is
|
# be parsed as :assign and echo will be suppressed, but the latter is
|
||||||
# parsed as a :method_add_arg and the output won't be suppressed
|
# parsed as a :method_add_arg and the output won't be suppressed
|
||||||
|
|
||||||
|
PROMPT_MAIN_TRUNCATE_LENGTH = 32
|
||||||
|
PROMPT_MAIN_TRUNCATE_OMISSION = '...'.freeze
|
||||||
|
CONTROL_CHARACTERS_PATTERN = "\x00-\x1F".freeze
|
||||||
|
|
||||||
# Creates a new irb session
|
# Creates a new irb session
|
||||||
def initialize(workspace = nil, input_method = nil)
|
def initialize(workspace = nil, input_method = nil)
|
||||||
@context = Context.new(self, workspace, input_method)
|
@context = Context.new(self, workspace, input_method)
|
||||||
@ -775,6 +779,15 @@ module IRB
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def truncate_prompt_main(str) # :nodoc:
|
||||||
|
str = str.tr(CONTROL_CHARACTERS_PATTERN, ' ')
|
||||||
|
if str.size <= PROMPT_MAIN_TRUNCATE_LENGTH
|
||||||
|
str
|
||||||
|
else
|
||||||
|
str[0, PROMPT_MAIN_TRUNCATE_LENGTH - PROMPT_MAIN_TRUNCATE_OMISSION.size] + PROMPT_MAIN_TRUNCATE_OMISSION
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def prompt(prompt, ltype, indent, line_no) # :nodoc:
|
def prompt(prompt, ltype, indent, line_no) # :nodoc:
|
||||||
p = prompt.dup
|
p = prompt.dup
|
||||||
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
|
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
|
||||||
@ -782,9 +795,9 @@ module IRB
|
|||||||
when "N"
|
when "N"
|
||||||
@context.irb_name
|
@context.irb_name
|
||||||
when "m"
|
when "m"
|
||||||
@context.main.to_s
|
truncate_prompt_main(@context.main.to_s)
|
||||||
when "M"
|
when "M"
|
||||||
@context.main.inspect
|
truncate_prompt_main(@context.main.inspect)
|
||||||
when "l"
|
when "l"
|
||||||
ltype
|
ltype
|
||||||
when "i"
|
when "i"
|
||||||
|
@ -656,6 +656,23 @@ module TestIRB
|
|||||||
$VERBOSE = verbose
|
$VERBOSE = verbose
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_prompt_main_escape
|
||||||
|
irb = IRB::Irb.new(IRB::WorkSpace.new("main\a\t\r\n"))
|
||||||
|
assert_equal("irb(main )>", irb.prompt('irb(%m)>', nil, 1, 1))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_prompt_main_inspect_escape
|
||||||
|
main = Struct.new(:inspect).new("main\\n\nmain")
|
||||||
|
irb = IRB::Irb.new(IRB::WorkSpace.new(main))
|
||||||
|
assert_equal("irb(main\\n main)>", irb.prompt('irb(%M)>', nil, 1, 1))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_prompt_main_truncate
|
||||||
|
irb = IRB::Irb.new(IRB::WorkSpace.new("a" * 100))
|
||||||
|
assert_equal('irb(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.prompt('irb(%m)>', nil, 1, 1))
|
||||||
|
assert_equal('irb("aaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.prompt('irb(%M)>', nil, 1, 1))
|
||||||
|
end
|
||||||
|
|
||||||
def test_lineno
|
def test_lineno
|
||||||
input = TestInputMethod.new([
|
input = TestInputMethod.new([
|
||||||
"\n",
|
"\n",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user