terminaldrawansi-escapeline-drawing

ANSI commands for drawing boxes in terminal


I am trying to draw a box on the screen that looks like this:

┌───┐



└───┘

I found ANSI commands to move the cursor here:

http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

My code looks like this:

int main()
{
    int boxsize = 5;
    std::string bs = std::to_string(boxsize);
    std::string up = "\033[<"+bs+">A";
    std::string down = "\033[<"+bs+">B";
    std::string back = "\033[<"+bs+">D";

    std::cout << "┌";
    for (int i = 0; i < boxsize-2; ++i)
    {
    std::cout << "─";
    std::cout << down;
    std::cout << "─";
    std::cout << up;
    }
    std::cout << "┐";
    std::cout << down;
    std::cout << "┘";
    std::cout << back;
    std::cout << "└";
}

And my result is this:

┌─5>B─5>A─5>B─5>A─5>B─5>A┐5>B┘5>D└

Am I using the ANSI commands incorrectly or is this an issue with my terminal (yakuake)?


Solution

  • The angle brackets (< and >) in these lines should be removed:

    std::string up = "\033[<"+bs+">A";
    std::string down = "\033[<"+bs+">B";
    std::string back = "\033[<"+bs+">D";
    

    In the TLDP page, <N> is supposed to be a number, using an archaic notation to denote italicized text, e.g., N.

    Even with that, you still have work to do, but this is a start:

    ┌─ ─ ─ ┐
    
    
    
    
      ─ └ ─ ┘