0

I'm building a ncurses program in c and i want to get the upper and lower half block characters (U+2580 and U+2584 for example) (▀) for graphics on small terminals (i normally use double space and inverted double space). I've tried a lot of different ways, but none of them worked. I also tried urxvt st and xterm with absolutely no success. For example:

#include <locale.h>
#include <ncurses.h>


int main (int argc, char *argv[]) {
    setlocale(LC_ALL, "");

    initscr();

    add_wch(L"\u2584");

    refresh();
    getch();
    endwin();
}

Also, i would be very interested if there is any other way of displaying graphics in a terminal, like some tiny dots i've seen.

EDIT: Also, when i paste the character to the terminal it shows it fine but when i use printf it shows nothing. why is that?

C32
  • 161
  • 8
  • "Also, i would be very interested if there is any other way of displaying graphics in a terminal" => Do you like caca ? http://caca.zoy.org/wiki/libcaca – Stargateur Apr 15 '18 at 13:08
  • Yes, I love that library, but I want ncurses because of the windows. I plan to have libcaca as an option too. I'm making a small emulator and I have a window that shows the assembly code as it's being run. it would be awesome if libcaca could return a 2D table that i can then use with ncurses – C32 Apr 15 '18 at 13:12
  • actually that might be possible, i will look into it – C32 Apr 15 '18 at 13:16
  • Use [ncursesw](https://www.gnu.org/software/ncurses/ncurses.html) instead of ncurses (in your `Makefile` or compiler include and library options), so that you use the wide-character version of ncurses. – Nominal Animal Apr 16 '18 at 00:05
  • @NominalAnimal still add_wch and addwstr not working – C32 Apr 16 '18 at 04:42
  • @H32: The function to write a wide-character string (like `L"\u2584"`) is `addwstr(L"\u2584")`, like Thomas Dickey mentioned in the answer. You need to do both: use ncursesw, and addwstr(). (`waddwstr()` is just the version that takes a window argument first.) I even verified this with a small test program: works fine in Linux at least. You might wish to check which terminal font you are using, though (use e.g. Character Map utility to see if your terminal font has the Block Elements Unicode characters). – Nominal Animal Apr 16 '18 at 13:52
  • @NominalAnimal when i paste the character in the terminal it shows up fine so i think my terminal supports it.this is the code i currently have https://pastebin.com/KEuRrxMp – C32 Apr 16 '18 at 16:44
  • If you save it as `h32.c`, and compile it using `gcc -Wall -O2 $(pkg-config --cflags ncursesw) h32.c $(pkg-config --libs ncursesw) -o h32` (in a Makefile, use backticks instead of `$()`), then `./h32` works. (That expands to `gcc -Wall -O2 -D_GNU_SOURCE -I/usr/include/ncursesw h32.c -lncursesw -ltinfo -o h32`, but the `pkg-config` way should work across Linux distributions, so I recommend using that). In other words, you didn't compile your program with the correct options. – Nominal Animal Apr 16 '18 at 18:06
  • @NominalAnimal i compiled it as you said and i tried it on urxvt, st, alacrity, the tty,tilix,mate-terminal and konsole and i got the same result in all of them, a space followed by the cursor – C32 Apr 16 '18 at 18:49
  • 1
    @H32: What is your locale? In other words, what does `env | grep -e '^LANG' -e '^LC_'` output in your terminal? For the C library to realize it supports Unicode (and uses UTF-8), it should be e.g. `en_GB.utf8`, with the `.utf8` suffix being the important part. – Nominal Animal Apr 16 '18 at 21:47
  • @NominalAnimal It worked! Thank you so much! I hadn't generated any locals and LANG was set to C. – C32 Apr 16 '18 at 21:54

1 Answers1

0

The statement

add_wch(L"\u2584");

won't work for any terminal because the parameter is the wrong datatype. add_wch expects a cchar_t datatype, while you have provided a string of wchar_t (you'll get better results with waddwstr).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105