Class: Warg::Console

Inherits:
Object
  • 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

#initializeConsole

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_widthObject

Returns the value of attribute hostname_width.



68
69
70
# File 'lib/warg.rb', line 68

def hostname_width
  @hostname_width
end

Class Method Details

.SGR(text) ⇒ Object



71
72
73
# File 'lib/warg.rb', line 71

def self.SGR(text)
  SelectGraphicRendition::Renderer.new(text)
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


103
104
105
# File 'lib/warg.rb', line 103

def print(text_or_content = nil)
  print_content(text_or_content)
end


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_stderrObject



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

  # starting from the current line, clear the line and move up a line
  #
  # this will bring us to the line the content we're reprinting, clearing all lines beneath
  # it
  rows_from_cursor_row_to_content_start.times do
    # clear the current line
    @io.print "\e[2K"

    # move up a line
    @io.print "\e[1A"
  end

  # go to the column of the content
  @io.print "\e[#{history_entry.column_number}G"

  # clear from the starting column of the content to the end of the line
  @io.print "\e[0K"

  # re-print the content from its original location
  @io.print content.to_s

  # initialize how much we'll be adjusting the column number by
  column_adjustment = history_entry.end_column

  current_entry = history_entry

  until current_entry.next_entry.nil?
    next_entry = current_entry.next_entry

    # we only update the column of subsequent entries if they were on the same line as the
    # entry being reprinted.
    if next_entry.row_number == history_entry.end_row
      next_entry.column_number = column_adjustment
      column_adjustment += next_entry.last_line_length
    end

    # update the next entry's row number by how many rows the updated entry grew or shrank
    next_entry.row_number += history_entry.newline_count_diff

    # print the content
    @io.print next_entry.to_s

    # get the next entry to repeat
    current_entry = next_entry
  end

  # Update the cursor position by how many row's the new content has changed and the new
  # end column of the last entry in the history
  @cursor_position.column = current_entry.end_column
  @cursor_position.row += history_entry.newline_count_diff

  # reset `newline_count` and `last_line_length` to what they now are in `@content`
  history_entry.sync!

  @io_mutex.unlock
end