Permalink
Browse files

create core_ext.rb for the few borrowed Facets methods

  • Loading branch information...
1 parent d2cc4bf commit 5c4fe7b7db948375f52d59acc1b07d902440bc6a @trans trans committed Dec 25, 2009
Showing with 32 additions and 8 deletions.
  1. +31 −0 lib/turn/core_ext.rb
  2. +1 −0 lib/turn/reporter.rb
  3. +0 −8 lib/turn/reporters/progress_reporter.rb
View
@@ -0,0 +1,31 @@
+# Borrowed methods from Ruby Facets.
+
+class String
+
+ # Aligns each line n spaces.
+ def tab(n)
+ gsub(/^ */, ' ' * n)
+ end
+
+ # Preserves relative tabbing.
+ # The first non-empty line ends up with n spaces before nonspace.
+ def tabto(n)
+ if self =~ /^( *)\S/
+ indent(n - $1.length)
+ else
+ self
+ end
+ end
+
+ # Indent left or right by n spaces.
+ # (This used to be called #tab and aliased as #indent.)
+ def indent(n, c=' ')
+ if n >= 0
+ gsub(/^/, c * n)
+ else
+ gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "")
+ end
+ end
+
+end
+
View
@@ -1,5 +1,6 @@
module Turn
require 'turn/colorize'
+ require 'turn/core_ext'
# = Reporter
#
@@ -1,14 +1,6 @@
require 'turn/reporter'
require 'ansi/progressbar'
-class String
- # Used to indent strings.
- # (method borrowed from Ruby Facets).
- def tab(n)
- gsub(/^ */, ' ' * n)
- end
-end
-
module Turn
#

0 comments on commit 5c4fe7b

Please sign in to comment.