Page 1 of 1

How does ls set the color of the text

Posted: Mon Jun 04, 2012 5:18 pm
by justin
When you open a C source file with nano and redirect the output to a text file, you can see the escape sequences used to set the color. If you do the same with ls you get only the directory listing with no escape sequences.

How does ls change the text color?

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 5:30 pm
by NickJohnson
Magic.

Also, a program can tell if stdout is a tty, using isatty(). But it's probably the magic.

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 5:56 pm
by justin
NickJohnson wrote:Magic.

Also, a program can tell if stdout is a tty, using isatty(). But it's probably the magic.
Is there a way I can see the raw output that the tty sees?

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 7:17 pm
by Love4Boobies
What does any of this have to do with OS development?

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 7:59 pm
by justin
Love4Boobies wrote:What does any of this have to do with OS development?
How does it not have to do with OS development? Does your OS not have a console?

I want to learn about ways to handle colored text.

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 8:05 pm
by VolTeK
The function printing has to have a parameter or a certain character representation in the string you pass to it, to determine if it wants to be changed to a certain color. Actually changing the colors though..

Is definitely in the wiki, i haven't checked, but i'm damn well certain they are.

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 8:40 pm
by roboman
You can google 'escape sequence change text color' or wait a week or two until I finish my video driver for DexOS :)

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 9:00 pm
by NickJohnson
justin wrote:
Love4Boobies wrote:What does any of this have to do with OS development?
How does it not have to do with OS development? Does your OS not have a console?

I want to learn about ways to handle colored text.
ls uses escape sequences to color its text just like any other program. You would make more progress looking up documentation on terminal emulators than trying to reverse engineer ls.

Re: How does ls set the color of the text

Posted: Mon Jun 04, 2012 11:28 pm
by Combuster
justin wrote:
NickJohnson wrote:Also, a program can tell if stdout is a tty, using isatty(). But it's probably the magic.
Is there a way I can see the raw output that the tty sees?
Terminal emulation

Re: How does ls set the color of the text

Posted: Tue Jun 05, 2012 1:39 am
by XanClic
justin wrote:
NickJohnson wrote:Magic.

Also, a program can tell if stdout is a tty, using isatty(). But it's probably the magic.
Is there a way I can see the raw output that the tty sees?
If it's only about a colored ls…

Code: Select all

ls --color=always | less

Re: How does ls set the color of the text

Posted: Tue Jun 05, 2012 8:51 am
by amd64pager
It is in this format:

Code: Select all

#include<stdio.h>
printf("\033[%d;%dm%s\033[0m\n",color,color + 30,string);
string contains your string to be printed in your colour and color can be these values:
  • 0 - black
    1 - red
    2 - green
    3 - yellow
    4 - blue
    5 - magenta
    6 - cyan
    7 - white
Try it out.

Re: How does ls set the color of the text

Posted: Sat Jun 09, 2012 1:22 pm
by justin
XanClic wrote: If it's only about a colored ls…

Code: Select all

ls --color=always | less
Thanks. This is exactly what I wanted to see.