Sha256: 4d38c9254b9fbd78f91f6251adeddd6497f5db1bb11e0910288d93be659fd257

Contents?: true

Size: 943 Bytes

Versions: 15

Compression:

Stored size: 943 Bytes

Contents

module Hirb
  module Unicode
    module StringUtil
      def size(string)
        string.display_width
      end

      def slice(string, offset, width)
        chars = string.chars.to_a[offset..-1].to_a

        current_length = 0
        split_index = 0
        chars.each_with_index do |c, i|
          char_width = self.size(c)
          break if current_length + char_width > width
          split_index = i+1
          current_length += char_width
        end

        split_index ||= chars.count
        head = chars[0, split_index].join
        head
      end

      def ljust(string, desired_width)
        leftover = desired_width - size(string)
        leftover > 0 ? string + " " * leftover : string
      end

      def rjust(string, desired_width)
        leftover = desired_width - size(string)
        leftover > 0 ? " " * leftover + string : string
      end

    end
  end
end

Hirb::String.extend(Hirb::Unicode::StringUtil)

Version data entries

15 entries across 14 versions & 3 rubygems

Version Path
hirb-unicode-steakknife-0.0.9 lib/hirb/unicode/string_util.rb
hirb-unicode-steakknife-0.0.8 lib/hirb/unicode/string_util.rb
hirb-unicode-steakknife-0.0.7 lib/hirb/unicode/string_util.rb
hirb-unicode-steakknife-0.0.6 lib/hirb/unicode/string_util.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.2 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
climine-0.0.1 vendor/bundle/ruby/2.0.0/gems/hirb-unicode-0.0.5/lib/hirb/unicode/string_util.rb
hirb-unicode-0.0.5 lib/hirb/unicode/string_util.rb
hirb-unicode-0.0.4 lib/hirb/unicode/string_util.rb
hirb-unicode-0.0.3 lib/hirb/unicode/string_util.rb