Sha256: ee236b8bbd5a3cafaee9502b15823af849f9febb96455553121e9995beca9fd9

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, 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)
				# Extract out the command environment:
				if arguments.first.is_a?(Hash)
					environment = arguments.shift
					self.new(environment, arguments, options)
				else
					self.new(nil, arguments, options)
				end
			end
			
			def initialize(environment, arguments, options)
				@environment = environment
				@arguments = arguments
				@options = options
				
				@start_time = Clock.now
				
				@end_time = nil
				@status = nil
			end
			
			def duration
				if @end_time
					@end_time - @start_time
				end
			end
			
			def to_hash
				Hash.new.tap do |hash|
					hash[:type] = :spawn
					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(*arguments, **options)
				options[:severity] ||= :info
				super
			end
			
			def status=(status)
				@end_time = Time.now
				@status = status
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
console-1.29.0 lib/console/event/spawn.rb
console-1.28.1 lib/console/event/spawn.rb
console-1.28.0 lib/console/event/spawn.rb