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

Add trailing_newline option to log #49

Open
wants to merge 2 commits into
base: master
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
2 changes: 2 additions & 0 deletions examples/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
10.times do |i|
spinner.log("[#{i}] Task")
sleep(0.1)
spinner.log("Multi\nLine\nLog\n", trailing_newline: false)
sleep(0.1)
spinner.spin
end

Expand Down
15 changes: 13 additions & 2 deletions lib/tty/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,26 @@ def update(tokens)
#
# @param [String] text
# the message to log out
# @param [Boolean] trailing_newline
# automatically add a trailing new line if message is missing one
# (using LF)
#
# @api public
def log(text)
def log(text, trailing_newline: true)
synchronize do
cleared_text = text.to_s.lines.map do |line|
TTY::Cursor.clear_line + line
end.join

write("#{cleared_text}#{"\n" unless cleared_text.end_with?("\n")}", false)
if trailing_newline
write(
"#{cleared_text}#{"\n" unless cleared_text.end_with?("\n")}",
false
)
else
write(cleared_text, false)
end

render
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@
"\e[2K\e[1Gbar\n"
].join)
end

context "when trailing_newline is false" do
it "logs a message above a spinner" do
spinner = TTY::Spinner.new(output: output)

2.times do
spinner.log "foo\r", trailing_newline: false
spinner.spin
end
output.rewind

expect(output.read).to eq([
"\e[2K\e[1Gfoo\r",
"\e[1G|",
"\e[1G|",
"\e[2K\e[1Gfoo\r",
"\e[1G/",
"\e[1G/"
].join)
end
end
end