Each code from 0 to 255 corresponds to a particular color. Here we use instead of print so we can print multiple items on the same line, but otherwise it's pretty self-explanatory. To begin with, let's start off with a plain-old vanilla Python prompt:
This post will explore the basics of what Ansi escape codes exist, and demonstrate how to use them to write your own interactive command-line from first principles: Nevertheless, it's possible to write programs that make use of ANSI escape codes, and at least will work on common Unix systems like Ubuntu or OS-X (though not Windows, which I won't cover here and is its own adventure!). Wikipedia has a reasonable listing of them, as do many other sites. Various terminals support different subsets of these codes, and it's difficult to find a "authoritative" list of what every code does. These are special codes that your program can print in order to give the terminal instructions. The way that most programs interact with the Unix terminal is through ANSI escape codes. If you enjoyed the contents on this blog, you may also enjoy Haoyi's book Hands-on Scala Programming
This post will explore the basics of how you can control the terminal from any command-line program, with examples in Python, and how your own code can directly make use of all the special features the terminal has to offer.Ībout the Author: Haoyi is a software engineer, and the author of many open-source Scala tools such as the Ammonite REPL and the Mill Build Tool. There are libraries like Readline, JLine, or the Python Prompt Toolkit that help you do this in various programming languages, but you can also do it yourself. This is what lets programs like Git implement its dynamic progress indicators, and Vim or Bash implement their editors that let you modify already-displayed text without scrolling the terminal. I Know, I am very late but, May be my answer can help someone.Strategic Scala Style: Designing Datatypes Scala Scripting and the 15 Minute Blog EngineĮveryone is used to programs printing out output in a terminal that scrolls as new text appears, but that's not all your can do: your program can color your text, move the cursor up, down, left or right, or clear portions of the screen if you are going to re-print them later. Have a look at example.c and test.cpp for C and C++ examples.
Printf("\n \n \t This is dummy program for text color ") Void ClearConsoleToColors(int ForgC, int BackC)
WORD wColor = ((BackC & 0x0F) //header file for windows The code is - void ClearConsoleToColors(int ForgC, int BackC) Now I am going to give the code of ChangeConsoleToColors. Now I am going to give you the code of each color - Name | Value When you run the program you will get the text color in RED. Printf("\n \n \t This text is written in Red Color \n ") For example I am taking your sample program - #include
To use this function you need to call it from your program. SetConsoleTextAttribute(hStdOut, wColor) WColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F) Mask out all but the background attribute, and add in the forgournd color
If(GetConsoleScreenBufferInfo(hStdOut, &csbi)) HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE) The code of SetColor is - void SetColor(int ForgC) You copy paste these functions code in your program and do the following steps.The code I am giving will not work in some compilers. I am going to give two function SetColor and ChangeConsoleToColors. In today's compilers these functions would not work. Functions like textcolor worked in old compilers like turbo C and Dev C.