DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 13th April 2013
LeFrettchen's Avatar
LeFrettchen LeFrettchen is offline
Marveled user
 
Join Date: Aug 2012
Location: Bronvaux, France
Posts: 108
Thanked 0 Times in 0 Posts
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
  #2   (View Single Post)  
Old 20th October 2013
LeFrettchen's Avatar
LeFrettchen LeFrettchen is offline
Marveled user
 
Join Date: Aug 2012
Location: Bronvaux, France
Posts: 108
Thanked 0 Times in 0 Posts
Default

Oops, sorry, the above code isn't the good one.

Just have checked, and this can't work, cause the set_default_colors function is not declared

So, here is the working one :

Code:
/*   OSC : Open Shell Color  */
/* Thiery SAMPY - 07/04/2013 */

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

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

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}
};



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



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

void set_default_colors()
{
	set_colors(37, 40);
}



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



int main(int argc, char **argv)
{
	int gsc_option;
	int count_argc;
	int background_color,text_color;
	
	while ((gsc_option = getopt(argc, argv, "DHhd?")) != -1)
	{
		switch (gsc_option)
		{
			case '?':
				help();
				break;
			case 'h':
				help();
				break;
			case 'H':
				help();
				break;
			default:
				set_default_colors();
		}
	}
	
	 // default colors
	background_color = 40; // black
	text_color       = 37;     // white
	
	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].nom) == 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;
}
__________________
In case of emergency
Reply With Quote
Reply

Tags
colors, shell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Color Prompt in OpenBSD. tetrodozombie Guides 18 13th April 2013 02:02 PM
Default terminal color disappearedng FreeBSD General 5 21st February 2012 01:28 AM
Windows Aren't the Right Color Zyos OpenBSD General 9 15th November 2011 11:02 PM
Color shell? giga FreeBSD General 3 14th August 2008 12:07 AM
Want to get rid of ugly color of rox rex FreeBSD General 6 12th May 2008 12:02 AM


All times are GMT. The time now is 12:03 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick