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.

Every method accepts with_guide: (default: the global setting). With guides off the level symbol stays on the first line and continuation lines lose their rail prefix.

Class Method Summary collapse

Class Method Details

.command(cmd, type: :info, with_guide: nil, output: $stdout) ⇒ Boolean

Stream from a subprocess command. Usage: Clack.stream.command("npm install", type: :info)

Parameters:

  • cmd (String)

    the shell command to run

  • type (Symbol) (defaults to: :info)

    which stream method renders the output (:info, :success, :step, :warn, :error)

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Returns:

  • (Boolean)

    true on success, false on failure or if the command cannot be executed



85
86
87
88
89
90
91
92
# File 'lib/clack/stream.rb', line 85

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

.error(source, with_guide: nil, 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

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Yields:

  • (line)

    optional block called for each line



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

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

.info(source, with_guide: nil, 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

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Yields:

  • (line)

    optional block called for each line



21
22
23
# File 'lib/clack/stream.rb', line 21

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

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

This method returns an undefined value.

Stream lines with a plain bar prefix (no symbol), or bare lines with guides off.

Parameters:

  • source (IO, String, Enumerable)

    data source to stream

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream



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

def message(source, with_guide: nil, output: $stdout)
  prefix = rail_prefix(with_guide)
  each_line(source) do |line|
    output.puts "#{prefix}#{line.chomp}"
    output.flush
  end
end

.step(source, with_guide: nil, 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

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Yields:

  • (line)

    optional block called for each line



41
42
43
# File 'lib/clack/stream.rb', line 41

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

.success(source, with_guide: nil, 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

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Yields:

  • (line)

    optional block called for each line



31
32
33
# File 'lib/clack/stream.rb', line 31

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

.warn(source, with_guide: nil, 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

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

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

    output stream

Yields:

  • (line)

    optional block called for each line



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

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