Skip to content

Commit

Permalink
More WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 1, 2024
1 parent cbfdced commit ee4a0f5
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 30 deletions.
10 changes: 6 additions & 4 deletions lib/console/output/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
module Console
module Output
module Default
def self.new(output, io: $stderr, **options)
if io.tty?
Terminal.new(io: io, **options)
def self.new(output, **options)
output ||= $stderr

if output.tty?
Terminal.new(output, **options)
else
Serialized.new(io: io, **options)
Serialized.new(output, **options)
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/console/output/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

require_relative 'generic'

module Console
module Output
class Null < Generic
class Null
def initialize(...)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/console/output/sensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Released under the MIT License.
# Copyright, 2021-2022, by Samuel Williams.

require_relative 'generic'
require_relative 'wrapper'

module Console
module Output
class Sensitive < Generic
class Sensitive < Wrapper
REDACT = /
phone
| email
Expand Down
6 changes: 4 additions & 2 deletions lib/console/output/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
module Console
module Output
class Serialized
def initialize(io: $stderr, format: Format.default, **options)
@io = io
def initialize(output, format: Format.default, **options)
@io = output
@format = format
end

Expand Down Expand Up @@ -66,5 +66,7 @@ def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
@io.puts(self.dump(record))
end
end

JSON = Serialized
end
end
10 changes: 5 additions & 5 deletions lib/console/output/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def self.start_at!(environment = ENV)
return start_at
end

def initialize(io: $stderr, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
@io = io
def initialize(output, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
@io = output
@start_at = start_at

@terminal = format.nil? ? Console::Terminal.for(io) : format.new(io)
@terminal = format.nil? ? Console::Terminal.for(@io) : format.new(@io)

if verbose.nil?
@verbose = !@terminal.colors?
Expand Down Expand Up @@ -226,13 +226,13 @@ def build_prefix(name)

module Text
def self.new(output, **options)
Terminal.new(format: Console::Terminal::Text, **options)
Terminal.new(output, format: Console::Terminal::Text, **options)
end
end

module XTerm
def self.new(output, **options)
Terminal.new(format: Console::Terminal::XTerm, **options)
Terminal.new(output, format: Console::Terminal::XTerm, **options)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Console
module Output
class Generic
class Wrapper
def initialize(delegate, **options)
@delegate = delegate
end
Expand Down
2 changes: 1 addition & 1 deletion test/console/compatible/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

describe Console::Compatible::Logger do
let(:io) {StringIO.new}
let(:output) {Console::Output::Terminal.new(io: io)}
let(:output) {Console::Output::Terminal.new(io)}
let(:logger) {Console::Compatible::Logger.new("Test", output)}

it "should log messages" do
Expand Down
6 changes: 3 additions & 3 deletions test/console/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

with env: {'CONSOLE_OUTPUT' => 'JSON'} do
it 'can set output to Serialized and format to JSON' do
expect(output).to be_a Console::Serialized::Logger
expect(output).to be_a Console::Output::Serialized
expect(output.format).to be_a(Console::Format::Safe)
end
end
Expand All @@ -53,15 +53,15 @@
with env: {'CONSOLE_OUTPUT' => 'XTerm'} do
it 'can force format to XTerm for non tty output by ENV' do
expect(Console::Terminal).not.to receive(:for)
expect(output).to be_a Console::Terminal::Logger
expect(output).to be_a Console::Output::Terminal
expect(output.terminal).to be_a Console::Terminal::XTerm
end
end

with env: {'CONSOLE_OUTPUT' => 'Text'} do
it 'can force format to text for tty output by ENV using Text' do
expect(Console::Terminal).not.to receive(:for)
expect(output).to be_a Console::Terminal::Logger
expect(output).to be_a Console::Output::Terminal
expect(output.terminal).to be_a Console::Terminal::Text
end
end
Expand Down
10 changes: 3 additions & 7 deletions test/console/output/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'console/capture'

describe Console::Output::Default do
let(:output) {Console::Capture.new}
let(:output) {nil}
let(:logger) {subject.new(output)}

def final_output(output)
Expand All @@ -18,11 +18,7 @@ def final_output(output)
end
end

with 'unspecified output' do
let(:output) {nil}

it 'should output to $stderr by default' do
expect(final_output(logger).io).to be == $stderr
end
it 'should output to $stderr by default' do
expect(final_output(logger).io).to be == $stderr
end
end
2 changes: 1 addition & 1 deletion test/console/output/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

describe Console::Output::Serialized do
let(:io) {StringIO.new}
let(:logger) {subject.new(io: io)}
let(:logger) {subject.new(io)}

let(:message) {"Hello World"}

Expand Down
2 changes: 1 addition & 1 deletion test/console/output/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

describe Console::Output::Terminal do
let(:io) {StringIO.new}
let(:logger) {subject.new(io: io, verbose: true)}
let(:logger) {subject.new(io, verbose: true)}

let(:message) {"Hello World"}

Expand Down

0 comments on commit ee4a0f5

Please sign in to comment.