Using ansicolor

http://term-ansicolor.rubyforge.org/

I did a successful gem install of term-ansicolor and am trying to use
it:

include Term::ANSIColor
print red, bold, “red bold”, reset, “\n”

gives me:

uninitialized constant Term

I tried all kinds of variations, found some code where other people
used this and tried all that out. No luck.

Is this a problem with my Ruby or gems installation?

Sy,

On 4/12/06, Sy Ali [email protected] wrote:

I tried all kinds of variations, found some code where other people
used this and tried all that out. No luck.

Is this a problem with my Ruby or gems installation?

make sure you require the correct library (you might need to require
‘rubygems’ first)

require “lib/term/ansicolor”

(why the maintainer chose to put the lib/ prefix, I don’t really know)

Cameron

Hi,

Sy Ali wrote:

I tried all kinds of variations, found some code where other people
used this and tried all that out. No luck.

Is this a problem with my Ruby or gems installation?

It seems to work fine here:

closure:~ flori$ ruby
require ‘term/ansicolor’
include Term::ANSIColor
print red, bold, “red bold”, reset, “\n”
red bold

Have you required the library before trying to use it?

Florian

On Apr 12, 2006, at 12:18 AM, Sy Ali wrote:

http://term-ansicolor.rubyforge.org/

Just FYI, HighLine can also do ANSI color, and much, much more… :slight_smile:

James Edward G. II

On 4/12/06, Cameron McBride [email protected] wrote:

make sure you require the correct library (you might need to require
‘rubygems’ first)

That was the solution. I needed to require rubygems. Apparently, I
thought everything was more magical. :wink:

require ‘rubygems’
require ‘term/ansicolor’
include Term::ANSIColor
print red, bold, “red bold”, reset, “\n”

On 4/12/06, James Edward G. II [email protected] wrote:

Just FYI, HighLine can also do ANSI color, and much, much more… :slight_smile:

Thanks. I went poking around for a solution and a quick search came
up with ansicolor. I’ll investigate this one when I have a bit of
time.

http://highline.rubyforge.org/
http://www.rubyquiz.com/quiz29.html

On 4/12/06, Sy Ali [email protected] wrote:

On 4/12/06, James Edward G. II [email protected] wrote:

Just FYI, HighLine can also do ANSI color, and much, much more… :slight_smile:

Thanks. I went poking around for a solution and a quick search came
up with ansicolor. I’ll investigate this one when I have a bit of
time.

http://highline.rubyforge.org/
Ruby Quiz - HighLine (#29)

Silly me, this is Ruby. Although nothing is straightforward, google
gave me the answers I needed:

require ‘rubygems’
require ‘highline/import’
say(“This is <%= color(‘red bold’, red, bold) %>!”)

On 4/12/06, Sy Ali [email protected] wrote:

Silly me, this is Ruby. Although nothing is straightforward, google
gave me the answers I needed:

I’m not sure that made english sense. Let’s try that again.

Silly me, “a bit of time” for Ruby really is only a small bit of time.
I have some time right now so I’ll play with it.

I played with it, and like everything else I’ve seen I’ll need to
learn a bit more to get it to work. However, I quickly googled for
someone else’s code and easily adapted it to my needs:

require ‘rubygems’
require ‘highline/import’
say(“This is <%= color(‘red bold’, red, bold) %>!”)

Sy Ali wrote:

That was the solution. I needed to require rubygems. Apparently, I
thought everything was more magical. :wink:

require ‘rubygems’
require ‘term/ansicolor’
include Term::ANSIColor
print red, bold, “red bold”, reset, “\n”

You can set RUBYOPT=-rubygems in your environment, if you don’t want to
require ‘rubygems’. So users can transparently either use gems or
install from source or use their package managers.

On Wed, 12 Apr 2006, Florian F. wrote:

Florian
that’s good stuff florian! any idea how to make this work? (it’s a
snippet out
of something i’m working on attm):

 harp:~ > cat a.rb
 require 'readline'
 require 'rubygems'
 require_gem 'term-ansicolor'
 class ::String; include Term::ANSIColor; end


 complete = lambda do |words|
   words = words.strip.split(%r/\s+/) unless Array === words
   Readline::completion_proc = lambda do |s|
     list = words.select{|c| c.index s}
     list = list.empty? ? words : list
     list.map{|word| word.blue}
   end
 end

 sif_in = []

 readline =
   if STDIN.tty?
     lambda{|prompt| line = 

Readline::readline(prompt.strip.red.dark.bold.underline << "
").to_s.strip; sif_in << line; line.empty? ? nil : line}
else
lambda{|prompt| line = STDIN.gets.to_s.strip; sif_in << line;
line.empty? ? nil : line}
end

 complete['one two three']
 line = readline['number >']
 puts line

the interaction between the completion_proc and term-ansicolor results
in the
the escape codes being shown literally? thoughts?

thanks.

-a

On Thu, 13 Apr 2006, Sy Ali wrote:

On 4/12/06, Florian F. [email protected] wrote:

You can set RUBYOPT=-rubygems in your environment

Aah, good point. Maybe I should also recommend this for the guys who
handle the packages for my Linux distro.

be careful. it’s breaks some packages, like the gsl.

-a

On 4/12/06, [email protected] [email protected] wrote:

On Thu, 13 Apr 2006, Sy Ali wrote:

On 4/12/06, Florian F. [email protected] wrote:

You can set RUBYOPT=-rubygems in your environment

Aah, good point. Maybe I should also recommend this for the guys who
handle the packages for my Linux distro.

be careful. it’s breaks some packages, like the gsl.

Always a catch. Ok, I’d rather insert it into my code manually every
time… at this point I"m just goofing around anyways.

On 4/12/06, Florian F. [email protected] wrote:

You can set RUBYOPT=-rubygems in your environment

Aah, good point. Maybe I should also recommend this for the guys who
handle the packages for my Linux distro.

On Thu, 13 Apr 2006, Jim W. wrote:

be careful. it’s breaks some packages, like the gsl.

Does gsl just not work with gems at all? Or is it that setting RUBYOPT
breaks it?

there is a superclass conflict with with the Rational class. afaik this
is
the fault of the gsl…

i can send the error message later - it’s on a box at home.

cheers.

-a

unknown wrote:

On Thu, 13 Apr 2006, Sy Ali wrote:

On 4/12/06, Florian F. [email protected] wrote:

You can set RUBYOPT=-rubygems in your environment

Aah, good point. Maybe I should also recommend this for the guys who
handle the packages for my Linux distro.

be careful. it’s breaks some packages, like the gsl.

Does gsl just not work with gems at all? Or is it that setting RUBYOPT
breaks it?

Thanks?


– Jim W.