清屏后添加额外空格
Extra Spaces Being Added After Clear Screen
我有以下代码:
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#define RESET "\x1b[1J"
#define D "\x1b[0m"
#define Y "\x1b[33;1m"
#define W "\x1b[37;1m"
#define B "\x1b[30;1m"
void printLeft(bool color)
{
if (color) {
printf(Y " __\n"
" / |\n"
" / \\n"
" / |\n"
W " _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/\n"
" --" Y "|" W "\____/" Y "|" W "--\n"
Y " \ |\n"
" \ /\n"
" \ |\n"
W " |" Y "\__|" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" / |\n"
" / \\n"
" / |\n"
" _\| |[*][*]| |/_\n"
" \ | | /\n"
" --|\____/|--\n"
" \ |\n"
" \ /\n"
" \ |\n"
" |\__||\n"
" | |\n"
" ==| |==");
}
}
void printRight(bool color) {
if (color) {
printf(Y " __\n"
" | \\n"
" / \\n"
" | \\n"
W " _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/\n"
" --" Y "|" W "\____/" Y "|" W "--\n"
Y " | /\n"
" \ /\n"
" | /\n"
W " |" Y "|__/" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" | \\n"
" / \\n"
" | \\n"
" _\| |[*][*]| |/_\n"
" \ | | /\n"
" --|\____/|--\n"
" | /\n"
" \ /\n"
" | /\n"
" ||__/|\n"
" | |\n"
" ==| |==");
}
}
void printBottom(bool color) {
if (color) {
printf(Y " __\n"
" | |\n"
" / \\n"
" | |\n"
" |" W "[" B "*" W "][" B "*" W "]" Y"|\n"
" | |\n"
W " _\| " Y "|" W "\____/" Y "| " W "|/_\n"
" \/" Y "| |" W "\/\n"
Y " | |\n"
W " /\" Y "\ /" W "/\\n"
" ==/ " Y "|__| " W "\==");
} else {
printf(" __\n"
" | |\n"
" / \\n"
" | |\n"
" |[*][*]|\n"
" | |\n"
" _\| |\____/| |/_\n"
" \/| |\/\n"
" | |\n"
" /\\ //\\n"
" ==/ |__| \==");
}
}
int main(int argc, char *argv[])
{
short counter = 0;
while (1) {
printf(RESET);
if (counter == 0) printLeft(true);
else if (counter == 2) printRight(true);
else printBottom(true);
if (true) {
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"" D);
} else {
printf("\n\n\"IT'S PEANUT BUTTER JELLY TIME\"");
}
fflush(stdout);
usleep(200000);
counter = (counter + 1) % 4;
}
return 0;
}
一切都很好,除了一件事。第一行似乎添加了额外的 space:
我怎样才能摆脱这个额外的space?
尝试在第一行前插入 \n
个字符。
虽然我无法在我的系统上使用控制代码进行测试,但您很可能需要在这段文字后加上 \n
:
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n" D);
至少,这在 Windows 系统上解决了同样的问题(而且 Y
和 D
似乎是颜色控制代码,所以这不应该影响位置无论如何)。
更新:
似乎也存在 position control code(虽然不能在这里测试):
#define GOTOYX "\x1B[%.2d;%.2dH"
Set cursor to (y, x). More...
所以您可以使用这段代码来定位您的第一行,而不是那个额外的换行符。
I;m on ubuntu linux 14.04 用 gcc 编译。
以下代码可以干净地编译并执行所需的操作。
我无法让 b[2j
(清屏)工作,我发现我不需要它。
一些重大变化是
- 每个图形的最后一行以
\n
结束所以最后一行总是立即输出
- 将图形修改为相同的宽度和高度
- 修改了
if(true)
语句以在彩色和纯文本输出之间交替
- 删除了对
fflush()
的不必要调用
- 图形第一行偏移量错误的原因是没有使用正确的间距来考虑图形中一行开头的
w
并且没有输出整个图形输出并且没有使所有图形相同 height/width.
下面的代码就是结果
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#define D "\x1b[0m"
#define Y "\x1b[33;1m"
#define W "\x1b[37;1m"
#define B "\x1b[30;1m"
#define CURSOR_1_1 "\x1b[1;1f \n"
void printLeft(bool color)
{
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" / | \n"
" / \ \n"
" / | \n"
W
" _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/ \n"
" --" Y "|" W "\____/" Y "|" W "-- \n"
Y
" \ | \n"
" \ / \n"
" \ | \n"
W
" |" Y "\__|" W "| \n"
" | | \n"
" ==| |= \n");
} else {
printf(CURSOR_1_1
" __ \n"
" / | \n"
" / \ \n"
" / | \n"
" _\| |[*][*]| |/_ \n"
" \ | | / \n"
" --|\____/|-- \n"
" \ | \n"
" \ / \n"
" \ | \n"
" |\__|| \n"
" | | \n"
" ==| |== \n");
}
}
void printRight(bool color) {
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" | \ \n"
" / \ \n"
" | \ \n"
W
" _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_ \n"
" \ " Y "| | " W "/ \n"
" --" Y "|" W "\____/" Y "|" W "-- \n"
Y
" | / \n"
" \ / \n"
" | / \n"
W
" |" Y "|__/" W "| \n"
" | | \n"
" ==| |== \n");
} else {
printf(CURSOR_1_1
" __ \n"
" | \ \n"
" / \ \n"
" | \ \n"
" _\| |[*][*]| |/_ \n"
" \ | | / \n"
" --|\____/|-- \n"
" | / \n"
" \ / \n"
" | / \n"
" ||__/| \n"
" | | \n"
" ==| |== \n");
}
}
void printBottom(bool color) {
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" | | \n"
" / \ \n"
" | | \n"
" |" W "[" B "*" W "][" B "*" W "]" Y"| \n"
" | | \n"
W
" _\| " Y "|" W "\____/" Y "| " W "|/_ \n"
" \/" Y "| |" W "\/ \n"
Y
" | | \n"
W
" /\" Y "\ /" W "/\ \n"
" ==/ " Y "|__| " W "\== \n"
" \n"
" \n");
} else {
printf(CURSOR_1_1
" __ \n"
" | | \n"
" / \ \n"
" | | \n"
" |[*][*]| \n"
" | | \n"
" _\| |\____/| |/_ \n"
" \/| |\/ \n"
" | | \n"
" /\\ //\ \n"
" ==/ |__| \== \n"
" \n"
" \n");
}
}
int main( void )
{
int counter = 0;
printf(CURSOR_1_1);
while (1)
{
if (counter == 0) printLeft(true);
else if (counter == 2) printRight(true);
else printBottom(true);
if (counter&0x01)
{
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"%s\n", D );
}
else
{
printf("\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n");
}
//fflush(stdout);
usleep(200000);
counter = (counter + 1) % 4;
}
return 0;
}
我有以下代码:
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#define RESET "\x1b[1J"
#define D "\x1b[0m"
#define Y "\x1b[33;1m"
#define W "\x1b[37;1m"
#define B "\x1b[30;1m"
void printLeft(bool color)
{
if (color) {
printf(Y " __\n"
" / |\n"
" / \\n"
" / |\n"
W " _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/\n"
" --" Y "|" W "\____/" Y "|" W "--\n"
Y " \ |\n"
" \ /\n"
" \ |\n"
W " |" Y "\__|" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" / |\n"
" / \\n"
" / |\n"
" _\| |[*][*]| |/_\n"
" \ | | /\n"
" --|\____/|--\n"
" \ |\n"
" \ /\n"
" \ |\n"
" |\__||\n"
" | |\n"
" ==| |==");
}
}
void printRight(bool color) {
if (color) {
printf(Y " __\n"
" | \\n"
" / \\n"
" | \\n"
W " _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/\n"
" --" Y "|" W "\____/" Y "|" W "--\n"
Y " | /\n"
" \ /\n"
" | /\n"
W " |" Y "|__/" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" | \\n"
" / \\n"
" | \\n"
" _\| |[*][*]| |/_\n"
" \ | | /\n"
" --|\____/|--\n"
" | /\n"
" \ /\n"
" | /\n"
" ||__/|\n"
" | |\n"
" ==| |==");
}
}
void printBottom(bool color) {
if (color) {
printf(Y " __\n"
" | |\n"
" / \\n"
" | |\n"
" |" W "[" B "*" W "][" B "*" W "]" Y"|\n"
" | |\n"
W " _\| " Y "|" W "\____/" Y "| " W "|/_\n"
" \/" Y "| |" W "\/\n"
Y " | |\n"
W " /\" Y "\ /" W "/\\n"
" ==/ " Y "|__| " W "\==");
} else {
printf(" __\n"
" | |\n"
" / \\n"
" | |\n"
" |[*][*]|\n"
" | |\n"
" _\| |\____/| |/_\n"
" \/| |\/\n"
" | |\n"
" /\\ //\\n"
" ==/ |__| \==");
}
}
int main(int argc, char *argv[])
{
short counter = 0;
while (1) {
printf(RESET);
if (counter == 0) printLeft(true);
else if (counter == 2) printRight(true);
else printBottom(true);
if (true) {
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"" D);
} else {
printf("\n\n\"IT'S PEANUT BUTTER JELLY TIME\"");
}
fflush(stdout);
usleep(200000);
counter = (counter + 1) % 4;
}
return 0;
}
一切都很好,除了一件事。第一行似乎添加了额外的 space:
我怎样才能摆脱这个额外的space?
尝试在第一行前插入 \n
个字符。
虽然我无法在我的系统上使用控制代码进行测试,但您很可能需要在这段文字后加上 \n
:
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n" D);
至少,这在 Windows 系统上解决了同样的问题(而且 Y
和 D
似乎是颜色控制代码,所以这不应该影响位置无论如何)。
更新:
似乎也存在 position control code(虽然不能在这里测试):
#define GOTOYX "\x1B[%.2d;%.2dH"
Set cursor to (y, x). More...
所以您可以使用这段代码来定位您的第一行,而不是那个额外的换行符。
I;m on ubuntu linux 14.04 用 gcc 编译。
以下代码可以干净地编译并执行所需的操作。
我无法让 b[2j
(清屏)工作,我发现我不需要它。
一些重大变化是
- 每个图形的最后一行以
\n
结束所以最后一行总是立即输出 - 将图形修改为相同的宽度和高度
- 修改了
if(true)
语句以在彩色和纯文本输出之间交替 - 删除了对
fflush()
的不必要调用
- 图形第一行偏移量错误的原因是没有使用正确的间距来考虑图形中一行开头的
w
并且没有输出整个图形输出并且没有使所有图形相同 height/width.
下面的代码就是结果
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#define D "\x1b[0m"
#define Y "\x1b[33;1m"
#define W "\x1b[37;1m"
#define B "\x1b[30;1m"
#define CURSOR_1_1 "\x1b[1;1f \n"
void printLeft(bool color)
{
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" / | \n"
" / \ \n"
" / | \n"
W
" _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \ " Y "| | " W "/ \n"
" --" Y "|" W "\____/" Y "|" W "-- \n"
Y
" \ | \n"
" \ / \n"
" \ | \n"
W
" |" Y "\__|" W "| \n"
" | | \n"
" ==| |= \n");
} else {
printf(CURSOR_1_1
" __ \n"
" / | \n"
" / \ \n"
" / | \n"
" _\| |[*][*]| |/_ \n"
" \ | | / \n"
" --|\____/|-- \n"
" \ | \n"
" \ / \n"
" \ | \n"
" |\__|| \n"
" | | \n"
" ==| |== \n");
}
}
void printRight(bool color) {
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" | \ \n"
" / \ \n"
" | \ \n"
W
" _\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_ \n"
" \ " Y "| | " W "/ \n"
" --" Y "|" W "\____/" Y "|" W "-- \n"
Y
" | / \n"
" \ / \n"
" | / \n"
W
" |" Y "|__/" W "| \n"
" | | \n"
" ==| |== \n");
} else {
printf(CURSOR_1_1
" __ \n"
" | \ \n"
" / \ \n"
" | \ \n"
" _\| |[*][*]| |/_ \n"
" \ | | / \n"
" --|\____/|-- \n"
" | / \n"
" \ / \n"
" | / \n"
" ||__/| \n"
" | | \n"
" ==| |== \n");
}
}
void printBottom(bool color) {
if (color) {
printf(CURSOR_1_1
Y
" __ \n"
" | | \n"
" / \ \n"
" | | \n"
" |" W "[" B "*" W "][" B "*" W "]" Y"| \n"
" | | \n"
W
" _\| " Y "|" W "\____/" Y "| " W "|/_ \n"
" \/" Y "| |" W "\/ \n"
Y
" | | \n"
W
" /\" Y "\ /" W "/\ \n"
" ==/ " Y "|__| " W "\== \n"
" \n"
" \n");
} else {
printf(CURSOR_1_1
" __ \n"
" | | \n"
" / \ \n"
" | | \n"
" |[*][*]| \n"
" | | \n"
" _\| |\____/| |/_ \n"
" \/| |\/ \n"
" | | \n"
" /\\ //\ \n"
" ==/ |__| \== \n"
" \n"
" \n");
}
}
int main( void )
{
int counter = 0;
printf(CURSOR_1_1);
while (1)
{
if (counter == 0) printLeft(true);
else if (counter == 2) printRight(true);
else printBottom(true);
if (counter&0x01)
{
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"%s\n", D );
}
else
{
printf("\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n");
}
//fflush(stdout);
usleep(200000);
counter = (counter + 1) % 4;
}
return 0;
}