Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add String methods for terminal colour

This commit adds the following methods:

 * String#black
 * String#red
 * String#green
 * String#yellow
 * String#blue
 * String#magenta
 * String#cyan
 * String#white
  • Loading branch information...
commit 3b568583669d34af5e1e3b60efd98390ed1815e1 1 parent 1a2fffc
@vinnydiehl authored
View
35 lib/nutella/core_ext/string.rb
@@ -1,34 +1 @@
-class String
- alias contains? include?
- alias includes? include?
-
- alias each each_char
-
- # The inverse of <tt>String#include?</tt>.
- #
- # @param [String] str the string to test for exclusion
- # @return [Boolean] whether or not the string excludes the substring +str+
- def exclude?(str)
- !include? str
- end
- alias excludes? exclude?
-
- # Left-aligns a heredoc by finding the least indented line in the string, and
- # removing that amount of leading whitespace from each line.
- #
- # @example Prints the contents of the heredoc as described on each line
- # puts <<-EOS.heredoc
- # This line would be left-aligned.
- # This line too.
- # This line would be indented by four spaces.
- # As well as this line.
- # And back to left-aligned.
- # EOS
- #
- # @return [String] the string with excess leading whitespace stripped
- def heredoc
- gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size}}/, ""
- end
- alias format_heredoc heredoc
- alias strip_heredoc heredoc
-end
+Dir[File.expand_path("../string/**/*.rb", __FILE__)].each { |f| require f }
View
6 lib/nutella/core_ext/string/aliases.rb
@@ -0,0 +1,6 @@
+class String
+ alias contains? include?
+ alias includes? include?
+
+ alias each each_char
+end
View
7 lib/nutella/core_ext/string/colour.rb
@@ -0,0 +1,7 @@
+class String
+ colours = %w[black red green yellow blue magenta cyan white]
+ colours.concat colours.map { |c| "light#{c}" }
+ colours.each_with_index do |colour, i|
+ define_method(colour) { "\e[38;5;#{i}m#{self}\e[0m" }
+ end
+end
View
10 lib/nutella/core_ext/string/exclude.rb
@@ -0,0 +1,10 @@
+class String
+ # The inverse of <tt>String#include?</tt>.
+ #
+ # @param [String] str the string to test for exclusion
+ # @return [Boolean] whether or not the string excludes the substring +str+
+ def exclude?(str)
+ !include? str
+ end
+ alias excludes? exclude?
+end
View
20 lib/nutella/core_ext/string/heredoc.rb
@@ -0,0 +1,20 @@
+class String
+ # Left-aligns a heredoc by finding the least indented line in the string, and
+ # removing that amount of leading whitespace from each line.
+ #
+ # @example Prints the contents of the heredoc as described on each line
+ # puts <<-EOS.heredoc
+ # This line would be left-aligned.
+ # This line too.
+ # This line would be indented by four spaces.
+ # As well as this line.
+ # And back to left-aligned.
+ # EOS
+ #
+ # @return [String] the string with excess leading whitespace stripped
+ def heredoc
+ gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size}}/, ""
+ end
+ alias format_heredoc heredoc
+ alias strip_heredoc heredoc
+end
Please sign in to comment.
Something went wrong with that request. Please try again.