Module: CodeUnion::Helpers::Text

Included in:
Command::Search, Search::ResultPresenter
Defined in:
lib/codeunion/helpers/text.rb

Overview

Formats text for presenting to a console

Instance Method Summary collapse

Instance Method Details

#format_output(text) ⇒ Object



8
9
10
11
12
13
# File 'lib/codeunion/helpers/text.rb', line 8

def format_output(text)
  _rows, cols  = IO.console.winsize
  line_length = [cols, 80].min

  indent(wrap(text, line_length))
end

#indent(lines, level = 1) ⇒ Object



33
34
35
# File 'lib/codeunion/helpers/text.rb', line 33

def indent(lines, level = 1)
  lines.split("\n").map { |line| ("  " * level) + line }.join("\n")
end

#wrap(text, max_width = IO.console.winsize.last) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/codeunion/helpers/text.rb', line 15

def wrap(text, max_width = IO.console.winsize.last)
  return "" if text.empty?

  words = text.split(/\s+/)
  output = words.shift
  chars_left = max_width - output.length

  words.inject(output) do |output_builder, word|
    if word.length + 1 > chars_left
      chars_left = max_width - word.length
      output_builder << "\n" + word
    else
      chars_left -= word.length + 1
      output_builder << " " + word
    end
  end
end