View Single Post
  #1   (View Single Post)  
Old 13th April 2013
LeFrettchen's Avatar
LeFrettchen LeFrettchen is offline
Marveled user
 
Join Date: Aug 2012
Location: France
Posts: 408
Smile Shell Color Program

Hi everybody.

I wanted to change the shell colors on my Blade, so I created a lil C program to do the job.

This is just a first try, but it works perfectly for me.
Well, works 'til the vi editor is launched...

------------------------------------------------------------------------

osc.c file :
Code:
/*  OSC - Open Shell Color  */
/* Thiery SAMPY  13/04/2013 */

#include <stdio.h>
#include <getopt.h>

void uppercase(char *sPtr)
{  
    while (*sPtr != '\0')
    {
         *sPtr++ = toupper(*sPtr);
    }
}

struct color_maps
{
	char *name;
	int  tColor;
	int  bgColor;
};

void set_colors(int text_color,int background_color)
{
	printf("\033[0;%i;%im", text_color, background_color);
}

void help()
{
	printf("usage : osc [ -hH ] [text_color] [background_color]\n");
	printf("possible colors : black, red, green, yellow, blue, magenta, cyan, white\n");
}

int main(int argc, char **argv)
{
	int osc_option;
	int count_argc;
	int background_color, text_color;
	
      // the different colors and their text & background color numbers
	static struct color_maps color_map[] =
	{
		{"BLACK", 30, 40},
		{"RED", 31, 41},
		{"GREEN", 32, 42},
		{"YELLOW", 33, 43},
		{"BLUE", 34, 44},
		{"MAGENTA", 35, 45},
		{"CYAN", 36, 46},
		{"WHITE", 37, 47}
	};
	
     // options ?
	while ((osc_option = getopt(argc, argv, "hH?")) != -1)
	{
		switch (osc_option)
		{
			case 'h':
				help();
				break;
			case 'H':
				help();
				break;
			default:
				set_default_colors();
		}
	}
	
	 // default colors
	background_color = 40; // black
	text_color       = 37; // white
	
     // colors in arguments ?
	if (optind < argc)
	{
		char *arg;
		int    i;
		
		count_argc = 0;
		
		while (optind < argc)
		{
			count_argc++;
			uppercase(arg = argv[optind++]);

			i = 0;
			while (i < 8)
			{
				if (strcmp(arg, color_map[i].name) == 0)
				{
					if (count_argc == 1)
						text_color = color_map[i].tColor;
					else background_color = color_map[i].bgColor;
				}
				i++;
			}
		}
		
		set_colors(text_color, background_color);
	}
	else set_default_colors();
	
	return 0;
}
------------------------------------------------------------------------

Then, compilation :
Code:
# gcc -o osc_53s_i386 osc.c
and a symbolic link :
Code:
#ln -s /var/www/htdocs/dev/c/osc/osc_53s_i386 /usr/local/bin/osc
And that's it !

For now, it's enough, since the vi utilisation perturbs the color scheme.
New ideas or critics are welcome.
Reply With Quote