idea设置控制台打印彩色_在控制台中打印彩色文本

本文介绍了如何在IntelliJ IDEA中设置控制台以显示彩色打印文本,参照了 Medium 上的相关教程。
摘要由CSDN通过智能技术生成

idea设置控制台打印彩色

Hello, everyone😁! I’m writing to share the solution of a problem that I had when I was working on a personal project, a Snake game in C++ running in console. After finishing the game logic, I tried to make it look nicer, so I decided to print coloured characters on the screen, but as far as I know, this language does not have native functions to do that 😥. For this reason, I searched for some external library, however, I didn’t wish to import a lot of stuff to make such a simple task, then I ended up finding a better solution: ANSI escape code.

^ h ELLO,everyone😁! 我正在写这篇文章,分享我在个人项目中遇到的问题的解决方案,这是在控制台中运行的C ++ Snake游戏 。 在完成了游戏逻辑之后,我试图使它看起来更好,所以我决定在屏幕上打印彩色字符,但是据我所知,这种语言没有本机功能来执行此操作。 因此,我搜索了一些外部库,但是,我不想导入很多东西来完成这样一个简单的任务,然后我最终找到了一个更好的解决方案: ANSI转义代码

为什么我认为使用它们是一个好主意? (Why do I think it’s a good idea to use them?)

Because it’s a built-in console function, You don’t need to import any external library to your code, which is a good thing, in my opinion, because every computer that is compatible with ANSI will run your program. Another good point of using escape codes is that you can use them in other programming languages that run in the terminal if they don’t have a better way to work with the Scape features, what makes this a great addition to your devs tool kit.

因为它是内置的控制台功能,所以您不需要将任何外部库导入代码中,我认为这是一件好事,因为与ANSI兼容的每台计算机都可以运行您的程序。 使用转义代码的另一个好处是,如果它们没有更好的方式使用Scape功能,则可以在终端机中运行的其他编程语言中使用它们,这使它成为您的devs工具包的重要补充。

那么,什么是ANSI转义码? (So, what is the ANSI Escape Code?)

ANSI escape sequences are a standard, developed in the 70s, used to format outputs in computer terminal. They are made of tags, making the console interpret what comes next as a command instead of a simple string. Those codes are very useful and can make many things like: move the cursor, apply bold or italic in text, clean the screen and change the background colour. However, I will just talk about the colour codes in this article.

ANSI转义序列是70年代开发的标准,用于格式化计算机终端中的输出。 它们由标签组成,使控制台将接下来的内容解释为命令,而不是简单的字符串。 这些代码非常有用,并且可以进行很多操作,例如:移动光标,在文本中应用粗体或斜体,清洁屏幕并更改背景颜色。 但是,我只会在本文中讨论颜色代码。

它在哪里工作? (Where does it work?)

This feature is compatible with OS based on Unix systems, if you use Linux distributions like Ubuntu or macOS, this tutorial will, probably, work fine🙃. On the other hand, Windows became compatible with ANSI escape code after version 1511 of Windows 10. If you have installed Windows Terminal on your computer, you are lucky! Because it’s compatible with the escape code by default👏. Previous versions of Windows like 8.1 or 7 are not compatible with it natively, that being so, you may need to install some extra programs. If you only want to use default stuff this tutorial does not work to you😢.

此功能与基于Unix系统的OS兼容,如果使用Linux发行版(如Ubuntu或macOS),则本教程可能会正常工作🙃。 另一方面,在Windows 10版本1511之后,Windows变得与ANSI转义代码兼容。如果您在计算机上安装了Windows Terminal,那么您很幸运! 因为默认情况下它与转义代码兼容👏。 Windows的早期版本(例如8.1或7)与它本身不兼容,因此,您可能需要安装一些其他程序。 如果您只想使用默认的东西,那么本教程对您不起作用。

让我们开始吧! (Let’s get started!)

The code looks like this:

代码如下:

\ESC[code Your output \ESC[m

The first two digits of the tag are the ASCII value of ESC, ( 27, 1Bh, 33o) and “[“ (91, 5Bh, 133o), the other part is an alphanumeric code representing some operation that will be applied in the output. We have a stop tag too, which is the last part of the command, and its function, as the name suggests, is to stop applying the modifications in the console.

标签的前两位数字是ESC的ASCII值(27,1Bh,33o)和“ [”(91,5Bh,133o),另一部分是字母数字代码,表示将在输出中应用的某些操作。 我们也有一个stop标签​​,这是命令的最后一部分,顾名思义,它的功能是停止在控制台中应用修改。

八种颜色 (Eight colours)

The escape code has some colour specifications, the simplest one only has eight tones for text and background, with an additional lighter variation, I printed them in the table below :

转义码具有一些颜色规范,最简单的只有八种用于文本和背景的色调,另外还有较浅的变化,我将它们打印在下表中:

The table’s values between 30 to 37 and 40 to 47 represent the standard text and background colours, respectively, the 30 is unreadable because it’s in black. The values in range 90–97 and 100–107 are the lighter versions of the foreground and screen.

该表的值介于30到37之间以及40到47之间,分别代表标准文本和背景色,因为30是黑色,所以它不可读。 90–97和100–107范围内的值是前景和屏幕的较浅版本。

The code used to generate this table is here:

用于生成此表的代码在这里:

void print_8_colours()
{
    for (int i = 0; i < 108; i++)
    {
        if (i % 9 == 0 && i != 0)
            cout << endl;
        printf("\033[%dm %3d\033[m", i, i);
    }
}

Also, if you want to modify both text and background, you may write your code like that:

另外,如果要同时修改文本和背景,则可以这样编写代码:

ESC[background_colour;Text_colourm output ESC[m”

An application of what I said above is here:

我上面所说的应用程序在这里:

Image for post
You have a purple text with a light blue background.
您有一个浅蓝色背景的紫色文本。

The code is here, I only wrote the part that matters the print instead of the whole subroutine.

代码在这里,我只写了重要的部分而不是整个子例程。

printf("\033[35;106m Hello, world!\033[m");

Another thing that I like to do, to Improve the readability is to use macros once we have a small number of colours. I will show an example by rewriting the piece of code that I used to change both text and console colour.

我想做的另一件事,就是要提高可读性,就是在颜色很少的情况下使用宏。 我将通过重写用于更改文本和控制台颜色的代码段来显示示例。

#define ESC "\033["
#define LIGHT_BLUE_BKG "106"
#define PURPLE_TXT "35"
#define RESET "\033[m"


int main()
{
    cout << ESC << LIGHT_BLUE_BKG << ";" << PURPLE_TXT <<"m"<< "Hello, world!" << RESET;
    
    return 0;
}

256色 (256 colours)

The other specification that I want to talk about is way more complete, it counts with 256 texts and background colour options, with all these alternative you can let your imagination run free while you paint your computer terminal😎!

我要谈的另一个规范是更完整的规范,它包含256种文本和背景颜色选项,所有这些替代方案可以让您在为计算机终端涂漆时自由发挥想象力!

Text

文本

This code prints a table with 256 shades of text colours generated using ANSI escape code.

此代码将打印一个表格,该表格具有使用ANSI转义码生成的256种阴影的文本颜色。

void print_256_colours_txt()
{
    for (int i = 0; i < 256; i++)
    {
        if (i % 16 == 0 && i != 0)
            cout << endl;
        printf("\033[38;5;%dm %3d\033[m", i, i);
    }
}
Image for post
Codes of the 256 options of ANSI text colours
256种ANSI文本颜色选项的代码

Background

背景

This one generates all the options available with the 256 encodings.

这将生成256种编码可用的所有选项。

void print_256_colours_background()
{
    for (int i = 0; i < 256; i++)
    {
        if (i % 16 == 0 && i != 0)
            cout << endl;
        printf("\033[48;5;%dm %3d\033[m", i, i);
    }
}
Image for post
Codes of the background colours available
可用的背景色代码

If you look at the last pieces of code that I displayed you will notice that they have a number at the beginning of the code. The first one has a 38, this means that we are working with the text colour. The second one has a 48 and shows to the system that the code is referent of console’s background.

如果查看我显示的最后几段代码,您会注意到它们在代码的开头有一个数字。 第一个具有38 ,这意味着我们正在使用文本颜色。 第二个有一个48 ,向系统显示该代码是控制台背景的参考。

Awesome!😍 don’t you think? We passed from a black-white terminal to a very colourful one!

太好了!😍你不觉得吗? 我们从黑白终端变成了非常丰富多彩的终端!

Here you have the references I used to write this article:

在这里,您有我用来撰写本文的参考资料:

翻译自: https://medium.com/@vitorcosta.matias/print-coloured-texts-in-console-a0db6f589138

idea设置控制台打印彩色

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值