Permalink
Browse files

Merge pull request #2 from bendyworker/master

Added movement methods
  • Loading branch information...
2 parents 442032a + 4e6938c commit 4800766ac067fd01abd5ce0e92eabc90fc496d22 @randland randland committed Dec 9, 2011
Showing with 37 additions and 0 deletions.
  1. +21 −0 README.markdown
  2. +3 −0 Rakefile
  3. +13 −0 lib/colorful.rb
View
@@ -69,4 +69,25 @@ These methods handle standard or shortened HTML codes, case insensitive, with or
Note that these methods require xterm 256 color support, and colors will be translated to the nearest possible valid color.
+Lastly, some methods have been added that allow for cursor movement. The supported movement operations include:
+* and_go_up(n)
+* and_go_down(n)
+* and_go_left(n)
+* and_go_right(n)
+* and_go_to(n)
+
+To return to the beginning of the line:
+
+ 10.times do |n|
+ print n.to_s.and_go_to 0
+ end
+
+or:
+
+ 10.times do |n|
+ puts n.to_s.and_go_up 1
+ end
+
+Note that some methods work better with puts, and others with print. Puts will implicitly add a new line to the end of the string it is printing, moving the cursor.
+
This is a work in progress but is stable. Let me know if you would like a feature added to the project.
View
@@ -1 +1,4 @@
require "bundler/gem_tasks"
+
+task(:default).clear
+task :default => [:spec]
View
@@ -19,6 +19,12 @@ module Colorful
:blink => 5,
:inverse => 7,
:hide => 8 }
+
+ MOVEMENT = { :up => 'F',
+ :down => 'E',
+ :left => 'D',
+ :right => 'C',
+ :to => 'G' }
end
class String
@@ -49,6 +55,13 @@ class String
end
end
+ Colorful::MOVEMENT.each do |move, code|
+ define_method "and_go_#{move}" do |num|
+ result = self
+ result += "\e[#{num}#{code}"
+ end
+ end
+
def foreground(*args)
code = ansi_color_code *args
inject_ansi_code 38, 5, code

0 comments on commit 4800766

Please sign in to comment.