Module: Hirb::Unicode::StringUtil

Defined in:
lib/hirb/unicode/string_util.rb

Instance Method Summary collapse

Instance Method Details

#ljust(string, desired_width) ⇒ Object



25
26
27
28
# File 'lib/hirb/unicode/string_util.rb', line 25

def ljust(string, desired_width)
  leftover = desired_width - size(string)
  leftover > 0 ? string + " " * leftover : string
end

#rjust(string, desired_width) ⇒ Object



30
31
32
33
# File 'lib/hirb/unicode/string_util.rb', line 30

def rjust(string, desired_width)
  leftover = desired_width - size(string)
  leftover > 0 ? " " * leftover + string : string
end

#size(string) ⇒ Object



4
5
6
# File 'lib/hirb/unicode/string_util.rb', line 4

def size(string)
  string.display_width
end

#slice(string, offset, width) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hirb/unicode/string_util.rb', line 8

def slice(string, offset, width)
  chars = string.chars.to_a[offset..-1].to_a

  current_length = 0
  split_index = 0
  chars.each_with_index do |c, i|
    char_width = self.size(c)
    break if current_length + char_width > width
    split_index = i+1
    current_length += char_width
  end

  split_index ||= chars.count
  head = chars[0, split_index].join
  head
end