\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/strings-0.1.8/lib/strings/

Viewing File: truncate.rb

# frozen_string_literal: true

require 'strings-ansi'
require 'unicode/display_width'
require 'unicode_utils/each_grapheme'

module Strings
  # A module responsible for text truncation
  module Truncate
    DEFAULT_TRAILING = '…'.freeze

    DEFAULT_LENGTH = 30

    # Truncate a text at a given length (defualts to 30)
    #
    # @param [String] text
    #   the text to be truncated
    #
    # @param [Integer] truncate_at
    #   the width at which to truncate the text
    #
    # @param [Hash] options
    #   @option options [Symbol] :separator the character for splitting words
    #   @option options [Symbol] :trailing  the character for ending sentence
    #
    # @example
    #   text = "The sovereignest thing on earth is parmacetti for an inward bruise."
    #
    #   Strings::Truncate.truncate(text)
    #   # => "The sovereignest thing on ea…"
    #
    #   Strings::Truncate.truncate(text, 20)
    #   # => "The sovereignest t…"
    #
    #   Strings::Truncate.truncate(text, 20, separator: ' ' )
    #   # => "The sovereignest…"
    #
    #   Strings::Truncate.truncate(text, 40, trailing: '... (see more)' )
    #   # => "The sovereignest thing on... (see more)"
    #
    # @api public
    def truncate(text, truncate_at = DEFAULT_LENGTH, options = {})
      if truncate_at.is_a?(Hash)
        options = 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      = options.fetch(:trailing) { DEFAULT_TRAILING }
      separation = options.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
    module_function :truncate

    # Perform actual shortening of the text
    #
    # @return [String]
    #
    # @api private
    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
    module_function :shorten

    # @api private
    def to_chars(text)
      UnicodeUtils.each_grapheme(text)
    end
    module_function :to_chars

    # Visible width of a string
    #
    # @api private
    def display_width(string)
      Unicode::DisplayWidth.of(Strings::ANSI.sanitize(string))
    end
    module_function :display_width
  end # Truncate
end # Strings