Use C++ to write a small tool for changing the terminal font color under ubuntu

Control code under Ubuntu:

The commonly used ANSI control codes are as follows (some are not supported):

\033[0m close all properties

\033[1m highlight

\033[2m brightness halved

\033[3m Italic

\033[4m underscore

\033[5m flashing

\033[6m flash

\033[7m reverse display

\033[8m blanking

\033[9m A horizontal line in the middle

10-19 About fonts

21-29 is basically the opposite of 1-9

30-37 Set the foreground color

40-47 set background color

30: black

31: red

32: green

33: yellow

34: blue

35: purple

36: dark green

37: white

38 Turn on underline and set the default foreground color

39 Turn off underline and set the default foreground color

40 black background

41 red background

42 green background

43 brown background

44 blue background

45 Magenta Background

46 Peacock Blue Background

47 white background

48 Don't know something

49 Set the default background color

50-89 useless

90-109 sets the foreground and background again, which is lighter than the previous color

\033[nA Move the cursor up n lines

\033[nB Move the cursor down n lines

\033[nC Move the cursor right n lines

\033[nD Move the cursor to the left by n lines

\033[y;xH set cursor position

\033[2J Clear screen

\033[K Clear the content from the cursor to the end of the line

\033[s save the cursor position

\033[u restore cursor position

\033[?25l hide the cursor

\033[?25h display cursor

Source code

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

const int CMD_SETTYPE = 0;
const int CMD_SETBGCOLOR = 1;
const int CMD_SETFGCOLOR = 2;
const int CMD_HELP = 3;

const size_t STYLE_LENGTH = 10;
const string STYLE[] = {"DEFAULT", "INTENSITY", "HALF_INTENSITY", "ITALIC", "UNDERLINE", "FLASH",
						"FAST_FLASH", "INVERT_SHOW", "HIDE", "DELETELINE"};

const size_t BGCOLOR_LENGTH = 8;
const string BGCOLOR[] = {"BLACK", "RED", "GREEN", "YELLOW", "BLUE", "PURPLE", "PEACOCK_BLUE", "WHITE"};

const size_t FGCOLOR_LENGTH = 8;
const string FGCOLOR[] = {"BLACK", "RED", "GREEN", "YELLOW", "BLUE", "PURPLE", "PEACOCK_BLUE", "WHITE"};

using namespace std;

void toUpCase(string &);

void help()
{
	cout << "\n******设置终端颜色的工具******\n";
	cout << "参数列表:\n";
	cout << "-s: \n    后面接样式类型:default--默认样式,intensity--高亮加粗,half_intensity--高亮减半,italic--斜体,"
		 << "underline--下划线,flash--闪烁,fast_flash--快速闪烁,invert_show--反显,hide--隐藏,deleteline--删除线。\n\n";

	cout << "-bg:\n    设置背景颜色,后面接颜色:black--黑,red--红,green--绿,yellow--黄,blue--蓝,purple--紫,"
		 << "peacock_blue--孔雀蓝,white--白。\n\n";

	cout << "-fg:\n    设置字的颜色,后面接颜色:black--黑,red--红,green--绿,yellow--黄,blue--蓝,purple--紫,"
		 << "peacock_blue--孔雀蓝,white--白。\n\n";

	cout << "-h: \n    显示帮助。\n";
	cout << "\n*******************************\n";
}

string setBgColor(const string &color)
{
	string sColor(color);
	string result;
	string &s = sColor;
	toUpCase(sColor);
	size_t i = 0;
	while (i < BGCOLOR_LENGTH)
	{
		if (sColor.compare(BGCOLOR[i]) == 0)
		{
			break;
		}
		++i;
	}

	ostringstream osm;
	if (i >= BGCOLOR_LENGTH)
		return result;
	else
		i += 40;

	osm << i;
	result += "\033[" + osm.str() + "m";

	return result;
}

string setFgColor(const string &color)
{
	string sColor(color);
	string result;
	toUpCase(sColor);
	size_t i = 0;
	while (i < FGCOLOR_LENGTH)
	{
		if (sColor.compare(FGCOLOR[i]) == 0)
		{
			break;
		}
		++i;
	}

	ostringstream osm;
	if (i >= FGCOLOR_LENGTH)
		return result;
	else
		i += 30;

	osm << i;
	result += "\033[" + osm.str() + "m";

	return result;
}

string setStyle(const string &style)
{
	string sStyle = style;
	string result;
	toUpCase(sStyle);
	size_t i = 0;
	while (i < STYLE_LENGTH)
	{
		if (sStyle.compare(STYLE[i]) == 0)
		{
			break;
		}
		++i;
	}

	ostringstream osm;
	if (i >= STYLE_LENGTH)
		return result;

	osm << i;
	result += "\033[" + osm.str() + "m";

	return result;
}

int getParamType(const string &cmd)
{
	if (cmd.compare("-s") == 0)
	{
		return CMD_SETTYPE;
	}
	else if (cmd.compare("-bg") == 0)
	{
		return CMD_SETBGCOLOR;
	}
	else if (cmd.compare("-fg") == 0)
	{
		return CMD_SETFGCOLOR;
	}
	else if (cmd.compare("-h") == 0)
	{
		return CMD_HELP;
	}
	else
	{
		return -1;
	}
}

typedef string (*strFP)(const string &);
strFP getFuncByCmdType(int cmdType)
{
	switch (cmdType)
	{
	case CMD_SETTYPE:
		return setStyle;
	case CMD_SETBGCOLOR:
		return setBgColor;
	case CMD_SETFGCOLOR:
		return setFgColor;
	default:
		return nullptr;
	}
}

string format(int count, char *argv[])
{
	string result;
	if (count <= 1)
		return result;

	if (getParamType(argv[1]) == CMD_HELP){
		help();
		return result;
	}

	int i = 1;
	int cmdIndex = -1;
	while (i < count)
	{
		if(i+1 >= count)
			return result;
		
		cmdIndex = getParamType(argv[i]);
		strFP func = getFuncByCmdType(cmdIndex);
		if (func != nullptr)
		{
			result += func(argv[i + 1]);
		}
		else
		{
			result = "";
			return result;
		}

		i += 2;
	}

	return result;
}

void toUpCase(string &s)
{
	size_t len = s.length();
	size_t interval = 'a' - 'A';
	for (size_t i = 0; i < len; i++)
	{
		s[i] = (s[i] >= 97 && s[i] <= 122) ? s[i] - interval : s[i];
	}
}

int main(int argc, char *argv[])
{
	if (argc <= 1)
	{
		help();
		return 0;
	}

	string formatStr = format(argc, argv);

	cout << formatStr;
	return 0;
}

Save the code as chcolor.cpp file.

Compile

g++ -std=c++11 -o chcolor chcolor.cpp

Copy chcolor to the /bin/ directory: sudo cp chcolor /bin/

use

Switch to root: sudo su

Enter in the terminal: chcolor -h

help

输入:chcolor -s underline -bg blue -fg yellow

You can set the style, background color, and foreground color at the same time:

cout << "\033[1;31;45mHello, ubuntu!\n";
//中间用;进行分隔

There is a question: if it is not used under the root user, there will be no effect, please expert guidance, thank you!

Guess you like

Origin blog.csdn.net/hn_tzy/article/details/103514823