How does ls set the color of the text

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

How does ls set the color of the text

Post 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?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: How does ls set the color of the text

Post by NickJohnson »

Magic.

Also, a program can tell if stdout is a tty, using isatty(). But it's probably the magic.
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

Re: How does ls set the color of the text

Post 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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: How does ls set the color of the text

Post by Love4Boobies »

What does any of this have to do with OS development?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

Re: How does ls set the color of the text

Post 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.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: How does ls set the color of the text

Post 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.
roboman
Posts: 24
Joined: Fri Feb 27, 2009 9:41 am
Location: USA
Contact:

Re: How does ls set the color of the text

Post by roboman »

You can google 'escape sequence change text color' or wait a week or two until I finish my video driver for DexOS :)
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: How does ls set the color of the text

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How does ls set the color of the text

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
XanClic
Member
Member
Posts: 138
Joined: Wed Feb 13, 2008 9:38 am

Re: How does ls set the color of the text

Post 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
User avatar
amd64pager
Member
Member
Posts: 73
Joined: Fri Nov 25, 2011 8:27 am
Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1

Re: How does ls set the color of the text

Post 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.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

Re: How does ls set the color of the text

Post 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.
Post Reply