Skip to content

Commit 91e8a6a

Browse files
committed
Merge pull request #2 from Wyatts/master
Add a unicode example.
2 parents e9aba1b + 431097c commit 91e8a6a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

examples/hello_unicode.d

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** hello_unicode.d
2+
*
3+
* This is a demonstration of Unicode output with ncurses in D.
4+
*
5+
* The functionality of this code is otherwise identical to hello_world.d
6+
*
7+
* Requirements for Unicode in ncurses:
8+
* - You need to link against ncursesw instead of ncurses. If you don't have ncursesw, rebuild
9+
* ncurses with --enable-widec (and file a bug with your distro where applicable).
10+
* - You need to be using a Unicode locale. If your $LANG looks something like `en_US.utf8`,
11+
* then you're in good shape. Keep in mind that users don't like when you force a locale; do
12+
* it only if you think it's really necessary.
13+
*
14+
* Modified by: Wyatt
15+
*/
16+
import std.string: toStringz;
17+
import std.c.locale; // Need setlocale()
18+
import deimos.ncurses.ncurses;
19+
20+
void main()
21+
{
22+
setlocale(LC_CTYPE,""); // You need to set the empty locale to use upper-plane glyphs
23+
// This sets the locale based on local variables. On most Unix-
24+
// like systems, you can use the `locale` command to show the
25+
// current settings for your environment.
26+
27+
auto hello = toStringz("日本語からの「Hello World!」");
28+
29+
initscr(); //initialize the screen
30+
printw(hello); //prints the char[] hello to the screen
31+
refresh(); //actually does the writing to the physical screen
32+
getch(); //gets a single character from the screen.
33+
endwin(); //Routine to call before exiting, or leaving curses mode temporarily
34+
}

examples/simple_spectrum.d

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//simple_spectrum: Sample of 256-colour ncurses output in D.
2+
//Modified by: Wyatt
3+
4+
import std.stdio: writeln;
5+
import std.conv;
6+
import deimos.ncurses.ncurses;
7+
8+
//Extended colour support was added in ncurses 5.5 for the wncurses
9+
//library. wncurses has generally been shipped by default in Linux
10+
//distros for roughly the last decade as of this writing (often as the
11+
//only version installed); if this doesn't work, check that your TERM
12+
//is compatible with 256-colour modes, that your terminal emulator
13+
//supports it, and that your copy of ncurses is built with
14+
//--enable-ext-colors. If the latter is false, you may wish to file a
15+
//bug with your distro.
16+
17+
int main()
18+
{
19+
initscr();
20+
if(has_colors() == false)
21+
{
22+
endwin();
23+
writeln("Your terminal does not support color...");
24+
return 1; //ends the program
25+
}
26+
start_color();
27+
foreach(ushort i; 0 .. 256){ // Define all the colours with the default palette
28+
init_pair(i, 0, i); // We're only setting the background here;
29+
} // The space is rendered as nothing.
30+
31+
auto w = COLS/9; // Let's just fill all the horizontal space
32+
33+
// Basic colours
34+
foreach(ushort i; 0 .. 16){
35+
attron(COLOR_PAIR(i));
36+
mvwhline(stdscr, cast(int)(i%8),w*(i/8),to!size_t(' '),w);//chtype is a size_t by spec.
37+
attroff(COLOR_PAIR(i));
38+
}
39+
40+
// 6x6x6 cubemap
41+
foreach(ushort i; 16 .. 232){
42+
attron(COLOR_PAIR(i));
43+
mvwhline(stdscr, cast(int)((i-16)%36),(2*w+w*((i-16)/36)),to!size_t(' '),w);
44+
attroff(COLOR_PAIR(i));
45+
}
46+
47+
// Greyscale
48+
foreach(ushort i; 232 .. 256){
49+
attron(COLOR_PAIR(i));
50+
mvwhline(stdscr, cast(int)(i-232),(8*w),to!size_t(' '),w);
51+
attroff(COLOR_PAIR(i));
52+
}
53+
54+
wmove(stdscr, LINES-1, COLS-1);//Moving this out of the way
55+
getch();
56+
endwin();
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)