Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/flumtter/app/core/string.rb

Instance Method Summary collapse

Instance Method Details

#_ljustObject



76
# File 'lib/flumtter/app/core/string.rb', line 76

alias_method :_ljust, :ljust

#background_color(color = :red) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/flumtter/app/core/string.rb', line 36

def background_color(color=:red)
  case color
  when :black
    "\e[40m#{self}\e[0m"
  when :red
    "\e[41m#{self}\e[0m"
  when :green
    "\e[42m#{self}\e[0m"
  when :yellow
    "\e[43m#{self}\e[0m"
  when :blue
    "\e[44m#{self}\e[0m"
  when :magenta
    "\e[45m#{self}\e[0m"
  when :cyan
    "\e[46m#{self}\e[0m"
  when :white
    "\e[47m#{self}\e[0m"
  end
end

#color(color = :red) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flumtter/app/core/string.rb', line 15

def color(color=:red)
  case color
  when :white       then "\e[1;37m#{self}\e[0m"
  when :light_gray  then "\e[37m#{self}\e[0m"
  when :gray        then "\e[1;30m#{self}\e[0m"
  when :back        then "\e[30m#{self}\e[0m"
  when :red         then "\e[31m#{self}\e[0m"
  when :light_red   then "\e[1;31m#{self}\e[0m"
  when :green       then "\e[32m#{self}\e[0m"
  when :light_green then "\e[1;32m#{self}\e[0m"
  when :brown       then "\e[33m#{self}\e[0m"
  when :yellow      then "\e[1;33m#{self}\e[0m"
  when :blue        then "\e[34m#{self}\e[0m"
  when :light_blue  then "\e[1;34m#{self}\e[0m"
  when :purple      then "\e[35m#{self}\e[0m"
  when :pink        then "\e[1;35m#{self}\e[0m"
  when :cyan        then "\e[36m#{self}\e[0m"
  when :light_cyan  then "\e[1;36m#{self}\e[0m"
  end
end

#dnlObject



65
66
67
# File 'lib/flumtter/app/core/string.rb', line 65

def dnl
  "\n#{self}\n"
end

#exact_sizeObject



57
58
59
# File 'lib/flumtter/app/core/string.rb', line 57

def exact_size
  self.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
end

#has_mb?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/flumtter/app/core/string.rb', line 69

def has_mb?
  self.bytes do |b|
    return true if  (b & 0b10000000) != 0
  end
  false
end

#ljust(length, padstr = nil, rstr = "") ⇒ Object



77
78
79
80
81
82
# File 'lib/flumtter/app/core/string.rb', line 77

def ljust(length, padstr=nil, rstr="")
  length -= (exact_size - 1) if has_mb?
  length -= rstr.exact_size
  text = padstr.nil? ? _ljust(length) : _ljust(length, padstr)
  text << rstr
end

#max_char_of_linesObject



138
139
140
141
# File 'lib/flumtter/app/core/string.rb', line 138

def max_char_of_lines
  max = self.each_line.max_by{|str|str.size}
  max.nil? ? 0 : max.size
end

#nl(i = 1) ⇒ Object



61
62
63
# File 'lib/flumtter/app/core/string.rb', line 61

def nl(i=1)
  self + "\n"*i
end

#nshift(n = 2) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/flumtter/app/core/string.rb', line 103

def nshift(n=2)
  self.each_line.map.with_index do |line, i|
    if i.zero?
      line.chomp
    else
      " "*n + line.chomp
    end
  end.join("\n")
end

#range?(max) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/flumtter/app/core/string.rb', line 128

def range?(max)
  unless 1 <= self.size && self.size <= max
    raise RangeError
  end
end

#shift(n = 2) ⇒ Object



97
98
99
100
101
# File 'lib/flumtter/app/core/string.rb', line 97

def shift(n=2)
  self.each_line.map do |line|
    " "*n + line.chomp
  end.join("\n")
end

#size_of_linesObject



134
135
136
# File 'lib/flumtter/app/core/string.rb', line 134

def size_of_lines
  self.each_line.to_a.size
end

#split_num(n) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/flumtter/app/core/string.rb', line 113

def split_num(n)
  text = []
  tmp = ""
  self.each_char.each do |char|
    if tmp.exact_size < n
      tmp << char
    else
      text << tmp
      tmp = char
    end
  end
  text << tmp
  text.join("\n")
end

#terminal_titleObject



6
7
8
9
# File 'lib/flumtter/app/core/string.rb', line 6

def terminal_title
  print "\033];#{self}\007"
  at_exit{print "\033];\007"}
end

#titleObject



11
12
13
# File 'lib/flumtter/app/core/string.rb', line 11

def title
  "#{self}"
end

#to_camelObject



2
3
4
# File 'lib/flumtter/app/core/string.rb', line 2

def to_camel
  self.split(/_/).map(&:capitalize).join
end

#to_emObject



84
85
86
# File 'lib/flumtter/app/core/string.rb', line 84

def to_em
  self.tr('0-9a-zA-Z', '0-9a-zA-Z')
end

#to_enObject



88
89
90
# File 'lib/flumtter/app/core/string.rb', line 88

def to_en
  self.tr('0-9a-zA-Z', '0-9a-zA-Z')
end

#to_regObject



92
93
94
95
# File 'lib/flumtter/app/core/string.rb', line 92

def to_reg
  reg = Regexp.union(self, self.to_em)
  /^#{Regexp.new(reg.source, Regexp::IGNORECASE)}[ | ]*(.*)/
end