From 5e517a4b096c85caed97765fb57ec33432a6df3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eren=20Hat=C4=B1rnaz?= Date: Tue, 14 Feb 2017 15:47:26 +0300 Subject: [PATCH 1/3] Fixes line-wrapping problem with colored output --- lib/thor/line_editor/readline.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/thor/line_editor/readline.rb b/lib/thor/line_editor/readline.rb index 32446e333..f8d07b1fe 100644 --- a/lib/thor/line_editor/readline.rb +++ b/lib/thor/line_editor/readline.rb @@ -17,7 +17,7 @@ def readline if complete = completion_proc ::Readline.completion_proc = complete end - ::Readline.readline(prompt, add_to_history?) + ::Readline.readline(normalized_prompt, add_to_history?) else super end @@ -25,6 +25,15 @@ def readline private + def normalized_prompt + ansi_colors = @prompt[0..-5].scan(/\e\[[0-9;]*m/) + .map { |color| "\001" + color + "\002" } + return @prompt if ansi_colors.empty? + + @prompt = @prompt.gsub(/\e\[[0-9;]*m/, "") + "#{ansi_colors.join}#{prompt}\001\e[0m\002" + end + def add_to_history? options.fetch(:add_to_history, true) end From c01ae2bac959c450d28f7f71832cdc1503cae2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eren=20Hat=C4=B1rnaz?= Date: Tue, 21 Feb 2017 00:00:59 +0300 Subject: [PATCH 2/3] Fixes multi-line method chaining style for old ruby versions --- lib/thor/line_editor/readline.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/thor/line_editor/readline.rb b/lib/thor/line_editor/readline.rb index f8d07b1fe..df5854d5e 100644 --- a/lib/thor/line_editor/readline.rb +++ b/lib/thor/line_editor/readline.rb @@ -26,7 +26,7 @@ def readline private def normalized_prompt - ansi_colors = @prompt[0..-5].scan(/\e\[[0-9;]*m/) + ansi_colors = @prompt[0..-5].scan(/\e\[[0-9;]*m/) \ .map { |color| "\001" + color + "\002" } return @prompt if ansi_colors.empty? From 995eff8032890982ec8036fa28a27fd5208fa656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eren=20Hat=C4=B1rnaz?= Date: Fri, 24 Feb 2017 20:34:05 +0300 Subject: [PATCH 3/3] Add test for fixes line wrapping issue --- spec/line_editor/readline_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/line_editor/readline_spec.rb b/spec/line_editor/readline_spec.rb index b2b0712d6..1cd400b93 100644 --- a/spec/line_editor/readline_spec.rb +++ b/spec/line_editor/readline_spec.rb @@ -35,6 +35,12 @@ expect(editor.readline).to eq("foo") end + it "with colored prompt message" do + expect(::Readline).to receive(:readline).with("\001\e[32m\002> \001\e[0m\002", true).and_return("foo") + editor = Thor::LineEditor::Readline.new("\e[32m> \e[0m", {}) + expect(editor.readline).to eq("foo") + end + it "provides tab completion when given a limited_to option" do expect(::Readline).to receive(:readline) expect(::Readline).to receive(:completion_proc=) do |proc| @@ -65,5 +71,6 @@ editor = Thor::LineEditor::Readline.new("Password: ", :echo => false) expect(editor.readline).to eq("secret") end + end end