summaryrefslogtreecommitdiff
path: root/lib/irb.rb
diff options
context:
space:
mode:
authortomoya ishida <tomoyapenguin@gmail.com>2023-03-02 22:53:39 +0900
committergit <svn-admin@ruby-lang.org>2023-03-02 13:53:44 +0000
commit556439613aa6a0d05a9884a305ae43f48cd2c5f0 (patch)
tree469ee829bd90c78d5bfac75f9e1086a03e717cc8 /lib/irb.rb
parentda6ac30d1e505bd15b8c118816a9990b5cb072a7 (diff)
[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
Diffstat (limited to 'lib/irb.rb')
-rw-r--r--lib/irb.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index 96dbaaa65b..cef6ebc952 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -463,6 +463,10 @@ module IRB
# 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
+ PROMPT_MAIN_TRUNCATE_LENGTH = 32
+ PROMPT_MAIN_TRUNCATE_OMISSION = '...'.freeze
+ CONTROL_CHARACTERS_PATTERN = "\x00-\x1F".freeze
+
# Creates a new irb session
def initialize(workspace = nil, input_method = nil)
@context = Context.new(self, workspace, input_method)
@@ -775,6 +779,15 @@ module IRB
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:
p = prompt.dup
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
@@ -782,9 +795,9 @@ module IRB
when "N"
@context.irb_name
when "m"
- @context.main.to_s
+ truncate_prompt_main(@context.main.to_s)
when "M"
- @context.main.inspect
+ truncate_prompt_main(@context.main.inspect)
when "l"
ltype
when "i"