Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6559215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:17:46+00:00 2026-05-25T13:17:46+00:00

How can I get colors with Ruby’s default Curses library? So something like: puts

  • 0

How can I get colors with Ruby’s default Curses library?

So something like:

puts "\e[0m\e[30;47mtest\e[0m"

works great. Gives a nice black on light gray background. But this:

#!/usr/bin/env ruby

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (for pageup/down)
Curses.stdscr.nodelay = 1

Curses.clear
Curses.setpos(0,0)
Curses.addstr "Hello!"
Curses.addstr "\e[0m\e[30;47mHello!\e[0m"

Displays:

Hello!

^[[0m^[[30;47mHello!^[[0m

Ultimately, I need colors for this:

How to capture a key press in Ruby?

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)

loop do
  case Curses.getch
  when Curses::Key::PPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Up")
  when Curses::Key::NPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Dn")
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:17:46+00:00Added an answer on May 25, 2026 at 1:17 pm

    There are examples of curses usage in the ruby source code, see, e.g. here.

    Your code could be something like:

    require 'curses'
    include Curses
    
    Curses.noecho # do not show typed keys
    Curses.init_screen
    Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)
    Curses.start_color
    # Determines the colors in the 'attron' below
    Curses.init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_BLACK) 
    Curses.init_pair(COLOR_RED,COLOR_RED,COLOR_BLACK)
    
    loop do
    
      case Curses.getch
    
      when Curses::Key::PPAGE
        Curses.clear
        Curses.setpos(0,0)
        # Use colors defined color_init
        Curses.attron(color_pair(COLOR_RED)|A_NORMAL){
          Curses.addstr("Page Up")
        }
      when Curses::Key::NPAGE
        Curses.clear
        Curses.setpos(0,0)
        Curses.attron(color_pair(COLOR_BLUE)|A_NORMAL){
          Curses.addstr("Page Down")
        }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a Ruby library that can list all of the colors
I can get the element like this $(#txtEmail) but I'm not sure how to
For image scanning purposes, I'd like a pixel (which I can get from a
How can I make the puts commands I output from a commandline based ruby
I would like know whether we can highlight text (colors) of already created PDF
I've a string like Colors: yellow, green, white. I need to get an array
I was just wondering if you can get two colours in a label so
How can I get the color of a pixel at the location of the
How can I convert a System.Windows.Media.Brush to System.Drawing.Brush? I'm trying to get the color
Can get all triples with value null in specific field? All people with date_of_birth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.