Class: Warg::Console
- Inherits:
-
Object
show all
- Defined in:
- lib/warg.rb
Defined Under Namespace
Classes: Content, CursorPosition, History, HostStatus, IOProxy, SelectGraphicRendition
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Console.
75
76
77
78
79
80
81
|
# File 'lib/warg.rb', line 75
def initialize
@io = $stderr
@history = History.new
@cursor_position = CursorPosition.new
@io_mutex = Mutex.new
@history_mutex = Mutex.new
end
|
Class Attribute Details
.hostname_width ⇒ Object
Returns the value of attribute hostname_width.
68
69
70
|
# File 'lib/warg.rb', line 68
def hostname_width
@hostname_width
end
|
Instance Method Details
#append_to_history(content) ⇒ Object
124
125
126
127
128
129
|
# File 'lib/warg.rb', line 124
def append_to_history(content)
@history_mutex.synchronize do
@history.append(content, at: @cursor_position)
@cursor_position.adjust_to(content)
end
end
|
#print(text_or_content = nil) ⇒ Object
103
104
105
|
# File 'lib/warg.rb', line 103
def print(text_or_content = nil)
print_content(text_or_content)
end
|
#print_content(text_or_content) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/warg.rb', line 107
def print_content(text_or_content)
content = case text_or_content
when Content, HostStatus
text_or_content
else
Content.new(text_or_content, self)
end
@io_mutex.synchronize do
@io.print content.to_s
append_to_history(content)
content
end
end
|
#puts(text_or_content = nil) ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'lib/warg.rb', line 93
def puts(text_or_content = nil)
content = print_content text_or_content
unless text_or_content.to_s.end_with?("\n")
print_content "\n"
end
content
end
|
#redirecting_stdout_and_stderr ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/warg.rb', line 83
def redirecting_stdout_and_stderr
$stdout = IOProxy.new($stdout, self)
$stderr = IOProxy.new($stderr, self)
yield
ensure
$stdout = $stdout.__getobj__
$stderr = $stderr.__getobj__
end
|
#reprint_content(content) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/warg.rb', line 133
def reprint_content(content)
@io_mutex.lock
history_entry = @history.find_entry_for(content)
rows_from_cursor_row_to_content_start = @cursor_position.row - history_entry.row_number
rows_from_cursor_row_to_content_start.times do
@io.print "\e[2K"
@io.print "\e[1A"
end
@io.print "\e[#{history_entry.column_number}G"
@io.print "\e[0K"
@io.print content.to_s
column_adjustment = history_entry.end_column
current_entry = history_entry
until current_entry.next_entry.nil?
next_entry = current_entry.next_entry
if next_entry.row_number == history_entry.end_row
next_entry.column_number = column_adjustment
column_adjustment += next_entry.last_line_length
end
next_entry.row_number += history_entry.newline_count_diff
@io.print next_entry.to_s
current_entry = next_entry
end
@cursor_position.column = current_entry.end_column
@cursor_position.row += history_entry.newline_count_diff
history_entry.sync!
@io_mutex.unlock
end
|