代码之家  ›  专栏  ›  技术社区  ›  Evolve

如何输出不同颜色的ruby命令行文本

  •  39
  • Evolve  · 技术社区  · 16 年前

    如何使我从基于命令行的ruby程序输出的puts命令变为彩色? 如果您能提及我如何称呼每种不同的颜色,我将不胜感激。

    puts "The following word is blue.. Im Blue!"
    puts "The following word is green.. Im Green!"
    puts "The following word is red.. Im Red!"
    

    我得到了不同的文字,我想要不同的颜色,我想要,你明白了。

    我正在使用Ubuntu,我是否需要改变我的方法,以便程序在diff os中正确输出?

    10 回复  |  直到 12 年前
        1
  •  57
  •   Veger    15 年前

    我发现 this article 描述一种将彩色文本写入控制台的非常简单的方法。这篇文章描述了这个小例子,它似乎做到了这一点(我冒昧地稍微改进了一下):

    def colorize(text, color_code)
      "\e[#{color_code}m#{text}\e[0m"
    end
    
    def red(text); colorize(text, 31); end
    def green(text); colorize(text, 32); end
    
    # Actual example
    puts 'Importing categories [ ' + green('DONE') + ' ]'
    puts 'Importing tags       [' + red('FAILED') + ']'
    

    在使用windowsxp时,作者提到了一个名为 win32console

        2
  •  38
  •   Mark Carey    16 年前

    我发现 Colored gem 使用起来最简单、最干净。

    puts "this is red".red
    puts "this is red with a blue background (read: ugly)".red_on_blue
    puts "this is red with an underline".red.underline
    puts "this is really bold and really blue".bold.blue
    logger.debug "hey this is broken!".red_on_yellow 
    
        3
  •  6
  •   darek129    16 年前

    begin
       require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
    rescue LoadError
       raise 'You must gem install win32console to use color on Windows'
    end
    
    class Colors
       COLOR1 = "\e[1;36;40m"
       COLOR2 = "\e[1;35;40m"
       NOCOLOR = "\e[0m"
       RED = "\e[1;31;40m"
       GREEN = "\e[1;32;40m"
       DARKGREEN = "\e[0;32;40m"
       YELLOW = "\e[1;33;40m"
       DARKCYAN = "\e[0;36;40m"
    end
    
    class String
       def color(color)
          return color + self + Colors::NOCOLOR
       end
    end
    

    "Hello World".color(Colors::DARKGREEN)
    

    要了解所有颜色,只需执行以下操作:

    begin
      require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
    rescue LoadError
      raise 'You must gem install win32console to use color on Windows'
    end
    
    [0, 1, 4, 5, 7].each do |attr|
      puts '----------------------------------------------------------------'
      puts "ESC[#{attr};Foreground;Background"
      30.upto(37) do |fg|
        40.upto(47) do |bg|
          print "\033[#{attr};#{fg};#{bg}m #{fg};#{bg}  "
        end
      puts "\033[0m"
      end
    end
    
        4
  •  5
  •   glasz    15 年前

    使用转义序列 \033 而不是 \e

        5
  •  4
  •   br3nt    10 年前

    我想我会添加另一个解决方案,因为它做的事情有点不同,包括更多的颜色代码:

    首先是一些例子。。。

    使用方法链接:

    String.include(AnsiTextStyles)
    
    puts "How are you?".blue.bold + " " + 'I am good!'.red.bold
    puts '%s %s' % ["How are you?".blue.bold, 'I am good!'.red.bold]
    

    使用 style 方法并应用多个属性:

    puts "How are you?".style(:red)
    puts 'I am good!'.style(:blue, :underline)
    puts 'Good to hear'.style([:bg_magenta, :blink])
    

    text_styles = {
        red_bold:       [:red, :bold],
        blue_underline: [:blue, :underline],
        pretty:         [:bg_magenta, :blink],
      }
    
    text_styles.each do |name, style|
      styled_text = "Text styled multiple ways".style(style)
      puts "%s: %s" % [name, styled_text]
    end
    

    我还举了几个例子 this gist I created 并对代码进行了扩展,以包括细化,从而限定对字符串的修改范围。

    module AnsiTextStyles
    
      TEXT_ATTRIBUTES = {
          # text properties
          none: 0, # turn off all attributes
          bold: 1, bright: 1, # these do the same thing really
          italic: 3, underline: 4, blink: 5,
          reverse: 7, # swap foreground and background colours
          hide: 8, # foreground color same as background
    
          # foreground colours
          black: 30, grey: 90, lt_grey: 37, :white => 97,
          red: 31, lt_red: 91, 
          green: 32, lt_green: 92,
          dk_yellow: 33, brown: 33, yellow: 93,
          blue: 34, lt_blue: 94,
          magenta: 35, pink: 95, lt_magenta: 95,
          cyan: 36, lt_cyan: 96,
          default: 39,
    
          # background colours
          bg_black: 40, bg_grey: 100, bg_lt_grey: 47, bg_white: 107,
          bg_red: 41, bg_lt_red: 101,
          bg_green: 42, bg_lt_green: 102,
          bg_dk_yellow: 43, bg_brown: 43, bg_yellow: 103,
          bg_blue: 44, bg_lt_blue: 104,
          bg_magenta: 45, bg_pink: 105, bg_lt_magenta: 105,
          bg_cyan: 46, bg_lt_cyan: 106,
        }
    
      def self.text_attributes
        TEXT_ATTRIBUTES.keys
      end
    
      # applies the text attributes to the current string
      def style(*text_attributes)
        codes = TEXT_ATTRIBUTES.values_at(*text_attributes.flatten).compact
        "\e[%sm%s\e[m" % [codes.join(';'), self.to_s]
      end
    
    end
    
        6
  •  1
  •   liwp    16 年前

    puts "The following word is blue.. \e[34mIm Blue!\e[0m"
    puts "The following word is green.. \e[32mIm Green!\e[0m"
    puts "The following word is red.. \e[31mIm Red!\e[0m"
    

    ASCII码还支持下划线、闪烁和突出显示文本等功能。

    helper library 可用于为您处理实际ASCII代码。

    编辑 win32console gem 这似乎解决了这个问题。

    您可以使用以下代码段(在链接到的页面上找到)仅在windows上加载Win32 Console库:

    begin
      require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
    rescue LoadError
      raise 'You must gem install win32console to use color on Windows'
    end
    
        7
  •  1
  •   Chris    15 年前
        8
  •  1
  •   J-_-L    15 年前

    我的建议是: paint 宝石它不强制执行字符串扩展,并支持256色(对于非256色的终端,使用后退模式)。

    用法:

    puts Paint["I'm blue!", :blue]
    puts Paint["I'm dark blue if your terminal supports it!", "#000044"]
    
        10
  •  1
  •   mpz    11 年前

    使用彩色宝石!过来看:

    https://github.com/fazibear/colorize

    安装:

    sudo gem install colorize
    

    require 'colorize'
    
    puts "I am now red.".red
    puts "I am now blue.".green
    puts "I am a super coder".yellow