Class: String

Inherits:
Object
  • Object
show all
Defined in:
fastlane_core/lib/fastlane_core/core_ext/shellwords.rb,
fastlane/lib/fastlane/action.rb,
fastlane_core/lib/fastlane_core/ui/interface.rb,
fastlane_core/lib/fastlane_core/string_filters.rb,
fastlane_core/lib/fastlane_core/core_ext/string.rb,
spaceship/lib/spaceship/tunes/language_converter.rb,
fastlane_core/lib/fastlane_core/ui/disable_colors.rb

Overview

Here be monkey patches

Helpers collapse

Instance Method Summary collapse

Instance Method Details

#dedent!Object


185
186
187
188
189
# File 'fastlane/lib/fastlane/action.rb', line 185

def dedent!
  first_line_indent = self.match(/^\s*/)[0]

  self.gsub!(/^#{first_line_indent}/, "")
end

#deprecatedObject


202
203
204
# File 'fastlane_core/lib/fastlane_core/ui/interface.rb', line 202

def deprecated
  self.bold.blue
end

#fastlane_classObject


2
3
4
# File 'fastlane_core/lib/fastlane_core/core_ext/string.rb', line 2

def fastlane_class
  split('_').collect!(&:capitalize).join
end

#fastlane_moduleObject


6
7
8
# File 'fastlane_core/lib/fastlane_core/core_ext/string.rb', line 6

def fastlane_module
  self == "pem" ? 'PEM' : self.fastlane_class
end

#fastlane_underscoreObject


10
11
12
13
14
15
16
# File 'fastlane_core/lib/fastlane_core/core_ext/string.rb', line 10

def fastlane_underscore
  self.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    tr("-", "_").
    downcase
end

#markdown_clean_heredoc!Object


180
181
182
183
# File 'fastlane/lib/fastlane/action.rb', line 180

def markdown_clean_heredoc!
  self.chomp! # remove the last new line added by the heredoc
  self.dedent! # remove the leading whitespace (similar to the squigly heredoc `<<~`)
end

#markdown_details(is_first) ⇒ Object


174
175
176
177
178
# File 'fastlane/lib/fastlane/action.rb', line 174

def markdown_details(is_first)
  self.prepend("\n") unless is_first
  self << "\n>" # continue the quote
  self.markdown_preserve_newlines
end

#markdown_list(is_first = false) ⇒ Object


167
168
169
170
171
172
# File 'fastlane/lib/fastlane/action.rb', line 167

def markdown_list(is_first = false)
  self.markdown_clean_heredoc!
  self.gsub!(/^/, "- ") # add list dashes
  self.prepend(">") unless is_first # the empty line that will be added breaks the quote
  self.markdown_details(is_first)
end

#markdown_preserve_newlinesObject


158
159
160
# File 'fastlane/lib/fastlane/action.rb', line 158

def markdown_preserve_newlines
  self.gsub(/(\n|$)/, '|\1') # prepend new lines with "|" so the erb template knows *not* to replace them with "<br>"s
end

#markdown_sample(is_first = false) ⇒ Object


162
163
164
165
# File 'fastlane/lib/fastlane/action.rb', line 162

def markdown_sample(is_first = false)
  self.markdown_clean_heredoc!
  self.markdown_details(is_first)
end

#middle_truncate(length = 20, options = {}) ⇒ Object


43
44
45
46
47
48
49
50
# File 'fastlane_core/lib/fastlane_core/string_filters.rb', line 43

def middle_truncate(length = 20, options = {})
  omission = options[:omission] || '...'
  return self if self.length <= length + omission.length
  return self[0..length] if length < omission.length
  len = (length - omission.length) / 2
  s_len = len - length % 2
  self[0..s_len] + omission + self[self.length - len..self.length]
end

#remove_markdownObject


191
192
193
194
195
196
197
# File 'fastlane/lib/fastlane/action.rb', line 191

def remove_markdown
  string = self.gsub(/^>/, "") # remove Markdown quotes
  string = string.gsub(/\[http[^\]]+\]\(([^)]+)\)/, '\1 🔗') # remove Markdown links
  string = string.gsub(/\[([^\]]+)\]\(([^\)]+)\)/, '"\1" (\2 🔗)') # remove Markdown links with custom text
  string = string.gsub("|", "") # remove new line preserve markers
  return string
end

#shellescapeObject

CrossplatformShellwords


8
9
10
# File 'fastlane_core/lib/fastlane_core/core_ext/shellwords.rb', line 8

def shellescape
  CrossplatformShellwords.shellescape(self)
end

#to_full_languageObject


62
63
64
# File 'spaceship/lib/spaceship/tunes/language_converter.rb', line 62

def to_full_language
  Spaceship::Tunes::LanguageConverter.from_standard_to_itc(self)
end

#to_itc_localeObject


58
59
60
# File 'spaceship/lib/spaceship/tunes/language_converter.rb', line 58

def to_itc_locale
  Spaceship::Tunes::LanguageConverter.from_standard_to_itc_locale(self)
end

#to_language_codeObject


54
55
56
# File 'spaceship/lib/spaceship/tunes/language_converter.rb', line 54

def to_language_code
  Spaceship::Tunes::LanguageConverter.from_itc_to_standard(self)
end

#truncate(truncate_at, options = {}) ⇒ Object

Truncates a given text after a given length if text is longer than length:

'Once upon a time in a world far far away'.truncate(27)
# => "Once upon a time in a wo..."

Pass a string or regexp :separator to truncate text at a natural break:

'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# => "Once upon a time in a..."

'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# => "Once upon a time in a..."

The last characters will be replaced with the :omission string (defaults to “…”) for a total length not exceeding length:

'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# => "And they f... (continued)"

20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'fastlane_core/lib/fastlane_core/string_filters.rb', line 20

def truncate(truncate_at, options = {})
  return dup unless length > truncate_at

  omission = options[:omission] || '...'
  length_with_room_for_omission = truncate_at - omission.length
  stop = \
    if options[:separator]
      rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
    else
      length_with_room_for_omission
    end

  "#{self[0, stop]}#{omission}"
end

#wordwrap(length = 80) ⇒ Object


36
37
38
39
40
# File 'fastlane_core/lib/fastlane_core/string_filters.rb', line 36

def wordwrap(length = 80)
  return [] if length == 0
  self.gsub!(/(\S{#{length}})(?=\S)/, '\1 ')
  self.scan(/.{1,#{length}}(?:\s+|$)/)
end