Module: Clack::Stream

Defined in:
lib/clack/stream.rb

Overview

Stream logging utility for iterables, enumerables, and IO streams. Similar to Log but works with streaming data in real-time.

Class Method Summary collapse

Class Method Details

.command(cmd, type: :info, output: $stdout) ⇒ Object

Stream from a subprocess command. Usage: Clack.stream.command(“npm install”, type: :info) Returns true on success, false on failure or if command cannot be executed



70
71
72
73
74
75
76
77
# File 'lib/clack/stream.rb', line 70

def command(cmd, type: :info, output: $stdout)
  IO.popen(cmd, err: %i[child out]) do |io|
    send(type, io, output: output)
  end
  $CHILD_STATUS.success?
rescue Errno::ENOENT
  false
end

.error(source, output: $stdout) {|line| ... } ⇒ void

This method returns an undefined value.

Stream lines with an error symbol (red).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream

Yields:

  • (line)

    optional block called for each line



52
53
54
# File 'lib/clack/stream.rb', line 52

def error(source, output: $stdout, &block)
  stream_with_symbol(source, Symbols::S_ERROR, :red, output, &block)
end

.info(source, output: $stdout) {|line| ... } ⇒ void

This method returns an undefined value.

Stream lines with an info symbol (cyan).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream

Yields:

  • (line)

    optional block called for each line



16
17
18
# File 'lib/clack/stream.rb', line 16

def info(source, output: $stdout, &block)
  stream_with_symbol(source, Symbols::S_INFO, :cyan, output, &block)
end

.message(source, output: $stdout) ⇒ void

This method returns an undefined value.

Stream lines with a plain bar prefix (no symbol).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream



60
61
62
63
64
65
# File 'lib/clack/stream.rb', line 60

def message(source, output: $stdout)
  each_line(source) do |line|
    output.puts "#{Colors.gray(Symbols::S_BAR)}  #{line.chomp}"
    output.flush
  end
end

.step(source, output: $stdout) {|line| ... } ⇒ void

This method returns an undefined value.

Stream lines with a step symbol (green).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream

Yields:

  • (line)

    optional block called for each line



34
35
36
# File 'lib/clack/stream.rb', line 34

def step(source, output: $stdout, &block)
  stream_with_symbol(source, Symbols::S_STEP_SUBMIT, :green, output, &block)
end

.success(source, output: $stdout) {|line| ... } ⇒ void

This method returns an undefined value.

Stream lines with a success symbol (green).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream

Yields:

  • (line)

    optional block called for each line



25
26
27
# File 'lib/clack/stream.rb', line 25

def success(source, output: $stdout, &block)
  stream_with_symbol(source, Symbols::S_SUCCESS, :green, output, &block)
end

.warn(source, output: $stdout) {|line| ... } ⇒ void

This method returns an undefined value.

Stream lines with a warning symbol (yellow).

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • output (IO) (defaults to: $stdout)

    output stream

Yields:

  • (line)

    optional block called for each line



43
44
45
# File 'lib/clack/stream.rb', line 43

def warn(source, output: $stdout, &block)
  stream_with_symbol(source, Symbols::S_WARN, :yellow, output, &block)
end