Module: Strings::Truncate
- Defined in:
- lib/strings/truncate.rb
Overview
A module responsible for text truncation
Constant Summary collapse
- DEFAULT_TRAILING =
"…".freeze
- DEFAULT_LENGTH =
30
Class Method Summary collapse
-
.display_width(string) ⇒ Object
private
Visible width of a string.
-
.shorten(original_chars, chars, length_without_trailing) ⇒ String
private
Perform actual shortening of the text.
- .to_chars(text) ⇒ Object private
-
.truncate(text, truncate_at = DEFAULT_LENGTH, options = {}) ⇒ Object
Truncate a text at a given length (defualts to 30).
Class Method Details
.display_width(string) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visible width of a string
102 103 104 |
# File 'lib/strings/truncate.rb', line 102 def display_width(string) Unicode::DisplayWidth.of(Strings::ANSI.sanitize(string)) end |
.shorten(original_chars, chars, length_without_trailing) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Perform actual shortening of the text
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/strings/truncate.rb', line 71 def shorten(original_chars, chars, length_without_trailing) truncated = [] return truncated if length_without_trailing.zero? char_width = display_width(chars[0]) while length_without_trailing - char_width > 0 orig_char = original_chars.shift char = chars.shift break unless char while orig_char != char # consume ansi ansi = true truncated << orig_char orig_char = original_chars.shift end truncated << char char_width = display_width(char) length_without_trailing -= char_width end truncated << ["\e[0m"] if ansi truncated end |
.to_chars(text) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
94 95 96 |
# File 'lib/strings/truncate.rb', line 94 def to_chars(text) UnicodeUtils.each_grapheme(text) end |
.truncate(text, truncate_at = DEFAULT_LENGTH, options = {}) ⇒ Object
Truncate a text at a given length (defualts to 30)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/strings/truncate.rb', line 42 def truncate(text, truncate_at = DEFAULT_LENGTH, = {}) if truncate_at.is_a?(Hash) = truncate_at truncate_at = DEFAULT_LENGTH end if display_width(text) <= truncate_at.to_i || truncate_at.to_i.zero? return text.dup end trail = .fetch(:trailing) { DEFAULT_TRAILING } separation = .fetch(:separator) { nil } sanitized_text = Strings::ANSI.sanitize(text) length_without_trailing = truncate_at - display_width(trail) chars = to_chars(sanitized_text).to_a stop = chars[0, length_without_trailing].rindex(separation) slice_length = stop || length_without_trailing sliced_chars = chars[0, slice_length] original_chars = to_chars(text).to_a[0, 3 * slice_length] shorten(original_chars, sliced_chars, length_without_trailing).join + trail end |