I have multiple strings that look something like this

domain1 = "│ domain1 │ 192.168.1.193 │ HTTP 200  │"
domain2 = "│ domain2 │ 192.168.1.194 │ HTTP DOWN │"
domain3 = "│ domain3 │ 192.168.1.195 │ HTTP DOWN │"
domain4 = "│ domain3 │ 192.168.1.196 │ HTTP 200  │"

Are there any easy way to only colorize the HTTP DOWN to red in curses?

Here is the full code:

#!/usr/bin/env python3
import curses
import os

def draw_menu(stdscr):
    global looped, pingstart

    domains = [
    "│ domain1.com │ 192.168.1.193 │ HTTP 200  │",
    "│ domain2.com │ 192.168.1.194 │ HTTP DOWN │",
    "│ domain3.com │ 192.168.1.195 │ HTTP DOWN │",
    "│ domain3.com │ 192.168.1.196 │ HTTP 200  │"
    ]

    stdscr.clear()
    stdscr.refresh()
    # Start colors in curses
    curses.start_color()
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
    curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
    curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_BLACK)
    curses.init_pair(6, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
    curses.init_pair(8, curses.COLOR_YELLOW, curses.COLOR_BLACK)

    while True:

        stdscr.clear()
        height, width = stdscr.getmaxyx()

        c=0
        for domain in domains:
            stdscr.addstr(2+c, 0, domain)
            c+=1

        time.sleep(1)
        stdscr.refresh()

def main():
    curses.wrapper(draw_menu)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        exit()

The code is simplified a bit. It goes through several steps to get these final strings.

It is probably one of my most messy programs I've ever made, and could probably construct it differently


I tried doing something like this

domain1 = "│ domain1 │ 192.168.1.193 │ HTTP 200  │"
domain2 = "│ domain2 │ 192.168.1.194 │ \033[91mHTTP DOWN\033[0m │"

But curses doesn't support that kind of color formatting

Are there any easier way of doing this?

0 Answers