Sha256: 77e9d42f292193db174e092a2845f4c1d7c3f5197faaef577afeba55b72bc257

Contents?: true

Size: 712 Bytes

Versions: 2

Compression:

Stored size: 712 Bytes

Contents

# frozen_string_literal: true

module DNote
  # Extensions for String class.
  # These methods are taken directly from Ruby Facets.
  #
  module StringExt
    # Indent left or right by num spaces.
    # (This used to be called #tab and aliased as #indent.)
    #
    #  CREDIT: Gavin Sinclair
    #  CREDIT: Trans

    def indent(num)
      if num >= 0
        gsub(/^/, " " * num)
      else
        gsub(/^ {0,#{-num}}/, "")
      end
    end

    def tabset(num)
      i = lines.map do |line|
        line.strip.empty? ? nil : line.index(/\S/)
      end
      x = i.compact.min
      t = num - x.to_i
      t = 0 if t < 0
      indent(t)
    end
  end
end

String.class_eval do
  include DNote::StringExt
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mvz-dnote-1.10.0 lib/dnote/core_ext.rb
mvz-dnote-1.9.0 lib/dnote/core_ext.rb