Class: Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/google/utils.rb

Class Method Summary collapse

Class Method Details

.widthObject

From the trollop rubygem File lib/trollop.rb, line 489



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/google/utils.rb', line 36

def self.width #:nodoc:
  @@width ||= if $stdout.tty?
    begin
      require 'curses'
      Curses::init_screen
      x = Curses::cols
      Curses::close_screen
      x
    rescue Exception
      80
    end
  else
    80
  end
end

.wrap(str, opts = {}) ⇒ Object

From the trollop rubygem File lib/trollop.rb, line 505



26
27
28
29
30
31
32
# File 'lib/google/utils.rb', line 26

def self.wrap str, opts={} # :nodoc:
  if str == ""
    [""]
  else
    str.split("\n").map { |s| self.wrap_line s, opts }.flatten(1).join("\n")
  end
end

.wrap_line(str, opts = {}) ⇒ Object

From the trollop rubygem File lib/trollop.rb, line 644



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/google/utils.rb', line 4

def self.wrap_line str, opts={}
  prefix = opts[:prefix] || 0
  width = opts[:width] || (self.width - 1)
  start = 0
  ret = []
  until start > str.length
    nextt =
      if start + width >= str.length
        str.length
      else
        x = str.rindex(/\s/, start + width)
        x = str.index(/\s/, start) if x && x < start
        x || str.length
      end
    ret << (ret.empty? ? "" : " " * prefix) + str[start ... nextt]
    start = nextt + 1
  end
  ret
end