Sha256: a8c5fd9852b0f578faaf270500ce7b9eb42e814eb75a5490b09663dcdd920457

Contents?: true

Size: 1.58 KB

Versions: 14

Compression:

Stored size: 1.58 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2013
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>

class String
  alias :starts_with?	:start_with?
  alias :ends_with?  	:end_with?

  def whackuum str=/\s+/
    self.split(str).map(&:strip)
  end

  ANSIColorRegexp = Regexp.new(/(?: \e \[ \d+ (?: ;\d+)* m )+/x)

  def truncate length, omission: "..."
    return self if self.length <= length

    ss        	= StringScanner.new(self)
    out       	= ss.scan(ANSIColorRegexp) || ""
    cut_length	= length - omission.str_length
    used      	= 0
    cut_at    	= 0

    while used < length and not ss.eos?
      out << ss.getch
      c = ss.scan(ANSIColorRegexp)
      out << c unless c.nil?
      used += 1

      cut_at = out.length if used == cut_length
    end

    if not ss.eos?
      out = out[0, cut_at]
      out << omission
    end

    out
  end

  def first limit=1
    if limit == 0      	; ""
    elsif limit >= size	; dup
    else               	; self[0..(limit - 1)]
    end
  end

  def last limit=1
    if limit == 0      	; ""
    elsif limit >= size	; dup
    else               	; self[(-limit)..-1]
    end
  end

  def indent! amount, indent_string = nil, indent_empty_lines = false
    indent_string = indent_string || self[/^[ \t]/] || " "
    re = indent_empty_lines ? /^/ : /^(?!$)/
    gsub!(re, indent_string * amount)
  end

  def indent amount, indent_string = nil, indent_empty_lines = false
    dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
  end

  def color name
    HighLine.color(self, name)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
muflax-0.3.21 lib/muflax/string.rb
muflax-0.3.20 lib/muflax/string.rb
muflax-0.3.19 lib/muflax/string.rb
muflax-0.3.18 lib/muflax/string.rb
muflax-0.3.17 lib/muflax/string.rb
muflax-0.3.16 lib/muflax/string.rb
muflax-0.3.14 lib/muflax/string.rb
muflax-0.3.13 lib/muflax/string.rb
muflax-0.3.12 lib/muflax/string.rb
muflax-0.3.11 lib/muflax/string.rb
muflax-0.3.10 lib/muflax/string.rb
muflax-0.3.9 lib/muflax/string.rb
muflax-0.3.8 lib/muflax/string.rb
muflax-0.3.7 lib/muflax/string.rb