Class: Browserctl::Commands::OutputFormat::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/browserctl/commands/output_format.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ Formatter

Returns a new instance of Formatter.



40
41
42
# File 'lib/browserctl/commands/output_format.rb', line 40

def initialize(mode)
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



38
39
40
# File 'lib/browserctl/commands/output_format.rb', line 38

def mode
  @mode
end

Instance Method Details

#emit(payload, text_block = nil, io: $stdout) ⇒ Object

Print success output for a command. ‘payload` is a JSON-serialisable Hash (or Array). `text_block` is either a String or a block that returns a String — only evaluated when needed.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/browserctl/commands/output_format.rb', line 48

def emit(payload, text_block = nil, io: $stdout)
  case @mode
  when "silent"
    nil
  when "json"
    io.puts payload.to_json
  else # "text"
    text = if block_given?
             yield
           elsif text_block.respond_to?(:call)
             text_block.call
           elsif text_block.nil?
             payload.to_json
           else
             text_block
           end
    io.puts text
  end
end

#json?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/browserctl/commands/output_format.rb', line 72

def json?
  @mode == "json"
end

#silent?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/browserctl/commands/output_format.rb', line 68

def silent?
  @mode == "silent"
end

#text?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/browserctl/commands/output_format.rb', line 76

def text?
  @mode == "text"
end