Class: RubyWasm::StatusPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_wasm/build/executor.rb

Overview

Human readable status printer for the build.

Instance Method Summary collapse

Constructor Details

#initializeStatusPrinter

Returns a new instance of StatusPrinter.



152
153
154
155
156
# File 'lib/ruby_wasm/build/executor.rb', line 152

def initialize
  @mutex = Mutex.new
  @counter = 0
  @indicators = "|/-\\"
end

Instance Method Details

#doneObject



187
188
189
# File 'lib/ruby_wasm/build/executor.rb', line 187

def done
  @mutex.synchronize { $stdout.print "\e[K" }
end

#stderr(message) ⇒ Object



183
184
185
# File 'lib/ruby_wasm/build/executor.rb', line 183

def stderr(message)
  @mutex.synchronize { $stdout.print message }
end

#stdout(message) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ruby_wasm/build/executor.rb', line 158

def stdout(message)
  require "io/console"
  @mutex.synchronize do
    $stdout.print "\e[K"
    first_line = message.lines(chomp: true).first || ""

    # Make sure we don't line-wrap the output
    size =
      __skip__ =
        IO.respond_to?(:console_size) ? IO.console_size : IO.console.winsize
    terminal_width = size[1].to_i.nonzero? || 80
    width_limit = terminal_width / 2 - 3

    if first_line.length > width_limit
      first_line = (first_line[0..width_limit - 5] || "") + "..."
    end
    indicator = @indicators[@counter] || " "
    to_print = "  " + indicator + " " + first_line
    $stdout.print to_print
    $stdout.print "\e[1A\n"
    @counter += 1
    @counter = 0 if @counter >= @indicators.length
  end
end