Class: Console::Output::Terminal

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

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

CONSOLE_START_AT =

This, and all related methods, is considered private.

"CONSOLE_START_AT"
UNKNOWN =
:unknown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Terminal.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/console/output/terminal.rb', line 54

def initialize(output, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
	@io = output
	@start_at = start_at
	
	@terminal = format.nil? ? Console::Terminal.for(@io) : format.new(@io)
	
	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

#ioObject (readonly)

Returns the value of attribute io.



86
87
88
# File 'lib/console/output/terminal.rb', line 86

def io
  @io
end

#startObject (readonly)

Returns the value of attribute start.



90
91
92
# File 'lib/console/output/terminal.rb', line 90

def start
  @start
end

#terminalObject (readonly)

Returns the value of attribute terminal.



91
92
93
# File 'lib/console/output/terminal.rb', line 91

def terminal
  @terminal
end

#verboseObject

Returns the value of attribute verbose.



88
89
90
# File 'lib/console/output/terminal.rb', line 88

def verbose
  @verbose
end

Class Method Details

.start_at!(environment = ENV) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/console/output/terminal.rb', line 41

def self.start_at!(environment = ENV)
	if time_string = environment[CONSOLE_START_AT]
		start_at = Time.parse(time_string) rescue nil
	end
	
	unless start_at
		start_at = Time.now
		environment[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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/console/output/terminal.rb', line 106

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
	
	@io.write buffer.string
end

#last_outputObject

This a final output that then writes to an IO object.



82
83
84
# File 'lib/console/output/terminal.rb', line 82

def last_output
	self
end

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



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

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

#verbose!(value = true) ⇒ Object



93
94
95
# File 'lib/console/output/terminal.rb', line 93

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