Skip to content

Commit 20b3f90

Browse files
committed
Added a few examples, more to come.
1 parent 40145b9 commit 20b3f90

17 files changed

+1174
-0
lines changed

examples/COPYING

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright (c) 2001, Pradeep Padala.
2+
3+
All the programs in this directory and sub directories are free
4+
software; you can redistribute them and/or modify them under the
5+
terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program; if not, write to the Free Software
16+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17+
18+
Please send comments to [email protected]

examples/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Here are a few examples to get you started.
2+
3+
The main site where these examples were originally found is here:http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
4+
These examples might not be entirely working correctly, but they should demonstrate that most everything is in working order.
5+
6+
Again, please file any bugs that you come across.
7+
Even if you don't know if it is actually a bug, we appreciate the time. We understand that sometimes it is hard to figure out.

examples/acs_vars.d

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//regretfully modified by: 1100110
2+
3+
import std.string: toStringz;
4+
import ncurses;
5+
6+
void main()
7+
{
8+
initscr();
9+
10+
//Please note: you might want to maximize your terminal before you try to
11+
//run this. It does not check the size or enable scrolling.
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.. ;)
14+
//The spaces are for readability on the screen when you run the program.
15+
16+
printw(toStringz("Upper left corner "));
17+
addch(acs_map[ACS.ULCORNER]);
18+
printw("\n");
19+
20+
printw(toStringz("Lower left corner "));
21+
addch(acs_map[ACS.LLCORNER]);
22+
printw("\n");
23+
24+
printw(toStringz("Lower right corner "));
25+
addch(acs_map[ACS.LRCORNER]);
26+
printw("\n");
27+
28+
printw(toStringz("Tee pointing right "));
29+
addch(acs_map[ACS.LTEE]);
30+
printw("\n");
31+
32+
printw(toStringz("Tee pointing left "));
33+
addch(acs_map[ACS.RTEE]);
34+
printw("\n");
35+
36+
printw(toStringz("Tee pointing up "));
37+
addch(acs_map[ACS.BTEE]);
38+
printw("\n");
39+
40+
printw(toStringz("Tee pointing down "));
41+
addch(acs_map[ACS.TTEE]);
42+
printw("\n");
43+
44+
printw(toStringz("Horizontal line "));
45+
addch(acs_map[ACS.HLINE]);
46+
printw("\n");
47+
48+
printw(toStringz("Vertical line "));
49+
addch(acs_map[ACS.VLINE]);
50+
printw("\n");
51+
52+
printw(toStringz("Large Plus or cross over "));
53+
addch(acs_map[ACS.PLUS]);
54+
printw("\n");
55+
56+
printw(toStringz("Scan Line 1 "));
57+
addch(acs_map[ACS.S1]);
58+
printw("\n");
59+
60+
printw(toStringz("Scan Line 3 "));
61+
addch(acs_map[ACS.S3]);
62+
printw("\n");
63+
64+
printw(toStringz("Scan Line 7 "));
65+
addch(acs_map[ACS.S7]);
66+
printw("\n");
67+
68+
printw(toStringz("Scan Line 9 "));
69+
addch(acs_map[ACS.S9]);
70+
printw("\n");
71+
72+
printw(toStringz("Diamond "));
73+
addch(acs_map[ACS.DIAMOND]);
74+
printw("\n");
75+
76+
printw(toStringz("Checker board (stipple) "));
77+
addch(acs_map[ACS.CKBOARD]);
78+
printw("\n");
79+
80+
printw(toStringz("Degree Symbol "));
81+
addch(acs_map[ACS.DEGREE]);
82+
printw("\n");
83+
84+
printw(toStringz("Plus/Minus Symbol "));
85+
addch(acs_map[ACS.PLMINUS]);
86+
printw("\n");
87+
88+
printw(toStringz("Bullet "));
89+
addch(acs_map[ACS.BULLET]);
90+
printw("\n");
91+
92+
printw(toStringz("Arrow Pointing Left "));
93+
addch(acs_map[ACS.LARROW]);
94+
printw("\n");
95+
96+
printw(toStringz("Arrow Pointing Right "));
97+
addch(acs_map[ACS.RARROW]);
98+
printw("\n");
99+
100+
printw(toStringz("Arrow Pointing Down "));
101+
addch(acs_map[ACS.DARROW]);
102+
printw("\n");
103+
104+
printw(toStringz("Arrow Pointing Up "));
105+
addch(acs_map[ACS.UARROW]);
106+
printw("\n");
107+
108+
printw(toStringz("Board of squares "));
109+
addch(acs_map[ACS.BOARD]);
110+
printw("\n");
111+
112+
printw(toStringz("Lantern Symbol "));
113+
addch(acs_map[ACS.LANTERN]);
114+
printw("\n");
115+
116+
printw(toStringz("Solid Square Block "));
117+
addch(acs_map[ACS.BLOCK]);
118+
printw("\n");
119+
120+
printw(toStringz("Less/Equal sign "));
121+
addch(acs_map[ACS.LEQUAL]);
122+
printw("\n");
123+
124+
printw(toStringz("Greater/Equal sign "));
125+
addch(acs_map[ACS.GEQUAL]);
126+
printw("\n");
127+
128+
printw(toStringz("Pi "));
129+
addch(acs_map[ACS.PI]);
130+
printw("\n");
131+
132+
printw(toStringz("Not equal "));
133+
addch(acs_map[ACS.NEQUAL]);
134+
printw("\n");
135+
136+
printw(toStringz("UK pound sign "));
137+
addch(acs_map[ACS.STERLING]);
138+
printw("\n");
139+
140+
refresh();
141+
getch();
142+
endwin();
143+
}

examples/form_attrib.d

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import form;
2+
import std.string;
3+
4+
5+
void main()
6+
{
7+
FIELD*[3] field;
8+
FORM* my_form;
9+
int ch;
10+
11+
//Initialize curses
12+
initscr();
13+
start_color();
14+
cbreak();
15+
noecho();
16+
keypad(stdscr, true);
17+
scope(exit) endwin();
18+
19+
//Initialize a few color pairs
20+
init_pair(1, COLOR_WHITE, COLOR_BLUE);
21+
init_pair(2, COLOR_WHITE, COLOR_BLUE);
22+
23+
//Initialize the fields
24+
field[0] = new_field(1, 10, 4, 18, 0, 0);
25+
field[1] = new_field(1, 10, 6, 18, 0, 0);
26+
field[2] = null;
27+
28+
//Set field options
29+
set_field_fore(field[0], COLOR_PAIR(1)); //Put the field with blue background
30+
set_field_back(field[0], COLOR_PAIR(2)); //and white foreground (characters
31+
//are printed in white
32+
field_opts_off(field[0], O_AUTOSKIP); //Don't go to next field when this
33+
//Field is filled up
34+
set_field_back(field[1], A_UNDERLINE);
35+
field_opts_off(field[1], O_AUTOSKIP);
36+
37+
//Create the form and post it
38+
my_form = new_form(field.ptr);
39+
post_form(my_form);
40+
refresh();
41+
42+
set_current_field(my_form, field[0]); //Set focus to the colored field
43+
mvprintw(4, 10, toStringz("Value 1:"));
44+
mvprintw(6, 10, toStringz("Value 2:"));
45+
mvprintw(LINES - 2, 0, toStringz("Use UP, DOWN arrow keys to switch between fields"));
46+
refresh();
47+
48+
//Loop through to get user requests
49+
while_loop:
50+
while((ch = getch()) != KEY_F(1))
51+
{
52+
switch(ch)
53+
{
54+
case KEY_DOWN:
55+
//Go to the next field
56+
form_driver(my_form, REQ_NEXT_FIELD);
57+
//Go to the end of the present buffer
58+
//Leaves nicely at the last character
59+
form_driver(my_form, REQ_END_LINE);
60+
break;
61+
case KEY_UP:
62+
//Go to previous field
63+
form_driver(my_form, REQ_PREV_FIELD);
64+
form_driver(my_form, REQ_END_LINE);
65+
break;
66+
default:
67+
//If this is a normal character, it gets Printed
68+
form_driver(my_form, ch);
69+
break;
70+
}//switch
71+
}//while
72+
73+
//Un post form and free the memory
74+
unpost_form(my_form);
75+
free_form(my_form);
76+
free_field(field[0]);
77+
free_field(field[1]);
78+
}//main

examples/form_simple.d

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import form;
2+
3+
int main()
4+
{ FIELD*[3] field;
5+
FORM* my_form;
6+
int ch;
7+
8+
/* Initialize curses */
9+
initscr();
10+
cbreak();
11+
noecho();
12+
keypad(stdscr, true);
13+
14+
/* Initialize the fields */
15+
field[0] = new_field(1, 10, 4, 18, 0, 0);
16+
field[1] = new_field(1, 10, 6, 18, 0, 0);
17+
field[2] = null;
18+
19+
/* Set field options */
20+
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
21+
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
22+
/* Field is filled up */
23+
set_field_back(field[1], A_UNDERLINE);
24+
field_opts_off(field[1], O_AUTOSKIP);
25+
26+
/* Create the form and post it */
27+
my_form = new_form(field.ptr);
28+
post_form(my_form);
29+
refresh();
30+
31+
mvprintw(4, 10, "Value 1:");
32+
mvprintw(6, 10, "Value 2:");
33+
refresh();
34+
35+
/* Loop through to get user requests */
36+
while((ch = getch()) != KEY_F(1))
37+
{ switch(ch)
38+
{ case KEY_DOWN:
39+
/* Go to next field */
40+
form_driver(my_form, REQ_NEXT_FIELD);
41+
/* Go to the end of the present buffer */
42+
/* Leaves nicely at the last character */
43+
form_driver(my_form, REQ_END_LINE);
44+
break;
45+
case KEY_UP:
46+
/* Go to previous field */
47+
form_driver(my_form, REQ_PREV_FIELD);
48+
form_driver(my_form, REQ_END_LINE);
49+
break;
50+
default:
51+
/* If this is a normal character, it gets */
52+
/* Printed */
53+
form_driver(my_form, ch);
54+
break;
55+
}
56+
}
57+
58+
/* Un post form and free the memory */
59+
unpost_form(my_form);
60+
free_form(my_form);
61+
free_field(field[0]);
62+
free_field(field[1]);
63+
64+
endwin();
65+
return 0;
66+
}

examples/hello_world.d

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/** hello_world.d
2+
*
3+
* I'm assuming that you are starting here, so I've included
4+
* lots of info. These build, so I won't explain everything over and over.
5+
*
6+
* You will either need to use the makefile (now fixed)
7+
* or fix the import path manually.
8+
*
9+
* Also see http://d-programming-language.org/arrays.html for more info
10+
* on strings and char[]s
11+
*
12+
* Modified by: 1100110
13+
*/
14+
import std.string: toStringz; //I don't need anything but toStringz()
15+
import ncurses; //Your inport path might(will) be different
16+
17+
void main()
18+
{ //toStringz returns immutable char* Which is what most of these
19+
//functions expect. I had to modify things a bit to get that to
20+
//work as nicely as it does.
21+
//D string d = "stuff" will never work with these functions.
22+
//they expect char* see below.
23+
auto hello = toStringz("Hello ncurses World!\nPress any key to continue...");
24+
25+
/* D char[]s are not 0 terminated, so you'll probably want to do that manually
26+
* with hello ~= '\0';
27+
* char[] hello = ['h','e','l','l','o',' ','w','o','r','l','d','!'];
28+
* hello ~= '\0';
29+
* Or use toStringz() like the example above. just make sure there are no
30+
* embedded 0's in the string already.
31+
*/
32+
33+
initscr(); //initialize the screen
34+
printw(hello); //prints the char[] hello to the screen
35+
refresh(); //actually does the writing to the physical screen
36+
getch(); //gets a single character from the screen.
37+
//here it is just used to hold the terminal open.
38+
//remove it and see what happens.
39+
endwin(); //Routine to call before exiting, or leaving curses mode temporarily
40+
//failure to endwin() seems to clear all terminal history
41+
//as well as other bad things. just endwin().
42+
//Your terminal might be left in an unusable state if you don't.
43+
//Mine certainly was.
44+
}

examples/key_code.d

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Modified by: 1100110
2+
3+
import std.stdio: writefln;
4+
import ncurses;
5+
6+
7+
void main()
8+
{
9+
//I'm going to assume that you've played with
10+
//a bunch of the other tutorials by now...
11+
int ch;
12+
13+
initscr();
14+
cbreak(); //Line buffering disabled
15+
noecho();
16+
keypad(stdscr, true);
17+
18+
ch = getch();
19+
endwin();
20+
writefln("The key pressed is: %d", ch);
21+
}

0 commit comments

Comments
 (0)