0

I have the following code:

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

Everything works wonderfully, except for one thing. There appears to be extra space added on the first line:

extra space

How can I get rid of this extra space?

carloabelli
  • 4,289
  • 3
  • 43
  • 70
  • Is it because of the faulty logic here: `if (true) {...} else {...}` which causes the first output text to always be output? – Weather Vane Dec 27 '15 at 17:34
  • @WeatherVane For each method yes the color version will be output. I will add a flag to switch to color or not color later. – carloabelli Dec 27 '15 at 18:05
  • the posted code is missing #include statements for stdio.h and unistd.h and stdbool.h There is no definition of `RESET`. – user3629249 Dec 30 '15 at 11:05

3 Answers3

1

While I can't test with the control codes on my system, you most likely need a \n after this text:

printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n" D);

At least, this fixes the same problem here on a Windows system (and Y and D seem to be color control codes, so this shouldn't affect the position anyway).

Update:

Seems like a position control code exists too (can't test it here though):

#define     GOTOYX   "\x1B[%.2d;%.2dH"
    Set cursor to (y, x). More...

So you could possibly use this code to position your first line instead of that extra newline.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • That does work but it does put an extra newline on the bottom which I guess isn't the end of the world. Thank! – carloabelli Dec 27 '15 at 21:35
  • Maybe you could do a check when to add a newline and when not? But don't you have controlcodes available for position also? That might even be better. – Danny_ds Dec 27 '15 at 21:40
  • Yes. I think I have figured it out. Maybe `\x1b[1J` retains the cursor position (I thought it would reset it). I will find a control code to fix that. Thanks for your help! :) – carloabelli Dec 27 '15 at 21:46
0

Try inserting a \n character before the first line.

William Price
  • 4,033
  • 1
  • 35
  • 54
FlowX
  • 102
  • 12
0

I;m on ubuntu linux 14.04 compiling with gcc.

The following code cleanly compiles and perform the desired operation.

I was not able to get the \1b[2j (clear screen) to work, and found that I did not need it.

Some of the significant changes were

  1. end the last line of each graphic with \n so last line always immediately output
  2. modified the graphics to all be the same width and height
  3. modified the if(true) statement to alternate between the coloured and plain text output
  4. removed the unnecessary call to fflush()
  5. the reason for the bad offset for the first line of the graphic was not using correct spacing to allow for the w at the beginning of a line in the graphic and not outputting the whole graphic output and not making all the graphics the same height/width.

The following code is the result

#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;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17