Skip to content

Commit

Permalink
Merge pull request #2 from bendyworker/master
Browse files Browse the repository at this point in the history
Added movement methods
  • Loading branch information
randland committed Dec 9, 2011
2 parents 442032a + 4e6938c commit 4800766
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.markdown
Expand Up @@ -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.
3 changes: 3 additions & 0 deletions Rakefile
@@ -1 +1,4 @@
require "bundler/gem_tasks"

task(:default).clear
task :default => [:spec]
13 changes: 13 additions & 0 deletions lib/colorful.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4800766

Please sign in to comment.