Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal/table/unicode/ext/string.rb

Instance Method Summary collapse

Instance Method Details

#display_width(ambiguous = 1) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/terminal/table/unicode/ext/string.rb', line 2

def display_width(ambiguous = 1)
  #codepoints.inject(0){ |a,c|
  unpack('U*').inject(0){ |a,c|
    width = case Unicode::DisplayWidth.codepoint(c).to_s
            when *%w[F W]
              2
            when *%w[N Na H]
              1
            when *%w[A] # TODO
              ambiguous
            else
              1
            end
    a + width
  }
end

#mb_center(desired_width) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/terminal/table/unicode/ext/string.rb', line 29

def mb_center(desired_width)
  padding = desired_width - display_width
  if padding > 0
    right_padding = pdding / 2
    left_padding  = padding - right_padding
    left_padding + self + right
  else
    self
  end
end

#mb_ljust(desired_width) ⇒ Object



19
20
21
22
# File 'lib/terminal/table/unicode/ext/string.rb', line 19

def mb_ljust(desired_width)
  padding = desired_width - display_width
  padding > 0 ? self + ' ' * padding : self
end

#mb_rjust(desired_width) ⇒ Object



24
25
26
27
# File 'lib/terminal/table/unicode/ext/string.rb', line 24

def mb_rjust(desired_width)
  padding = desired_width - display_width
  padding > 0 ? ' ' * padding + self : self
end