From 815cd299c19e11424bf7f09e99b1abc6b0ae01a6 Mon Sep 17 00:00:00 2001 From: Ronan Potage Date: Fri, 7 Oct 2022 15:15:02 -0700 Subject: [PATCH 1/2] Add trailing_newline option to log --- examples/log.rb | 2 ++ lib/tty/spinner.rb | 11 +++++++++-- spec/unit/log_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/log.rb b/examples/log.rb index 5dd5cd0..19b5aa3 100644 --- a/examples/log.rb +++ b/examples/log.rb @@ -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 diff --git a/lib/tty/spinner.rb b/lib/tty/spinner.rb index 55f48c4..79e557d 100644 --- a/lib/tty/spinner.rb +++ b/lib/tty/spinner.rb @@ -504,15 +504,22 @@ 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 diff --git a/spec/unit/log_spec.rb b/spec/unit/log_spec.rb index 250e2b8..1e98d37 100644 --- a/spec/unit/log_spec.rb +++ b/spec/unit/log_spec.rb @@ -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 { + spinner.log "foo\r", trailing_newline: false + spinner.spin + } + 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 From f690495f73071118ce1f6ebb8423840a1002eaf0 Mon Sep 17 00:00:00 2001 From: Ronan Potage Date: Fri, 7 Oct 2022 15:24:49 -0700 Subject: [PATCH 2/2] Lint --- lib/tty/spinner.rb | 8 ++++++-- spec/unit/log_spec.rb | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/tty/spinner.rb b/lib/tty/spinner.rb index 79e557d..c339755 100644 --- a/lib/tty/spinner.rb +++ b/lib/tty/spinner.rb @@ -505,7 +505,8 @@ 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) + # automatically add a trailing new line if message is missing one + # (using LF) # # @api public def log(text, trailing_newline: true) @@ -515,7 +516,10 @@ def log(text, trailing_newline: true) end.join if trailing_newline - write("#{cleared_text}#{"\n" unless cleared_text.end_with?("\n")}", false) + write( + "#{cleared_text}#{"\n" unless cleared_text.end_with?("\n")}", + false + ) else write(cleared_text, false) end diff --git a/spec/unit/log_spec.rb b/spec/unit/log_spec.rb index 1e98d37..b5bcdc1 100644 --- a/spec/unit/log_spec.rb +++ b/spec/unit/log_spec.rb @@ -62,10 +62,10 @@ it "logs a message above a spinner" do spinner = TTY::Spinner.new(output: output) - 2.times { + 2.times do spinner.log "foo\r", trailing_newline: false spinner.spin - } + end output.rewind expect(output.read).to eq([