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.



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
97
# File 'lib/console/output/terminal.rb', line 72

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.



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

def start
  @start
end

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

#terminalObject (readonly)

Returns the value of attribute terminal.



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

def terminal
  @terminal
end

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



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

attr :terminal

#verboseObject

Returns the value of attribute verbose.



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

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.



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

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.



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
177
# File 'lib/console/output/terminal.rb', line 145

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.



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

def last_output
	self
end

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

Register all formatters in the given namespace.



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

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



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

attr :stream

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



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

attr :start

#verbose!(value = true) ⇒ Object

Set the verbose output.



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

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

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



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

attr_accessor :verbose