a grand renaming so that the most significant portion of the name comes first
This commit is contained in:
35
curses-practice/basics/Makefile
Normal file
35
curses-practice/basics/Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
# Makefile for JustForFun Files
|
||||
|
||||
# A few variables
|
||||
|
||||
CC=gcc
|
||||
LIBS=-lncurses
|
||||
|
||||
SRC_DIR=.
|
||||
EXE_DIR=../demo/exe
|
||||
|
||||
EXES = \
|
||||
${EXE_DIR}/hello_world \
|
||||
${EXE_DIR}/init_func_example \
|
||||
${EXE_DIR}/key_code \
|
||||
${EXE_DIR}/mouse_menu \
|
||||
${EXE_DIR}/other_border \
|
||||
${EXE_DIR}/printw_example \
|
||||
${EXE_DIR}/scanw_example \
|
||||
${EXE_DIR}/simple_attr \
|
||||
${EXE_DIR}/simple_color \
|
||||
${EXE_DIR}/simple_key \
|
||||
${EXE_DIR}/temp_leave \
|
||||
${EXE_DIR}/win_border \
|
||||
${EXE_DIR}/with_chgat
|
||||
|
||||
${EXE_DIR}/%: %.o
|
||||
${CC} -o $@ $< ${LIBS}
|
||||
|
||||
%.o: ${SRC_DIR}/%.c
|
||||
${CC} -o $@ -c $<
|
||||
|
||||
all: ${EXES}
|
||||
|
||||
clean:
|
||||
@rm -f ${EXES}
|
20
curses-practice/basics/README
Normal file
20
curses-practice/basics/README
Normal file
@@ -0,0 +1,20 @@
|
||||
Description of files
|
||||
--------------------
|
||||
basics
|
||||
|
|
||||
|----> acs_vars.c -- ACS_ variables example
|
||||
|----> hello_world.c -- Simple "Hello World" Program
|
||||
|----> init_func_example.c -- Initialization functions example
|
||||
|----> key_code.c -- Shows the scan code of the key pressed
|
||||
|----> mouse_menu.c -- A menu accessible by mouse
|
||||
|----> other_border.c -- Shows usage of other border functions apart
|
||||
| -- box()
|
||||
|----> printw_example.c -- A very simple printw() example
|
||||
|----> scanw_example.c -- A very simple getstr() example
|
||||
|----> simple_attr.c -- A program that can print a c file with comments
|
||||
| -- in attribute
|
||||
|----> simple_color.c -- A simple example demonstrating colors
|
||||
|----> simple_key.c -- A menu accessible with keyboard UP, DOWN arrows
|
||||
|----> temp_leave.c -- Demonstrates temporarily leaving curses mode
|
||||
|----> win_border.c -- Shows Creation of windows and borders
|
||||
|----> with_chgat.c -- chgat() usage example
|
44
curses-practice/basics/acs_vars.c
Normal file
44
curses-practice/basics/acs_vars.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
initscr();
|
||||
|
||||
printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n");
|
||||
printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n");
|
||||
printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n");
|
||||
printw("Tee pointing right "); addch(ACS_LTEE); printw("\n");
|
||||
printw("Tee pointing left "); addch(ACS_RTEE); printw("\n");
|
||||
printw("Tee pointing up "); addch(ACS_BTEE); printw("\n");
|
||||
printw("Tee pointing down "); addch(ACS_TTEE); printw("\n");
|
||||
printw("Horizontal line "); addch(ACS_HLINE); printw("\n");
|
||||
printw("Vertical line "); addch(ACS_VLINE); printw("\n");
|
||||
printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n");
|
||||
printw("Scan Line 1 "); addch(ACS_S1); printw("\n");
|
||||
printw("Scan Line 3 "); addch(ACS_S3); printw("\n");
|
||||
printw("Scan Line 7 "); addch(ACS_S7); printw("\n");
|
||||
printw("Scan Line 9 "); addch(ACS_S9); printw("\n");
|
||||
printw("Diamond "); addch(ACS_DIAMOND); printw("\n");
|
||||
printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n");
|
||||
printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n");
|
||||
printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n");
|
||||
printw("Bullet "); addch(ACS_BULLET); printw("\n");
|
||||
printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n");
|
||||
printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n");
|
||||
printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n");
|
||||
printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n");
|
||||
printw("Board of squares "); addch(ACS_BOARD); printw("\n");
|
||||
printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n");
|
||||
printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n");
|
||||
printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n");
|
||||
printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n");
|
||||
printw("Pi "); addch(ACS_PI); printw("\n");
|
||||
printw("Not equal "); addch(ACS_NEQUAL); printw("\n");
|
||||
printw("UK pound sign "); addch(ACS_STERLING); printw("\n");
|
||||
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
12
curses-practice/basics/hello_world.c
Normal file
12
curses-practice/basics/hello_world.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
initscr();
|
||||
printw("Hello World !!!");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
18
curses-practice/basics/hello_world.py
Normal file
18
curses-practice/basics/hello_world.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def hello_world(stdscr):
|
||||
stdscr.addstr(0, 0, "Hello World !!!")
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(hello_world)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
37
curses-practice/basics/init_func_example.c
Normal file
37
curses-practice/basics/init_func_example.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
void _do_setup(){
|
||||
initscr();
|
||||
raw();
|
||||
keypad(stdscr, TRUE);
|
||||
noecho();
|
||||
}
|
||||
|
||||
|
||||
void _display_and_quit(){
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch;
|
||||
_do_setup();
|
||||
|
||||
printw("Type any character to see it in bold\n");
|
||||
ch = getch();
|
||||
if(ch == KEY_F(1)){
|
||||
printw("F1 Key pressed");
|
||||
} else {
|
||||
printw("The pressed key is ");
|
||||
attron(A_BOLD);
|
||||
printw("%c", ch);
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
|
||||
_display_and_quit();
|
||||
|
||||
return 0;
|
||||
}
|
33
curses-practice/basics/init_func_example.py
Normal file
33
curses-practice/basics/init_func_example.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(init_func_example)
|
||||
return 0
|
||||
|
||||
|
||||
def init_func_example(stdscr):
|
||||
curses.raw()
|
||||
stdscr.keypad(1)
|
||||
curses.noecho()
|
||||
stdscr.addstr(0, 0, "Type any character to see it in bold\n")
|
||||
|
||||
ch = stdscr.getch()
|
||||
if ch == curses.KEY_F1:
|
||||
stdscr.addstr(1, 0, "F1 Key pressed")
|
||||
else:
|
||||
key_pressed = "The pressed key is "
|
||||
stdscr.addstr(1, 0, key_pressed)
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
stdscr.addstr(1, len(key_pressed), chr(ch))
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
14
curses-practice/basics/key_code.c
Normal file
14
curses-practice/basics/key_code.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main()
|
||||
{ int ch;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
ch = getch();
|
||||
endwin();
|
||||
printf("The key pressed is %d\n", ch);
|
||||
}
|
106
curses-practice/basics/mouse_menu.c
Normal file
106
curses-practice/basics/mouse_menu.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
#define WIDTH 30
|
||||
#define HEIGHT 10
|
||||
|
||||
int startx = 0;
|
||||
int starty = 0;
|
||||
|
||||
char *choices[] = { "Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit",
|
||||
};
|
||||
|
||||
int n_choices = sizeof(choices) / sizeof(char *);
|
||||
|
||||
void print_menu(WINDOW *menu_win, int highlight);
|
||||
void report_choice(int mouse_x, int mouse_y, int *p_choice);
|
||||
|
||||
int main()
|
||||
{ int c, choice = 0;
|
||||
WINDOW *menu_win;
|
||||
MEVENT event;
|
||||
|
||||
/* Initialize curses */
|
||||
initscr();
|
||||
clear();
|
||||
noecho();
|
||||
cbreak(); //Line buffering disabled. pass on everything
|
||||
|
||||
/* Try to put the window in the middle of screen */
|
||||
startx = (80 - WIDTH) / 2;
|
||||
starty = (24 - HEIGHT) / 2;
|
||||
|
||||
attron(A_REVERSE);
|
||||
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
|
||||
refresh();
|
||||
attroff(A_REVERSE);
|
||||
|
||||
/* Print the menu for the first time */
|
||||
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
|
||||
print_menu(menu_win, 1);
|
||||
/* Get all the mouse events */
|
||||
mousemask(ALL_MOUSE_EVENTS, NULL);
|
||||
|
||||
while(1)
|
||||
{ c = wgetch(menu_win);
|
||||
switch(c)
|
||||
{ case KEY_MOUSE:
|
||||
if(getmouse(&event) == OK)
|
||||
{ /* When the user clicks left mouse button */
|
||||
if(event.bstate & BUTTON1_PRESSED)
|
||||
{ report_choice(event.x + 1, event.y + 1, &choice);
|
||||
if(choice == -1) //Exit chosen
|
||||
goto end;
|
||||
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
print_menu(menu_win, choice);
|
||||
break;
|
||||
}
|
||||
}
|
||||
end:
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void print_menu(WINDOW *menu_win, int highlight)
|
||||
{
|
||||
int x, y, i;
|
||||
|
||||
x = 2;
|
||||
y = 2;
|
||||
box(menu_win, 0, 0);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
{ if(highlight == i + 1)
|
||||
{ wattron(menu_win, A_REVERSE);
|
||||
mvwprintw(menu_win, y, x, "%s", choices[i]);
|
||||
wattroff(menu_win, A_REVERSE);
|
||||
}
|
||||
else
|
||||
mvwprintw(menu_win, y, x, "%s", choices[i]);
|
||||
++y;
|
||||
}
|
||||
wrefresh(menu_win);
|
||||
}
|
||||
|
||||
/* Report the choice according to mouse position */
|
||||
void report_choice(int mouse_x, int mouse_y, int *p_choice)
|
||||
{ int i,j, choice;
|
||||
|
||||
i = startx + 2;
|
||||
j = starty + 3;
|
||||
|
||||
for(choice = 0; choice < n_choices; ++choice)
|
||||
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
|
||||
{ if(choice == n_choices - 1)
|
||||
*p_choice = -1;
|
||||
else
|
||||
*p_choice = choice + 1;
|
||||
break;
|
||||
}
|
||||
}
|
120
curses-practice/basics/other_border.c
Normal file
120
curses-practice/basics/other_border.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
typedef struct _win_border_struct {
|
||||
chtype ls, rs, ts, bs,
|
||||
tl, tr, bl, br;
|
||||
}WIN_BORDER;
|
||||
|
||||
typedef struct _WIN_struct {
|
||||
|
||||
int startx, starty;
|
||||
int height, width;
|
||||
WIN_BORDER border;
|
||||
}WIN;
|
||||
|
||||
void init_win_params(WIN *p_win);
|
||||
void print_win_params(WIN *p_win);
|
||||
void create_box(WIN *win, bool flag);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{ WIN win;
|
||||
int ch;
|
||||
|
||||
initscr(); /* Start curses mode */
|
||||
start_color(); /* Start the color functionality */
|
||||
cbreak(); /* Line buffering disabled, Pass on
|
||||
* everty thing to me */
|
||||
keypad(stdscr, TRUE); /* I need that nifty F1 */
|
||||
noecho();
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
/* Initialize the window parameters */
|
||||
init_win_params(&win);
|
||||
print_win_params(&win);
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
printw("Press F1 to exit");
|
||||
refresh();
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
create_box(&win, TRUE);
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_LEFT:
|
||||
create_box(&win, FALSE);
|
||||
--win.startx;
|
||||
create_box(&win, TRUE);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
create_box(&win, FALSE);
|
||||
++win.startx;
|
||||
create_box(&win, TRUE);
|
||||
break;
|
||||
case KEY_UP:
|
||||
create_box(&win, FALSE);
|
||||
--win.starty;
|
||||
create_box(&win, TRUE);
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
create_box(&win, FALSE);
|
||||
++win.starty;
|
||||
create_box(&win, TRUE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
endwin(); /* End curses mode */
|
||||
return 0;
|
||||
}
|
||||
void init_win_params(WIN *p_win)
|
||||
{
|
||||
p_win->height = 3;
|
||||
p_win->width = 10;
|
||||
p_win->starty = (LINES - p_win->height)/2;
|
||||
p_win->startx = (COLS - p_win->width)/2;
|
||||
|
||||
p_win->border.ls = '|';
|
||||
p_win->border.rs = '|';
|
||||
p_win->border.ts = '-';
|
||||
p_win->border.bs = '-';
|
||||
p_win->border.tl = '+';
|
||||
p_win->border.tr = '+';
|
||||
p_win->border.bl = '+';
|
||||
p_win->border.br = '+';
|
||||
|
||||
}
|
||||
void print_win_params(WIN *p_win)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
|
||||
p_win->width, p_win->height);
|
||||
refresh();
|
||||
#endif
|
||||
}
|
||||
void create_box(WIN *p_win, bool flag)
|
||||
{ int i, j;
|
||||
int x, y, w, h;
|
||||
|
||||
x = p_win->startx;
|
||||
y = p_win->starty;
|
||||
w = p_win->width;
|
||||
h = p_win->height;
|
||||
|
||||
if(flag == TRUE)
|
||||
{ mvaddch(y, x, p_win->border.tl);
|
||||
mvaddch(y, x + w, p_win->border.tr);
|
||||
mvaddch(y + h, x, p_win->border.bl);
|
||||
mvaddch(y + h, x + w, p_win->border.br);
|
||||
mvhline(y, x + 1, p_win->border.ts, w - 1);
|
||||
mvhline(y + h, x + 1, p_win->border.bs, w - 1);
|
||||
mvvline(y + 1, x, p_win->border.ls, h - 1);
|
||||
mvvline(y + 1, x + w, p_win->border.rs, h - 1);
|
||||
|
||||
}
|
||||
else
|
||||
for(j = y; j <= y + h; ++j)
|
||||
for(i = x; i <= x + w; ++i)
|
||||
mvaddch(j, i, ' ');
|
||||
|
||||
refresh();
|
||||
|
||||
}
|
19
curses-practice/basics/printw_example.c
Normal file
19
curses-practice/basics/printw_example.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char mesg[] = "Just a string";
|
||||
int row, col;
|
||||
|
||||
initscr();
|
||||
getmaxyx(stdscr, row, col);
|
||||
mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg);
|
||||
mvprintw(row - 2, 0, "This screen has %d rows and %d columns\n", row, col);
|
||||
printw("Try resizing your window (if possible) and then run this program again");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
25
curses-practice/basics/printw_example.py
Normal file
25
curses-practice/basics/printw_example.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def printw_example(stdscr):
|
||||
mesg = "Just a string"
|
||||
row, col = stdscr.getmaxyx()
|
||||
stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg)
|
||||
stdscr.addstr(row - 2, 0, "This screen has {0} rows and {1} "
|
||||
"columns\n".format(row, col))
|
||||
stdscr.addstr(row - 1, 0, "Try resizing your window (if possible) and then"
|
||||
"run this program again")
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(printw_example)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
19
curses-practice/basics/scanw_example.c
Normal file
19
curses-practice/basics/scanw_example.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char * mesg = "Enter a string: ";
|
||||
char str[80];
|
||||
int row, col;
|
||||
|
||||
initscr();
|
||||
getmaxyx(stdscr, row, col);
|
||||
mvprintw(row / 2, (col - strlen(mesg)) / 2, "%s", mesg);
|
||||
getstr(str);
|
||||
mvprintw(LINES - 2, 0, "You Entered: %s", str);
|
||||
getch();
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
24
curses-practice/basics/scanw_example.py
Normal file
24
curses-practice/basics/scanw_example.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def scanw_example(stdscr):
|
||||
mesg = 'Enter a string: '
|
||||
|
||||
row, col = stdscr.getmaxyx()
|
||||
curses.echo()
|
||||
stdscr.addstr(row / 2, (col - len(mesg)) / 2, mesg)
|
||||
instr = stdscr.getstr()
|
||||
stdscr.addstr(row - 2, 0, 'You Entered: {0}'.format(instr))
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(scanw_example)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
56
curses-practice/basics/simple_attr.c
Normal file
56
curses-practice/basics/simple_attr.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */
|
||||
#include <ncurses.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch, prev, row, col;
|
||||
prev = EOF;
|
||||
FILE *fp;
|
||||
int y, x;
|
||||
|
||||
if(argc != 2)
|
||||
{
|
||||
printf("Usage: %s <a c file name>\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
fp = fopen(argv[1], "r");
|
||||
if(fp == NULL)
|
||||
{
|
||||
perror("Cannot open input file");
|
||||
exit(1);
|
||||
}
|
||||
initscr(); /* Start curses mode */
|
||||
getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */
|
||||
while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */
|
||||
{
|
||||
getyx(stdscr, y, x); /* get the current curser position */
|
||||
if(y == (row - 1)) /* are we are at the end of the screen */
|
||||
{
|
||||
printw("<-Press Any Key->"); /* tell the user to press a key */
|
||||
getch();
|
||||
clear(); /* clear the screen */
|
||||
move(0, 0); /* start at the beginning of the screen */
|
||||
}
|
||||
if(prev == '/' && ch == '*') /* If it is / and * then only
|
||||
* switch bold on */
|
||||
{
|
||||
attron(A_BOLD); /* cut bold on */
|
||||
getyx(stdscr, y, x); /* get the current curser position */
|
||||
move(y, x - 1); /* back up one space */
|
||||
printw("%c%c", '/', ch); /* The actual printing is done here */
|
||||
} else {
|
||||
printw("%c", ch);
|
||||
}
|
||||
refresh();
|
||||
|
||||
if(prev == '*' && ch == '/') {
|
||||
attroff(A_BOLD); /* Switch it off once we got */
|
||||
}
|
||||
/* and then / */
|
||||
prev = ch;
|
||||
}
|
||||
endwin(); /* End curses mode */
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
50
curses-practice/basics/simple_attr.py
Normal file
50
curses-practice/basics/simple_attr.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import curses
|
||||
|
||||
FILE = {'fp': None}
|
||||
|
||||
|
||||
def simple_attr(stdscr):
|
||||
row, col = stdscr.getmaxyx()
|
||||
prev = ''
|
||||
fp = FILE['fp']
|
||||
for line in fp:
|
||||
for ch in line:
|
||||
y, x = stdscr.getyx()
|
||||
if y == (row - 1):
|
||||
stdscr.addstr(row - 1, 0, "<-Press Any Key->")
|
||||
stdscr.getch()
|
||||
stdscr.clear()
|
||||
stdscr.move(0, 0)
|
||||
elif prev == '/' and ch == '*':
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
y, x = stdscr.getyx()
|
||||
stdscr.addstr(y, x - 1, '/{0}'.format(ch))
|
||||
else:
|
||||
stdscr.addstr(y, x, ch)
|
||||
stdscr.refresh()
|
||||
|
||||
if prev == '*' and ch == '/':
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
prev = ch
|
||||
|
||||
|
||||
def main(sysargs=sys.argv[:]):
|
||||
if not sysargs[1:]:
|
||||
print('Usage: {0} <a c file name>'.format(sysargs[0]))
|
||||
return 1
|
||||
try:
|
||||
FILE['fp'] = open(sysargs[1], 'r')
|
||||
except (OSError, IOError):
|
||||
print('Cannot open input file', file=sys.stderr)
|
||||
return 1
|
||||
curses.wrapper(simple_attr)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
40
curses-practice/basics/simple_color.c
Normal file
40
curses-practice/basics/simple_color.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
|
||||
int main(int argc, char *argv[])
|
||||
{ initscr(); /* Start curses mode */
|
||||
if(has_colors() == FALSE)
|
||||
{ endwin();
|
||||
printf("Your terminal does not support color\n");
|
||||
exit(1);
|
||||
}
|
||||
start_color(); /* Start color */
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
|
||||
attroff(COLOR_PAIR(1));
|
||||
getch();
|
||||
endwin();
|
||||
}
|
||||
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
|
||||
{ int length, x, y;
|
||||
float temp;
|
||||
|
||||
if(win == NULL)
|
||||
win = stdscr;
|
||||
getyx(win, y, x);
|
||||
if(startx != 0)
|
||||
x = startx;
|
||||
if(starty != 0)
|
||||
y = starty;
|
||||
if(width == 0)
|
||||
width = 80;
|
||||
|
||||
length = strlen(string);
|
||||
temp = (width - length)/ 2;
|
||||
x = startx + (int)temp;
|
||||
mvwprintw(win, y, x, "%s", string);
|
||||
refresh();
|
||||
}
|
||||
|
92
curses-practice/basics/simple_key.c
Normal file
92
curses-practice/basics/simple_key.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <stdio.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#define WIDTH 30
|
||||
#define HEIGHT 10
|
||||
|
||||
int startx = 0;
|
||||
int starty = 0;
|
||||
|
||||
char *choices[] = {
|
||||
"Choice 1",
|
||||
"Choice 2",
|
||||
"Choice 3",
|
||||
"Choice 4",
|
||||
"Exit",
|
||||
};
|
||||
int n_choices = sizeof(choices) / sizeof(char *);
|
||||
void print_menu(WINDOW *menu_win, int highlight);
|
||||
|
||||
int main()
|
||||
{ WINDOW *menu_win;
|
||||
int highlight = 1;
|
||||
int choice = 0;
|
||||
int c;
|
||||
|
||||
initscr();
|
||||
clear();
|
||||
noecho();
|
||||
cbreak(); /* Line buffering disabled. pass on everything */
|
||||
startx = (80 - WIDTH) / 2;
|
||||
starty = (24 - HEIGHT) / 2;
|
||||
|
||||
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
|
||||
keypad(menu_win, TRUE);
|
||||
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
|
||||
refresh();
|
||||
print_menu(menu_win, highlight);
|
||||
while(1)
|
||||
{ c = wgetch(menu_win);
|
||||
switch(c)
|
||||
{ case KEY_UP:
|
||||
if(highlight == 1)
|
||||
highlight = n_choices;
|
||||
else
|
||||
--highlight;
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if(highlight == n_choices)
|
||||
highlight = 1;
|
||||
else
|
||||
++highlight;
|
||||
break;
|
||||
case 10:
|
||||
choice = highlight;
|
||||
break;
|
||||
default:
|
||||
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
|
||||
refresh();
|
||||
break;
|
||||
}
|
||||
print_menu(menu_win, highlight);
|
||||
if(choice != 0) /* User did a choice come out of the infinite loop */
|
||||
break;
|
||||
}
|
||||
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
|
||||
clrtoeol();
|
||||
refresh();
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void print_menu(WINDOW *menu_win, int highlight)
|
||||
{
|
||||
int x, y, i;
|
||||
|
||||
x = 2;
|
||||
y = 2;
|
||||
box(menu_win, 0, 0);
|
||||
for(i = 0; i < n_choices; ++i)
|
||||
{ if(highlight == i + 1) /* High light the present choice */
|
||||
{ wattron(menu_win, A_REVERSE);
|
||||
mvwprintw(menu_win, y, x, "%s", choices[i]);
|
||||
wattroff(menu_win, A_REVERSE);
|
||||
}
|
||||
else
|
||||
mvwprintw(menu_win, y, x, "%s", choices[i]);
|
||||
++y;
|
||||
}
|
||||
wrefresh(menu_win);
|
||||
}
|
||||
|
20
curses-practice/basics/temp_leave.c
Normal file
20
curses-practice/basics/temp_leave.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
initscr(); /* Start curses mode */
|
||||
printw("Hello World !!!\n"); /* Print Hello World */
|
||||
refresh(); /* Print it on to the real screen */
|
||||
def_prog_mode(); /* Save the tty modes */
|
||||
endwin(); /* End curses mode temporarily */
|
||||
system("/bin/sh"); /* Do whatever you like in cooked mode */
|
||||
reset_prog_mode(); /* Return to the previous tty mode*/
|
||||
/* stored by def_prog_mode() */
|
||||
refresh(); /* Do refresh() to restore the */
|
||||
/* Screen contents */
|
||||
printw("Another String\n"); /* Back to curses use the full */
|
||||
refresh(); /* capabilities of curses */
|
||||
endwin(); /* End curses mode */
|
||||
|
||||
return 0;
|
||||
}
|
82
curses-practice/basics/win_border.c
Normal file
82
curses-practice/basics/win_border.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
WINDOW *create_newwin(int height, int width, int starty, int startx);
|
||||
void destroy_win(WINDOW *local_win);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{ WINDOW *my_win;
|
||||
int startx, starty, width, height;
|
||||
int ch;
|
||||
|
||||
initscr(); /* Start curses mode */
|
||||
cbreak(); /* Line buffering disabled, Pass on
|
||||
* everty thing to me */
|
||||
keypad(stdscr, TRUE); /* I need that nifty F1 */
|
||||
|
||||
height = 3;
|
||||
width = 10;
|
||||
starty = (LINES - height) / 2; /* Calculating for a center placement */
|
||||
startx = (COLS - width) / 2; /* of the window */
|
||||
printw("Press F1 to exit");
|
||||
refresh();
|
||||
my_win = create_newwin(height, width, starty, startx);
|
||||
|
||||
while((ch = getch()) != KEY_F(1))
|
||||
{ switch(ch)
|
||||
{ case KEY_LEFT:
|
||||
destroy_win(my_win);
|
||||
my_win = create_newwin(height, width, starty,--startx);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
destroy_win(my_win);
|
||||
my_win = create_newwin(height, width, starty,++startx);
|
||||
break;
|
||||
case KEY_UP:
|
||||
destroy_win(my_win);
|
||||
my_win = create_newwin(height, width, --starty,startx);
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
destroy_win(my_win);
|
||||
my_win = create_newwin(height, width, ++starty,startx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
endwin(); /* End curses mode */
|
||||
return 0;
|
||||
}
|
||||
|
||||
WINDOW *create_newwin(int height, int width, int starty, int startx)
|
||||
{ WINDOW *local_win;
|
||||
|
||||
local_win = newwin(height, width, starty, startx);
|
||||
box(local_win, 0 , 0); /* 0, 0 gives default characters
|
||||
* for the vertical and horizontal
|
||||
* lines */
|
||||
wrefresh(local_win); /* Show that box */
|
||||
|
||||
return local_win;
|
||||
}
|
||||
|
||||
void destroy_win(WINDOW *local_win)
|
||||
{
|
||||
/* box(local_win, ' ', ' '); : This won't produce the desired
|
||||
* result of erasing the window. It will leave it's four corners
|
||||
* and so an ugly remnant of window.
|
||||
*/
|
||||
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
|
||||
/* The parameters taken are
|
||||
* 1. win: the window on which to operate
|
||||
* 2. ls: character to be used for the left side of the window
|
||||
* 3. rs: character to be used for the right side of the window
|
||||
* 4. ts: character to be used for the top side of the window
|
||||
* 5. bs: character to be used for the bottom side of the window
|
||||
* 6. tl: character to be used for the top left corner of the window
|
||||
* 7. tr: character to be used for the top right corner of the window
|
||||
* 8. bl: character to be used for the bottom left corner of the window
|
||||
* 9. br: character to be used for the bottom right corner of the window
|
||||
*/
|
||||
wrefresh(local_win);
|
||||
delwin(local_win);
|
||||
}
|
15
curses-practice/basics/with_chgat.c
Normal file
15
curses-practice/basics/with_chgat.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
initscr();
|
||||
start_color();
|
||||
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
||||
printw("A Big string which i didn't care to type fully ");
|
||||
mvchgat(0, 0, -1, A_BLINK, 1, NULL);
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
21
curses-practice/basics/with_chgat.py
Normal file
21
curses-practice/basics/with_chgat.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import sys
|
||||
import curses
|
||||
|
||||
|
||||
def with_chgat(stdscr):
|
||||
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
stdscr.addstr("A Big string which i didn't care to type fully ",
|
||||
curses.color_pair(1) | curses.A_BLINK)
|
||||
stdscr.refresh()
|
||||
stdscr.getch()
|
||||
|
||||
|
||||
def main():
|
||||
curses.wrapper(with_chgat)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
# vim:filetype=python
|
Reference in New Issue
Block a user