First, why we need this feature. Imagine working on a project for hours and suddenly realizing that the text is blending with the background, making it impossible to read. Or worse yet, accidentally changing the font size or style, causing your eyes to strain and your head to spin.
Relax, it’s all good! With Python’s curses module, you can customize every aspect of your terminal colors, from the foreground to the background, and everything in between. And let me tell you, it’s easier than pie (or should I say, cake?).
To get started, import the curses library at the beginning of your script:
# Import the curses library to access its functions and methods
import curses
# Initialize the curses screen
screen = curses.initscr()
# Turn off automatic echoing of keys to the screen
curses.noecho()
# Enable keypad mode to capture special keys such as arrow keys
screen.keypad(True)
# Set the cursor to invisible
curses.curs_set(0)
# Start colors in curses
curses.start_color()
# Define color pairs for foreground and background colors
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_BLACK)
# Clear the screen and refresh
screen.clear()
screen.refresh()
# Print a message in red
screen.addstr("Welcome to the colorful world of curses!", curses.color_pair(1))
# Move the cursor to the next line
screen.move(1, 0)
# Print a message in green
screen.addstr("You can customize every aspect of your terminal colors.", curses.color_pair(2))
# Move the cursor to the next line
screen.move(2, 0)
# Print a message in yellow
screen.addstr("And let me tell you, it's easier than pie (or should I say, cake?).", curses.color_pair(3))
# Move the cursor to the next line
screen.move(3, 0)
# Refresh the screen to show the changes
screen.refresh()
# Wait for user input
screen.getch()
# End the curses screen
curses.endwin()
Now that we have our tools ready, Let’s get started with some colorful examples. First, to change the foreground and background colors for a specific text, you can use the `color_pair()` function. This function takes an integer as its argument, which represents the pair of colors you want to use. For example:
# Initialize the screen
screen = curses.initscr()
# Enable color support
curses.start_color()
# Set up custom color pairs
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) # Creates a color pair with red foreground and black background
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE) # Creates a color pair with green foreground and white background
# Print text using the custom colors
screen.addstr(0, 0, "Hello World! ", curses.color_pair(1)) # Prints "Hello World!" with the color pair created in line 7
screen.addstr(2, 0, "This is a test.", curses.color_pair(2)) # Prints "This is a test." with the color pair created in line 8
In this example, we first initialize the screen using `curses.initscr()`, enable color support with `curses.start_color()`, and then create two custom color pairs (1 for red and black, 2 for green and white) using `curses.init_pair()`. Finally, we print some text using these custom colors by calling the `addstr()` function and passing it our desired string and color pair as arguments.
You can also change the default foreground and background colors for your entire terminal window using the `init_pair()` function with a special argument of 0:
# Import the curses module
import curses
# Initialize the color red with RGB values (255, 0, 0)
curses.init_color(15, 255, 0, 0)
# Initialize the color green with RGB values (0, 255, 0)
curses.init_color(16, 0, 255, 0)
# Initialize the curses color settings
curses.start_color()
# Initialize the screen
screen = curses.initscr()
# Set the terminal to raw mode
curses.raw()
# Disable automatic echoing of keypresses
curses.noecho()
# Enable cbreak mode, allowing for immediate input without waiting for Enter key
curses.cbreak()
# Disable newline mode, preventing Enter key from adding a new line
curses.nonl()
# Enable interrupt flushing for the standard input
curses.intrflush(sys.stdin, True)
# Set up custom color pairs
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) # pair 1 has red foreground and black background
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE) # pair 2 has green foreground and white background
# Print text using the custom color pairs
screen.addstr(0, 0, "Hello World! ", curses.color_pair(1)) # prints "Hello World!" with red foreground and black background
screen.addstr(2, 0, "This is a test.", curses.color_pair(2)) # prints "This is a test." with green foreground and white background
In this example, we first set up the default colors using `curses.init_color()`, which takes four arguments: RGB values for red, green, blue, and transparency (in that order). We then enable color support with `curses.start_color()`. The rest of the code is similar to our previous example.
Now you might be wondering, “Can I change the colors on-the-fly?” And the answer is yes! You can use the `init_pair()` function at any time during your program’s execution:
# Import the curses library
import curses
# Initialize the screen
screen = curses.initscr()
# Print some text using default colors
screen.addstr(0, 0, "Hello World! ")
# Enable color support
curses.start_color()
# Set up custom color pairs for later use
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) # pair 1: red foreground and black background
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE) # pair 2: green foreground and white background
# Print some text using the new colors
screen.addstr(10, 0, "This is a test.", curses.color_pair(1)) # use pair 1 for text
screen.addstr(12, 0, "And this is another line! ", curses.color_pair(2)) # use pair 2 for text
# Refresh the screen to show changes
screen.refresh()
# Wait for user input
screen.getch()
# End the curses session
curses.endwin()
# Explanation:
# - Import the curses library to access its functions
# - Initialize the screen to create a new window for displaying text
# - Use the addstr() function to print text at a specific location on the screen
# - Enable color support with the start_color() function
# - Use the init_pair() function to set up custom color pairs for later use
# - Use the color_pair() function to specify which color pair to use for text
# - Refresh the screen to show any changes made
# - Use the getch() function to wait for user input before ending the program
# - End the curses session to return the terminal to its original state
In this example, we first print some text using the default colors, then set up two custom color pairs (same as before), and finally print more text using these new colors.
And that’s it! You now have full control over your terminal colors in Python using curses module. So go ahead, experiment with different color combinations, and let your creativity run wild!