Skip to content

Commit

Permalink
More WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 2, 2024
1 parent 6cb04f8 commit e56e532
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
4 changes: 0 additions & 4 deletions lib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def fatal(...)
Logger.instance.fatal(...)
end

def failure(...)
Logger.instance.failure(...)
end

def call(...)
Logger.instance.call(...)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def self.for(exception)
self.new(exception, self.default_root)
end

def self.log(subject, exception, **options)
Console.error(subject, **self.for(exception).to_hash, **options)
end

def initialize(exception, root = Dir.getwd)
@exception = exception
@root = root
Expand Down
6 changes: 2 additions & 4 deletions lib/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

require_relative '../clock'

require_relative 'generic'

module Console
module Event
class Progress < Generic
class Progress
def self.now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(output, subject, total = 0, minimum_output_duration: 0.1, **options)
def initialize(subject, total = 0, minimum_output_duration: 0.1, output: Console, **options)
@output = output
@subject = subject
@options = options
Expand Down
38 changes: 36 additions & 2 deletions lib/console/event/spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
# Copyright, 2019-2022, by Samuel Williams.

require_relative 'generic'
require_relative '../clock'

module Console
module Event
# Represents a spawn event.
#
# ```ruby
# Console.info(self, **Console::Event::Spawn.for("ls", "-l"))
#
# event = Console::Event::Spawn.for("ls", "-l")
# event.status = Process.wait
# ```
class Spawn < Generic
def self.for(*arguments, **options)
def self.for(*arguments, **options, output: Console)
# Extract out the command environment:
if arguments.first.is_a?(Hash)
environment = arguments.shift
Expand All @@ -23,10 +27,23 @@ def self.for(*arguments, **options)
end
end

def initialize(environment, arguments, options)
def initialize(environment, arguments, options, output: Console)
@environment = environment
@arguments = arguments
@options = options

@start_time = Clock.now

@end_time = nil
@status = nil

@output = output
end

def duration
if @end_time
@end_time - @start_time
end
end

def to_hash
Expand All @@ -35,8 +52,25 @@ def to_hash
hash[:environment] = @environment if @environment&.any?
hash[:arguments] = @arguments if @arguments&.any?
hash[:options] = @options if @options&.any?

hash[:status] = @status.to_i if @status

if duration = self.duration
hash[:duration] = duration
end
end
end

def emit
@output.info(self, **to_hash)
end

def status=(status)
@end_time = Time.now
@status = status

self.emit
end
end
end
end
21 changes: 7 additions & 14 deletions lib/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,14 @@ def progress(subject, total, **options)
end

def error(subject, *arguments, **options, &block)
if self.enabled?(subject, ERROR)
if arguments.first.is_a?(Exception)
# It's better to use `failure`.
exception = arguments.shift
options.merge!(Event::Failure.for(exception))
end

self.call(subject, *arguments, severity: :error, **@options, **options, &block)
end
end

def failure(subject, exception, *arguments, **options, &block)
if self.enabled?(subject, ERROR)
self.call(subject, *arguments, severity: :error, **@options, **options, **Event::Failure.for(exception), &block)
# This is a special case where we want to create a failure event from an exception.
# It's common to see `Console.error(self, exception)` in code.
if arguments.first.is_a?(Exception)
exception = arguments.shift
options[:event] = Event::Failure.for(exception)
end

super
end
end
end
14 changes: 11 additions & 3 deletions lib/console/output/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **
end
end

if event and formatter = @formatters[event]
formatter.format(options, buffer, verbose: @verbose, width: width - indent_size)
elsif options&.any?
if event
format_event(event)
if options&.any?
format_options(options, buffer)
end

Expand All @@ -132,6 +132,14 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **

protected

def format_event(event)
event = event.to_hash

if formatter = @formatters[event]

formatter.format(options, buffer, verbose: @verbose, width: width - indent_size)
end

def format_options(options, output)
format_value(::JSON.pretty_generate(options), output)
end
Expand Down

0 comments on commit e56e532

Please sign in to comment.