Commit 4584e2d
Changed files (2)
lib
elelem
spec
elelem
lib/elelem/terminal.rb
@@ -7,7 +7,11 @@ module Elelem
@quiet = quiet
@dots_thread = nil
@at_line_start = true
- setup_completion unless @quiet
+ setup_completion unless quiet?
+ end
+
+ def quiet?
+ @quiet
end
def ask(prompt)
@@ -21,7 +25,7 @@ module Elelem
end
def markdown(text)
- return if @quiet || blank?(text)
+ return if quiet? || blank?(text)
gap
width = $stdout.winsize[1] rescue 80
@@ -35,27 +39,23 @@ module Elelem
end
def print(text)
- return if blank?(text)
+ return if quiet? || blank?(text)
- unless @quiet
- stop_dots
- $stdout.print text
- end
+ stop_dots
+ $stdout.print text
@at_line_start = false
end
def say(text)
- return if blank?(text)
+ return if quiet? || blank?(text)
- unless @quiet
- stop_dots
- $stdout.puts text
- end
+ stop_dots
+ $stdout.puts text
@at_line_start = true
end
def newline(n: 1)
- n.times { $stdout.puts("") }
+ n.times { $stdout.puts("") } unless quiet?
@at_line_start = true
end
@@ -65,13 +65,13 @@ module Elelem
end
def display_file(path, fallback: nil)
- return if @quiet
+ return if quiet?
system("bat", "--paging=never", path) || say(fallback || path)
end
def waiting
- return if @quiet
+ return if quiet?
@dots_thread = Thread.new do
loop do
spec/elelem/terminal_spec.rb
@@ -3,54 +3,109 @@
require "spec_helper"
RSpec.describe Elelem::Terminal do
- subject(:terminal) { described_class.new(quiet: true) }
+ subject(:terminal) { described_class.new(quiet: false) }
describe "#gap" do
- it "outputs newline when not at line start" do
- terminal.instance_variable_set(:@at_line_start, false)
- expect { terminal.gap }.to output("\n").to_stdout
+ it "outputs newline after print" do
+ expect {
+ terminal.print("hello")
+ terminal.gap
+ }.to output("hello\n").to_stdout
end
- it "outputs nothing when already at line start" do
- terminal.instance_variable_set(:@at_line_start, true)
- expect { terminal.gap }.not_to output.to_stdout
+ it "outputs nothing after say" do
+ expect {
+ terminal.say("hello")
+ terminal.gap
+ }.to output("hello\n").to_stdout
end
- it "is idempotent - calling twice produces one newline" do
- terminal.instance_variable_set(:@at_line_start, false)
- expect { terminal.gap; terminal.gap }.to output("\n").to_stdout
+ it "outputs nothing after newline" do
+ expect {
+ terminal.print("hello")
+ terminal.newline
+ terminal.gap
+ }.to output("hello\n").to_stdout
end
- it "stops dots thread when called" do
- dots_thread = double("thread")
- allow(dots_thread).to receive(:kill)
- terminal.instance_variable_set(:@dots_thread, dots_thread)
- terminal.gap
- expect(terminal.instance_variable_get(:@dots_thread)).to be_nil
+ it "is idempotent" do
+ expect {
+ terminal.print("hello")
+ terminal.gap
+ terminal.gap
+ terminal.gap
+ }.to output("hello\n").to_stdout
+ end
+
+ it "outputs nothing in quiet mode" do
+ quiet_terminal = described_class.new(quiet: true)
+ expect {
+ quiet_terminal.print("hello")
+ quiet_terminal.gap
+ }.not_to output.to_stdout
+ end
+ end
+
+ describe "#say" do
+ it "outputs text with newline" do
+ expect { terminal.say("hello") }.to output("hello\n").to_stdout
+ end
+
+ it "outputs nothing for blank text" do
+ expect { terminal.say("") }.not_to output.to_stdout
+ end
+
+ it "outputs nothing in quiet mode" do
+ quiet_terminal = described_class.new(quiet: true)
+ expect { quiet_terminal.say("hello") }.not_to output.to_stdout
end
end
- describe "@at_line_start tracking" do
- it "starts true (cursor at line start)" do
- expect(terminal.instance_variable_get(:@at_line_start)).to be true
+ describe "#print" do
+ it "outputs text without newline" do
+ expect { terminal.print("hello") }.to output("hello").to_stdout
end
- it "becomes true after say" do
- terminal.instance_variable_set(:@at_line_start, false)
- terminal.say("hello")
- expect(terminal.instance_variable_get(:@at_line_start)).to be true
+ it "outputs nothing for blank text" do
+ expect { terminal.print("") }.not_to output.to_stdout
end
- it "becomes false after print" do
- terminal.instance_variable_set(:@at_line_start, true)
- terminal.print("hello")
- expect(terminal.instance_variable_get(:@at_line_start)).to be false
+ it "outputs nothing in quiet mode" do
+ quiet_terminal = described_class.new(quiet: true)
+ expect { quiet_terminal.print("hello") }.not_to output.to_stdout
+ end
+ end
+
+ describe "#newline" do
+ it "outputs blank line" do
+ expect { terminal.newline }.to output("\n").to_stdout
+ end
+
+ it "outputs multiple blank lines" do
+ expect { terminal.newline(n: 3) }.to output("\n\n\n").to_stdout
+ end
+
+ it "outputs nothing in quiet mode" do
+ quiet_terminal = described_class.new(quiet: true)
+ expect { quiet_terminal.newline }.not_to output.to_stdout
+ end
+ end
+
+ describe "spacing consistency" do
+ it "produces single blank line between sections regardless of method used" do
+ expect {
+ terminal.say("section 1")
+ terminal.gap
+ terminal.say("section 2")
+ }.to output("section 1\nsection 2\n").to_stdout
end
- it "becomes true after newline" do
- terminal.instance_variable_set(:@at_line_start, false)
- terminal.newline
- expect(terminal.instance_variable_get(:@at_line_start)).to be true
+ it "produces single blank line after print then gap" do
+ expect {
+ terminal.print("partial")
+ terminal.gap
+ terminal.say("next")
+ }.to output("partial\nnext\n").to_stdout
end
end
end