DEADBEEF
DEADBEEF

Reputation: 665

Print rectangles to terminal

I'm trying to write a text editor for Linux that looks like MS-DOS EDIT.

However, I'm stuck because I can't figure out how to draw the thin rectangles around the editor screen and dialog box. I know the Linux dialog command can do something similar:

How can I draw rectangles like that around the screen (preferably without curses)?

Upvotes: 3

Views: 6624

Answers (4)

ephemient
ephemient

Reputation: 204926

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃These are box-drawing characters.      ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│They live in the U+2500-U+257F range of│
│Unicode characters.                    │
└───────────────────────────────────────┘

░▒▓▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜▓▒░
░▒▓▌ The shadows are block elements, ▐▓▒░
░▒▓▌ Unicode U+2580-U+259F.          ▐▓▒░
░▒▓▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟▓▒░

Once upon a time, box-drawing characters and block elements and were common in CP-437. Modern terminals likely expect UTF-8. (They don't work very well in web browsers... see here if the above text looks odd.)

There are also ANSI escapes to set the background color, foreground color, and other attributes of text displayed on a terminal. I can't demonstrate it well on Stack Overflow, though.

Upvotes: 6

MD XF
MD XF

Reputation: 8129

You're looking for box-drawing characters. Here's a complete table.

Assuming your system has a Unicode font installed, which most modern distros do, you could print those to your terminal like this:

#include <wchar.h>
#include <locale.h>
...
    setlocale(LC_ALL,"en_US.UTF-8");
    wprintf(L"\u250C\u2500\u2510\n");    // ┏━┓
    wprintf(L"\u2502 \u2502\n");         // │   │
    wprintf(L"\u2514\u2500\u2518\n");    // └━─┘

Upvotes: 0

Davislor
Davislor

Reputation: 15144

The ncurses library is a good way to do what you want, although you say you want alternatives. You can use the Unicode box-drawing characters as wide characters. They include all the characters from MS-DOS code page 437.

Modern distributions should be set up to support UTF-8 by default, so this should work. (I recommend saving the source file as UTF-8 with a byte-order mark.)

#define _XOPEN_SOURCE 700

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

int main(void)
{
  setlocale( LC_ALL, "" );
  fputws( L"╒╩╤╣\n", stdout );

  return EXIT_SUCCESS;
}

Without curses, you can check the environment variables LINES and COLS to get the dimensions of the terminal. The control characters to print colors and such on the Linux console are in the console_codes(4) man page (and are a variant of the VT102 control codes, which are a superset of VT100, a superset of ANSI standard terminals). If you want to invoke it from a program such as gnome_terminal, check its documentation too, but it will probably implement an extension of xterm, which is an extension of VT102, etc. One that is very useful is that the form feed character '\L' will clear the screen and let you redraw it. You could also use terminfo or termcap for a more abstract and general interface, but in practical terms, nobody uses anything other than an extension of VT100 plus ANSI color any more.

Make sure your terminal font includes the line-drawing characters you want to use! DejaVu Sans Mono is an excellent monospace font, especially for its coverage of Unicode. Also, you can check that your locale is set correctly with the locale command; the locale names you see should end in something like .utf8 or UTF-8.

Upvotes: 4

gavinb
gavinb

Reputation: 20038

What you're describing is using the box drawing characters present in various extended character sets. The characters available depend at least on the platform and terminal emulation.

Given your question is tagged with Linux, the easiest method would be to use the ncurses library. Why would you prefer not to use it and have to reinvent that wheel?

If you can expect at least VT100 emulation (reasonable) then you can use the basic line drawing, but higher levels have more characters.

It's a bit old, but have a look at the window sample code here:

You may also want to look into the Xterm escape characters (expands the VT100 set):

Upvotes: 2

Related Questions