Class: Console::Output::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/console/output/terminal.rb

Overview

Represents a terminal output, and formats log messages for display.

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

CONSOLE_START_AT =

The environment variable used to store the start time of the console terminal output.

"CONSOLE_START_AT"
UNKNOWN =

The default severity for log messages, if not specified.

:unknown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, verbose: nil, start_at: Terminal.start_at!, format: nil, **options) ⇒ Terminal

Create a new terminal output.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/console/output/terminal.rb', line 71

def initialize(stream, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
	@stream = stream
	@start_at = start_at
	
	@terminal = format.nil? ? Console::Terminal.for(@stream) : format.new(@stream)
	
	if verbose.nil?
		@verbose = !@terminal.colors?
	else
		@verbose = verbose
	end
	
	@terminal[:logger_suffix] ||= @terminal.style(:white, nil, :faint)
	@terminal[:subject] ||= @terminal.style(nil, nil, :bold)
	@terminal[:debug] = @terminal.style(:cyan)
	@terminal[:info] = @terminal.style(:green)
	@terminal[:warn] = @terminal.style(:yellow)
	@terminal[:error] = @terminal.style(:red)
	@terminal[:fatal] = @terminal[:error]
	
	@terminal[:annotation] = @terminal.reset
	@terminal[:value] = @terminal.style(:blue)
	
	@formatters = {}
	self.register_formatters
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



110
111
112
# File 'lib/console/output/terminal.rb', line 110

def start
  @start
end

#streamObject (readonly)

Returns the value of attribute stream.



104
105
106
# File 'lib/console/output/terminal.rb', line 104

def stream
  @stream
end

#terminalObject (readonly)

Returns the value of attribute terminal.



113
114
115
# File 'lib/console/output/terminal.rb', line 113

def terminal
  @terminal
end

#The format to use for terminal output.(formattouse) ⇒ Object (readonly)



113
# File 'lib/console/output/terminal.rb', line 113

attr :terminal

#verboseObject

Returns the value of attribute verbose.



107
108
109
# File 'lib/console/output/terminal.rb', line 107

def verbose
  @verbose
end

Class Method Details

.start_at!(env = ENV) ⇒ Object

Exports CONSOLE_START_AT which can be used to synchronize the start times of all child processes when they log using delta time.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/console/output/terminal.rb', line 51

def self.start_at!(env = ENV)
	if time_string = env[CONSOLE_START_AT]
		start_at = Time.parse(time_string) rescue nil
	end
	
	unless start_at
		start_at = Time.now
		env[CONSOLE_START_AT] = start_at.to_s
	end
	
	return start_at
end

Instance Method Details

#call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **options, &block) ⇒ Object

Log a message with the given severity.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/console/output/terminal.rb', line 144

def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **options, &block)
	width = @terminal.width
	
	prefix = build_prefix(name || severity.to_s)
	indent = " " * prefix.size
	
	buffer = Buffer.new("#{indent}| ")
	indent_size = buffer.prefix.size
	
	format_subject(severity, prefix, subject, buffer)
	
	arguments.each do |argument|
		format_argument(argument, buffer)
	end
	
	if block_given?
		if block.arity.zero?
			format_argument(yield, buffer)
		else
			yield(buffer, @terminal)
		end
	end
	
	if event
		format_event(event, buffer, width - indent_size)
	end
	
	if options&.any?
		format_options(options, buffer)
	end
	
	@stream.write buffer.string
end

#last_outputObject

This a final output.



99
100
101
# File 'lib/console/output/terminal.rb', line 99

def last_output
	self
end

#register_formatters(namespace = Console::Terminal::Formatter) ⇒ Object

Register all formatters in the given namespace.



123
124
125
126
127
128
# File 'lib/console/output/terminal.rb', line 123

def register_formatters(namespace = Console::Terminal::Formatter)
	namespace.constants.each do |name|
		formatter = namespace.const_get(name)
		@formatters[formatter::KEY] = formatter.new(@terminal)
	end
end

#The output stream.=(outputstream. = (value)) ⇒ Object



104
# File 'lib/console/output/terminal.rb', line 104

attr :stream

#The start time of the terminal output.=(starttimeoftheterminaloutput. = (value)) ⇒ Object



110
# File 'lib/console/output/terminal.rb', line 110

attr :start

#verbose!(value = true) ⇒ Object

Set the verbose output.



118
119
120
# File 'lib/console/output/terminal.rb', line 118

def verbose!(value = true)
	@verbose = value
end

#Whether to print verbose output.=(toprintverboseoutput. = (value)) ⇒ Object



107
# File 'lib/console/output/terminal.rb', line 107

attr_accessor :verbose