Skip to content

Commit 88cda6c

Browse files
committed
fixed withChgat, acsVars, simpleKey, simpleSpectrum, tempLeave,
withChgat
1 parent 2a58fae commit 88cda6c

File tree

7 files changed

+212
-41
lines changed

7 files changed

+212
-41
lines changed

examples/Makefile

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
DMD=rdmd -g -w --build-only -ofbin/$@ -I../ -L-lncursesw $^
1+
DC=rdmd -g -w --build-only -ofbin/$@ -I../ -L-lncursesw $^
22

3-
all: init helloUnicode helloWorld simpleColor simpleKey keyCode
3+
all: init helloUnicode helloWorld simpleColor simpleKey simpleSpectrum keyCode withChgat acsVars
44

55
init:
66
@echo "building all examples..."
77

88
helloWorld: helloWorld.d
9-
$(DMD)
9+
$(DC)
1010

1111
helloUnicode: helloUnicode.d
12-
$(DMD)
12+
$(DC)
1313

1414
simpleColor: simpleColor.d
15-
$(DMD)
15+
$(DC)
1616

1717
keyCode: keyCode.d
18-
$(DMD)
18+
$(DC)
1919

2020
# TODO fix
2121
simpleKey: simpleKey.d
22-
$(DMD)
22+
$(DC)
23+
24+
simpleSpectrum: simpleSpectrum.d
25+
$(DC)
26+
27+
acsVars: acsVars.d
28+
$(DC)
29+
30+
31+
withChgat: withChgat.d
32+
$(DC)
33+
34+
tempLeave: tempLeave.d
35+
$(DC)
2336

2437
clean:
2538
@rm bin/*

examples/acs_vars.d renamed to examples/acsVars.d

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
1+
#!/usr/bin/rdmd -L-lncurses
22
import std.string: toStringz;
33
import deimos.ncurses.curses;
44

55
void main()
66
{
77
initscr();
8-
8+
scope(exit) endwin();
9+
scope(failure) endwin();
910
//Please note: you might want to maximize your terminal before you try to
1011
//run this. It does not check the size or enable scrolling.
1112
//In other word, if your terminal is <= 23 by 79, it will do weird things.
12-
//That is left as an exersize for the reader.
13-
//Plus I'm lazy and I still have to port the rest of the tutorials.. ;)
1413
//The spaces are for readability on the screen when you run the program.
1514

1615
printw(toStringz("Upper left corner "));
@@ -139,5 +138,4 @@ void main()
139138

140139
refresh();
141140
getch();
142-
endwin();
143141
}

examples/simpleKey.d

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/rdmd -L-lncursesw
2+
//This does NOT loop properly.
3+
import deimos.ncurses.ncurses;
4+
import std.conv: to;
5+
6+
enum WIDTH = 30;
7+
enum HEIGHT = 10;
8+
9+
int startx = 0;
10+
int starty = 0;
11+
12+
immutable choices = ["Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"];
13+
14+
int main()
15+
{
16+
scope(failure) endwin();
17+
scope(exit) endwin();
18+
19+
WINDOW* menu_win;
20+
int highlight = 1;
21+
int choice = 0;
22+
int c;
23+
24+
initscr();
25+
nclear();
26+
noecho();
27+
cbreak(); /* Line buffering disabled. pass on everything */
28+
startx = (80 - WIDTH) / 2;
29+
starty = (24 - HEIGHT) / 2;
30+
31+
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
32+
keypad(menu_win, true);
33+
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
34+
refresh();
35+
print_menu(menu_win, highlight);
36+
while(1)
37+
{ c = wgetch(menu_win);
38+
switch(c)
39+
{ case KEY_UP:
40+
if(highlight == 1)
41+
highlight = choices.length.to!int;
42+
else
43+
--highlight;
44+
break;
45+
case KEY_DOWN:
46+
if(highlight == choices.length)
47+
highlight = 1;
48+
else
49+
++highlight;
50+
break;
51+
case 10:
52+
choice = highlight;
53+
break;
54+
default:
55+
mvprintw(24, 0, "Character pressed is = %3d Hopefully it can be printed as '%c'", c, c);
56+
refresh();
57+
break;
58+
}
59+
print_menu(menu_win, highlight);
60+
if(choice != 0) /* User did a choice come out of the infinite loop */
61+
break;
62+
}
63+
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, (choices[choice - 1]~'\0').ptr);
64+
clrtoeol();
65+
getch();
66+
refresh();
67+
return 0;
68+
}
69+
70+
71+
void print_menu(WINDOW *menu_win, int highlight)
72+
{
73+
int x, y, i;
74+
75+
x = 2;
76+
y = 2;
77+
box(menu_win, 0, 0);
78+
for(i = 0; i < choices.length; ++i)
79+
{ if(highlight == i + 1) /* High light the present choice */
80+
{ wattron(menu_win, A_REVERSE);
81+
mvwprintw(menu_win, y, x, "%s", (choices[i]~'\0').ptr);
82+
wattroff(menu_win, A_REVERSE);
83+
}
84+
else
85+
mvwprintw(menu_win, y, x, "%s", (choices[i]~'\0').ptr);
86+
++y;
87+
}
88+
wrefresh(menu_win);
89+
}
90+

examples/simpleSpectrum.d

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

examples/tempLeave.d

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/rdmd -L-lncursesw
2+
//Modified by: 1100110
3+
4+
import std.string: toStringz;
5+
import deimos.ncurses.ncurses;
6+
7+
extern(C) int system(immutable char* command);
8+
9+
void main()
10+
{
11+
initscr(); //Start curses mode
12+
scope(failure) endwin();
13+
scope(exit) endwin();
14+
15+
printw(toStringz("Hello World !!!\nJust hit any key to continue..")); //Print Hello World
16+
refresh(); //Print it on to the real screen
17+
getch();
18+
19+
def_prog_mode(); //Save the tty modes
20+
endwin(); //End curses mode temporarily
21+
system(toStringz("/bin/sh")); //Do whatever you like in cooked mode
22+
reset_prog_mode(); //Return to the previous tty mode stored by def_prog_mode()
23+
refresh(); //Do refresh() to restore the screen contents
24+
25+
printw(toStringz("Another String\n")); //Back to curses use the full capabilities
26+
refresh();
27+
getch();
28+
}

examples/temp_leave.d

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/with_chgat.d renamed to examples/withChgat.d

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import std.string: toStringz;
1+
#!/usr/bin/rdmd -L-lncurses
2+
import std.string: toStringz;
3+
import std.conv: to;
24
import deimos.ncurses.ncurses;
35

46
void main()
5-
{ initscr(); //Start curses mode
7+
{
8+
initscr(); //Start curses mode
9+
scope(failure) endwin();
10+
scope(exit) endwin();
11+
612
start_color(); //Start color functionality
713

814
init_pair(1, COLOR_CYAN, COLOR_BLACK);
915
printw(toStringz("A Big string which i didn't care to type fully... "));
10-
mvchgat(0, 0, -1, cast(attr_t)A_BLINK, cast(short)1, cast(void*)null);
16+
//type attr_t
17+
mvchgat(0, 0, -1, A_BLINK.to!attr_t, 1.to!short, null.to!(void*));
1118
/*
1219
* First two parameters specify the position at which to start
1320
* Third parameter number of characters to update. -1 means till
@@ -20,5 +27,4 @@ void main()
2027
*/
2128
refresh();
2229
getch();
23-
endwin(); //End curses mode
2430
}

0 commit comments

Comments
 (0)