Class: Brut::CLI::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/cli/output.rb

Overview

An IO-like class that provides user-helpful output, to be used in place of puts. This is not strictly an IO and is intended to provide only a few options for sending output to a human.

Command line apps that a human runs are often executed in the context of other apps or themselves spawn child processes. Thus, it can be hard to know what output is coming from where. This class addresses that by prefixing every line with the name of the command line app:

> bin/my_app doit
[ bin/my_app ] About to do sometohing
Cannot connect to house
[ bin/my_app ] Problem connecting

The prefix allows the human to know what output was generated by the app they ran and what not. This can be extremely helpful for debugging or just understanding what is going on.

Instance Method Summary collapse

Constructor Details

#initialize(io:, prefix:) ⇒ Output

Create a wrapper for the given IO that will use the given prefix

Parameters:

  • io (IO)

    an IO where output should be sent, for example $stdout.

  • prefix (String)

    a prefix to be placed in front of all messages (unless a _no_prefix version is called)



21
22
23
24
25
# File 'lib/brut/cli/output.rb', line 21

def initialize(io:, prefix:)
  @io          = io
  @prefix      = prefix
  @sync_status = @io.sync
end

Instance Method Details

#flushObject

Flush the underlying IO.



73
74
75
76
# File 'lib/brut/cli/output.rb', line 73

def flush
  @io.flush
  self
end

Calls print without any prefix. In theory, this is the escape hatch to sending arbitrary data to the underlying IO without expose said IO



53
54
55
# File 'lib/brut/cli/output.rb', line 53

def print(*objects)
  @io.print(*objects)
end

#printf(format_string, *objects) ⇒ Object

Prints a string via printf, using the prefix. This is useful for communciating to a human, but you need more power for formatting than is afforded by #puts.



61
62
63
# File 'lib/brut/cli/output.rb', line 61

def printf(format_string,*objects)
  @io.printf(@prefix + format_string,*objects)
end

#printf_no_prefix(format_string, *objects) ⇒ Object

Prints a string via printf, without the prefix.



68
69
70
# File 'lib/brut/cli/output.rb', line 68

def printf_no_prefix(format_string,*objects)
  @io.printf(format_string,*objects)
end

#puts(*objects) ⇒ Object

Calls puts, adding a prefix to each of the objects in *objects. This is useful for sending messages that a human may want to read.



39
40
41
42
43
44
45
46
47
# File 'lib/brut/cli/output.rb', line 39

def puts(*objects)
  if objects.empty?
    objects << ""
  end
  objects.each do |object|
    @io.puts(@prefix + object.to_s)
  end
  nil
end

#puts_no_prefix(*objects) ⇒ Object

Calls puts without the prefix. This is useful if the output is going to be obvious to the human user (e.g. CLI help), or if it's intended to be piped into another app.



31
32
33
# File 'lib/brut/cli/output.rb', line 31

def puts_no_prefix(*objects)
  @io.puts(*objects)
end