changing text color in c


 
Thread Tools Search this Thread
Top Forums Programming changing text color in c
# 1  
Old 04-17-2010
changing text color in c

I'm writing my own Unix ls command in c and I was wondering if anyone knows how to or can point me to a tutorial that shows me how to change the text color of the filename depending on if it's a directory, regular file, linked file, etc..., like the real ls command does? Thanks.
# 2  
Old 04-17-2010
You can use the \e with the different values for making the colors.

Code:
 printf("\e[35m["); //This will show a maroon/pink like color

Like wise you can use different numbers for showing appropriate colors.
# 3  
Old 04-17-2010
Yeah, basically you print the escape character (either \e or \033, I prefer the latter because I'm used to it and \e isn't actually standard C - although as far as I know it is widely supported), an opening square bracket ('['), numbers separated by semicolons and the letter 'm' (other letters are used for other types of escape codes).
To get back to the default, you must use '0' as the number. If several numbers are specified they will be executed in the order they appear, thus if two incompatible options are set (like green and red at the same time), the last one will be effective. You can prefix all your options with a '0' like "\033[0;<your options here>m", in case some of them are already set.
Don't forget to print a "\033[0m" at the end if you don't want the rest of your output to be messed.
Code:
printf("\033[0;31mhello, world\033[0m\n"); // prints "hello, world" in red

For a reference on ANSI escape codes, see here
# 4  
Old 04-17-2010
Using curses will keep terminal specific escape sequences out of your program.
# 5  
Old 04-19-2010
Are there any non-ANSI terminals left in this world?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - changing a color of a substring

Hello! I need to write a bash script for my university classes, and I came up with an idea of a program that would test the speed of typing - there is some random text that you have to rewrite, and the script measures time, number of mistakes etc. The text would be visible on the screen all... (3 Replies)
Discussion started by: xqwzts
3 Replies

2. Red Hat

New Background and Text Color

Hi. How do I change the background color and text in Fedora. I did find the set_color -b command. Thanks (1 Reply)
Discussion started by: Ccccc
1 Replies

3. Shell Programming and Scripting

Python- Changing background color on Button click

Hi, I am trying to write a python program which changes background color on click of button. However i am stuck up. Instead of changing the color currently it is creating a new frame every time. please look at the code and let me know how to correct it #!/usr/bin/env python from Tkinter... (0 Replies)
Discussion started by: vickylife
0 Replies

4. Shell Programming and Scripting

text color

Hi all I have a file contains columns showing figures as below : Input file A B C D 50 60 90 E 100 20 53 F 30 40 70 G 25 27 45 I want to color the value above or equal 90 by red and to be shown as below output file... (5 Replies)
Discussion started by: Bluetoot
5 Replies

5. UNIX for Advanced & Expert Users

Changing text color in existing xterm or dtterm

On solaris and irix systems, I'm using csh in an existing xterm or dterm and would like to change the text colors. How do I accomplish this? Thanks (1 Reply)
Discussion started by: fjc
1 Replies

6. Shell Programming and Scripting

How to change prompt color when changing path

Hi all, Can you tell me how to change the prompt color (only the path part) when I chnange directory with "cd"? I use the sequence below in ".bashrc" (Solaris 8) to change my prompt colors and I'd like to modify it to change the path color when I cange directory. PSC() { echo -ne "\"; }... (0 Replies)
Discussion started by: majormark
0 Replies

7. UNIX for Advanced & Expert Users

Changing Text Color Using VT100

Hi, I want to change the color of the text. Currently, I am using the following VT100 command, which changes the color of the foreground: <ESC>[{attr1};...;{attrn}m The problem is, when I change the color of the foreground, it changes the color of the text as expected, but it also... (4 Replies)
Discussion started by: Abed Alnaif
4 Replies

8. Shell Programming and Scripting

Changing font and color in log file of Script

Hi, We have process log with user defined error messages and information echos. I wanted to highlight error messgaes in that log file while writing in it. Is there any option with either echo or any other command which enables making text bold or we can change the font of body text of echo. ... (4 Replies)
Discussion started by: satgo
4 Replies
Login or Register to Ask a Question