Colton Anglin
Colton Anglin

Reputation: 451

Change color of a string - Haskell

I'm making a command line based game in Haskell and need to make a string a certain color. I'm fairly new to Haskell but I made a game last night. I basically need to change the "Welcome to the Haskell Guessing Game" non-colored string to a different colored string if possible.

Upvotes: 6

Views: 4295

Answers (2)

Alexander Kurz
Alexander Kurz

Reputation: 308

The following works for me. You can run this code here.

-- https://ss64.com/nt/syntax-ansi.html for the colours
main = do
  putStrLn $ "\ESC[0mdefault"
  putStrLn $ "\ESC[30mblack"
  putStrLn $ "\ESC[31mred"
  putStrLn $ "\ESC[32mgreen"
  putStrLn $ "\ESC[33myellow"
  putStrLn $ "\ESC[34mblue"
  putStrLn $ "\ESC[35mmagenta"
  putStrLn $ "\ESC[36mcyan"
  putStrLn $ "\ESC[37mwhite"
  putStrLn $ "\ESC[90mblack"
  putStrLn $ "\ESC[91mred"
  putStrLn $ "\ESC[92mgreen"
  putStrLn $ "\ESC[93myellow"
  putStrLn $ "\ESC[94mblue"
  putStrLn $ "\ESC[95mmagenta"
  putStrLn $ "\ESC[96mcyan"
  putStrLn $ "\ESC[97mwhite"

Upvotes: 5

chtenb
chtenb

Reputation: 16184

You cannot color a string. A string is just a sequence of characters. You can however tell a terminal to print a string in certain colors, if the terminal supports it.

So you should use some library which can deal with terminal stuff like that. The System.Console.ANSI module provides ANSI terminal support for Windows and ANSI terminal software running on a UNIX-like operating system, which is likely to suit your needs.

Upvotes: 11

Related Questions