Sha256: 7a60a97182ea61a930bdba26c7947309b134c6b8fc1f84dd0b9d909805ccb22a

Contents?: true

Size: 904 Bytes

Versions: 4

Compression:

Stored size: 904 Bytes

Contents

module ClipStringRefinement
  refine String do
    using AnsilessStringRefinement

    # Warning: Still not going to work nicely if a string ends in an ansi code!
    def clip_at(clip_length, ignore_ansi_codes: false)
      length_method = ignore_ansi_codes ? :ansiless_length : :length
      original_length = send(length_method)
      ansi_length = ignore_ansi_codes ? length - ansiless_length : 0
      slice_length = clip_length + ansi_length
      clipped_string = self[0...slice_length]
      if clipped_string.send(length_method) < original_length
        clipped_string = clipped_string.mark_as_abbreviated
      end

      clipped_string
    end

    # Warning: Still not going to work nicely if a string ends in an ansi code!
    def mark_as_abbreviated
      self_dup = dup
      self_dup[-1] = '…' if self_dup[-1]
      self_dup[-2] = '…' if self_dup[-2]
      self_dup
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scryglass-1.1.0 lib/refinements/clip_string_refinement.rb
scryglass-1.0.1 lib/refinements/clip_string_refinement.rb
scryglass-1.0.0 lib/refinements/clip_string_refinement.rb
scryglass-0.1.0 lib/refinements/clip_string_refinement.rb