Class: Bosh::Cli::TaskTracking::SmartWhitespacePrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/task_tracking/smart_whitespace_printer.rb

Constant Summary collapse

VALID_SEPARATORS =
[:line_around, :line_before, :before, :none].freeze
SPACE_BETWEEN_LAST_SEP_AND_SEP =
{
  [:line_around, :line_around] => "\n\n",
  [:line_around, :line_before] => "\n\n",
  [:line_around, :before] => "\n\n",
  [:line_around, :none]   => "\n\n",

  [:line_before, :line_around] => "\n\n",
  [:line_before, :line_before] => "\n\n",
  [:line_before, :before] => "\n",
  [:line_before, :none]   => nil,

  [:before, :line_around] => "\n\n",
  [:before, :line_before] => "\n\n",
  [:before, :before] => "\n",
  [:before, :none]   => nil,

  [:none, :line_around] => "\n\n",
  [:none, :line_before] => "\n\n",
  [:none, :before] => "\n",
  [:none, :none]   => nil,
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeSmartWhitespacePrinter

Returns a new instance of SmartWhitespacePrinter


29
30
31
32
# File 'lib/cli/task_tracking/smart_whitespace_printer.rb', line 29

def initialize
  @buffer = StringIO.new
  @last_sep = :start
end

Instance Method Details

#finishObject


50
51
52
53
54
# File 'lib/cli/task_tracking/smart_whitespace_printer.rb', line 50

def finish
  if VALID_SEPARATORS.include?(@last_sep)
    @buffer.print("\n")
  end
end

#outputObject


46
47
48
# File 'lib/cli/task_tracking/smart_whitespace_printer.rb', line 46

def output
  @buffer.string.tap { @buffer.string = '' }
end

34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cli/task_tracking/smart_whitespace_printer.rb', line 34

def print(separator, msg)
  unless VALID_SEPARATORS.include?(separator)
    raise ArgumentError, "Unknown separator #{separator.inspect}"
  end

  space = SPACE_BETWEEN_LAST_SEP_AND_SEP[[@last_sep, separator]]
  @buffer.print(space) if space

  @last_sep = separator
  @buffer.print(msg)
end