Class: RubyWasm::StatusPrinter

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

Overview

Human readable status printer for the build.

Instance Method Summary collapse

Constructor Details

#initializeStatusPrinter

Returns a new instance of StatusPrinter.



150
151
152
153
154
# File 'lib/ruby_wasm/build.rb', line 150

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

Instance Method Details

#doneObject



185
186
187
# File 'lib/ruby_wasm/build.rb', line 185

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

#stderr(message) ⇒ Object



181
182
183
# File 'lib/ruby_wasm/build.rb', line 181

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

#stdout(message) ⇒ Object



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

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