Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes line-wrapping problem with colored output #549

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/thor/line_editor/readline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ 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
end

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
Expand Down
7 changes: 7 additions & 0 deletions spec/line_editor/readline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -65,5 +71,6 @@
editor = Thor::LineEditor::Readline.new("Password: ", :echo => false)
expect(editor.readline).to eq("secret")
end

end
end