Sha256: 70c8d42d793f1f2f4d83fe25e3d4f12899d439a3e99dedb446029daed4a234fb

Contents?: true

Size: 836 Bytes

Versions: 3

Compression:

Stored size: 836 Bytes

Contents

require 'unicode/display_width'

module Bankscrap
  module Utils
    class CliString < String
      def ljust(to_length, padstr = ' ')
        self.class.new(self + padding(to_length, padstr))
      end

      def rjust(to_length, padstr = ' ')
        self.class.new(padding(to_length, padstr) + self)
      end

      def truncate(truncate_at)
        if display_width > truncate_at
          self.class.new(self[0..-2]).truncate(truncate_at)
        else
          self.class.new(self)
        end
      end

      private

      def padding(to_length, padstr)
        padstr.to_str * padding_length(to_length)
      end

      def padding_length(to_length)
        [
          0,
          to_length - display_width
        ].max
      end

      def display_width
        Unicode::DisplayWidth.of(self)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bankscrap-2.1.1 lib/bankscrap/utils/cli_string.rb
bankscrap-2.1.0 lib/bankscrap/utils/cli_string.rb
bankscrap-2.0.6 lib/bankscrap/utils/cli_string.rb