Skip to content

Commit 12e3f98

Browse files
committed
Added a few more examples
1 parent 41c455f commit 12e3f98

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

examples/init_func_example.d

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This is rather interesting, it prints the bold version
2+
* of whatever you type on the screen.
3+
*
4+
* Today's exercise: put all of this in a while loop,
5+
* so that you can keep typing stuff in until you get bored.
6+
* Might be harder than you think. ;)
7+
* Modified by: 1100110
8+
*/
9+
10+
import std.string: toStringz;
11+
import ncurses;
12+
13+
void main()
14+
{
15+
int ch;
16+
17+
initscr(); //Start curses mode
18+
raw(); //Line buffering disabled
19+
keypad(stdscr, true); //We want to get F1, F2 etc..
20+
noecho(); //Don't echo() while we do getch
21+
22+
printw(toStringz("Type any character to see it in bold!\n"));
23+
ch = getch(); //If raw() hadn't been called
24+
//we have to press enter before it
25+
//gets to the program
26+
27+
if(ch == KEY_F(1)) //Without keypad enabled this will not get to us either
28+
printw(toStringz("F1 Key pressed"));
29+
//Without noecho() some ugly escape
30+
//charachters might have been printed
31+
//to the screen
32+
else
33+
{ printw(toStringz("The pressed key is: "));
34+
attron(A_BOLD);
35+
printw("%c", ch); //again with the implicit conversion...
36+
attroff(A_BOLD);
37+
}
38+
refresh(); //Actually print it on the screen
39+
getch(); //Wait for user input/Hold the terminal open
40+
endwin(); //End curses mode
41+
}
42+

examples/printw_example.d

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Modified by: 1100110
2+
3+
import std.string;
4+
import ncurses; /* ncurses.h includes stdio.h */
5+
6+
void main()
7+
{
8+
string mesg = "Just a string..."; //message to appear on the screen
9+
int row, col; //to store the number of rows and
10+
//the number of colums of the screen
11+
initscr(); //start the curses mode
12+
getmaxyx(stdscr, row, col); //get the number of rows and columns
13+
//print the message at the center of the screen
14+
mvprintw(row/2, (col-(mesg.length - 1))/2, "%s", toStringz(mesg));
15+
//Did you notice? there is a '%s' not toStringified.
16+
//In one of those great weird blips, D2 will pass small characters
17+
//like that, and yet will choke on larger strings.
18+
//This is where implicit rules make things unexpected happen.
19+
20+
auto rowcol = toStringz("This screen has %d rows and %d columns\n");
21+
mvprintw(row-2, 0, rowcol, row+1, col+1);
22+
printw(toStringz("Try resizing your window(if possible) and then run this program again"));
23+
refresh();
24+
getch();
25+
endwin();
26+
}

examples/simple_attr.d

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//pager functionality by Joseph Spainhour" <[email protected]>
2+
//Modified by: 1100110
3+
//I believe that this relies on deprecated features..
4+
//Gonna have to be rewritten I believe.
5+
6+
import std.stdio: writefln;
7+
import std.string: toStringz;
8+
import std.c.stdio;
9+
import ncurses;
10+
11+
12+
int main(char[][] args)
13+
{
14+
//um...can you put a char into an int? will that work?
15+
//I think that is the problem...
16+
int ch, prev, row, col;
17+
prev = EOF;
18+
FILE* fp;
19+
int y, x;
20+
21+
if(args.length != 2)
22+
{
23+
writefln("Usage: %s <a c file name>\n", args[0]);
24+
return 1; //Exit
25+
}//if
26+
27+
fp = fopen(toStringz(args[1]), "r");
28+
if(fp == null)
29+
{
30+
perror("Cannot open input file");
31+
return 1;
32+
}//if
33+
initscr(); //start curses mode
34+
getmaxyx(stdscr, row, col); //find the boundaries of the screeen
35+
while((ch = fgetc(fp)) != EOF) //read the file till we reach the end
36+
{
37+
getyx(stdscr, y, x); //get the current curser position
38+
if(y == (row - 1)) //are we are at the end of the screen
39+
{
40+
printw(toStringz("<-press any key->")); //tell the user to press a key
41+
getch();
42+
clear(); //clear the screen
43+
move(0, 0); //start at the beginning of the screen
44+
}//if
45+
if(prev == '/' && ch == '*') //if it is / and * then only switch bold on
46+
{
47+
attron(A_BOLD); //cut bold on
48+
getyx(stdscr, y, x); //get the current curser position
49+
move(y, x - 1); //back up one space
50+
printw("%c%c", '/', ch); //the actual printing is done here
51+
}//if
52+
else
53+
printw("%c", ch);
54+
refresh();
55+
if(prev == '*' && ch == '/')
56+
attroff(A_BOLD); //switch it off once we got * and then /
57+
prev = ch;
58+
}//while
59+
getch();
60+
endwin(); //end curses mode
61+
fclose(fp);
62+
63+
return 0;
64+
}//main

examples/with_chgat.d

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import std.string: toStringz;
2+
import ncurses;
3+
4+
void main()
5+
{ initscr(); //Start curses mode
6+
start_color(); //Start color functionality
7+
8+
init_pair(1, COLOR_CYAN, COLOR_BLACK);
9+
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, null);
11+
/*
12+
* First two parameters specify the position at which to start
13+
* Third parameter number of characters to update. -1 means till
14+
* end of line
15+
* Forth parameter is the normal attribute you wanted to give
16+
* to the charcter
17+
* Fifth is the color index. It is the index given during init_pair()
18+
* use 0 if you didn't want color
19+
* Sixth one is always NULL
20+
*/
21+
refresh();
22+
getch();
23+
endwin(); //End curses mode
24+
}

0 commit comments

Comments
 (0)