Sha256: 6632475128ad9627f01f506c7ed77153cda709279b7e925c23eb8515dc7ac676

Contents?: true

Size: 1.48 KB

Versions: 20

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.

require_relative 'generic'

module Console
	module Event
		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
			end
			
			attr :environment
			attr :arguments
			attr :options
			
			def chdir_string(options)
				if options and chdir = options[:chdir]
					" in #{chdir}"
				end
			end
			
			def self.register(terminal)
				terminal[:shell_command] ||= terminal.style(:blue, nil, :bold)
			end
			
			def to_h
				Hash.new.tap do |hash|
					hash[:environment] = @environment if @environment&.any?
					hash[:arguments] = @arguments if @arguments&.any?
					hash[:options] = @options if @options&.any?
				end
			end
			
			def format(output, terminal, verbose)
				arguments = @arguments.flatten.collect(&:to_s)
				
				output.puts "  #{terminal[:shell_command]}#{arguments.join(' ')}#{terminal.reset}#{chdir_string(options)}"
				
				if verbose and @environment
					@environment.each do |key, value|
						output.puts "    export #{key}=#{value}"
					end
				end
			end
		end
	end
	
	# Deprecated.
	Shell = Event::Spawn
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
console-1.24.0 lib/console/event/spawn.rb
console-1.23.7 lib/console/event/spawn.rb
console-1.23.6 lib/console/event/spawn.rb
console-1.23.5 lib/console/event/spawn.rb
console-1.23.4 lib/console/event/spawn.rb
console-1.23.3 lib/console/event/spawn.rb
console-1.23.2 lib/console/event/spawn.rb
console-1.23.1 lib/console/event/spawn.rb
console-1.23.0 lib/console/event/spawn.rb
console-1.22.0 lib/console/event/spawn.rb
console-1.21.0 lib/console/event/spawn.rb
console-1.20.0 lib/console/event/spawn.rb
console-1.19.0 lib/console/event/spawn.rb
console-1.18.0 lib/console/event/spawn.rb
console-1.17.4 lib/console/event/spawn.rb
console-1.17.3 lib/console/event/spawn.rb
console-1.17.2 lib/console/event/spawn.rb
console-1.17.1 lib/console/event/spawn.rb
console-1.17.0 lib/console/event/spawn.rb
console-1.16.2 lib/console/event/spawn.rb