Initial curses integration

modified:   src/Makefile
	modified:   src/nms.h
This commit is contained in:
Brian Barto 2016-04-14 16:18:42 -04:00
parent ece2551814
commit 46fc803fe7
2 changed files with 39 additions and 57 deletions

View File

@ -1,15 +1,16 @@
CC = gcc CC = gcc
ODIR = obj ODIR = obj
DEPS = nms.h DEPS = nms.h
CFLAGS = -lncurses
_OBJ = nms.o _OBJ = nms.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS) $(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CC) -c -o $@ $< $(CFLAGS)
nms: $(OBJ) nms: $(OBJ)
$(CC) -o $@ $^ $(CC) -o $@ $^ $(CFLAGS)
clean: clean:
rm -f $(ODIR)/*.o nms rm -f $(ODIR)/*.o nms

View File

@ -1,5 +1,5 @@
#include <sys/ioctl.h>
#include <stdio.h> #include <stdio.h>
#include <ncurses.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -31,9 +31,6 @@ char *display = NULL;
void nmsprintf(const char *, ...); void nmsprintf(const char *, ...);
void nmsexec(void); void nmsexec(void);
int getTermSizeRows(void);
int getTermSizeCols(void);
void clearTermWindow(int, int);
char getMaskChar(void); char getMaskChar(void);
void nmsprintf(const char *format, ...) { void nmsprintf(const char *format, ...) {
@ -56,13 +53,26 @@ void nmsexec(void) {
struct winpos *list_pointer = NULL; struct winpos *list_pointer = NULL;
struct winpos *start; // Always points to start of list struct winpos *start; // Always points to start of list
struct winpos *temp; // Used for free()ing the list struct winpos *temp; // Used for free()ing the list
int termSizeRows = getTermSizeRows(); int termSizeRows, termSizeCols;
int termSizeCols = getTermSizeCols(); int c, n, x = 0, y = 0;
int c, n, x = 1, y = 1;
int r_time, r_time_l, r_time_s; int r_time, r_time_l, r_time_s;
int ms, ls; int ms, ls;
bool first = true; bool first = true;
// Start and initialize curses mode
initscr();
cbreak();
noecho();
// Setting up and starting colors if terminal supports them
if (has_colors()) {
start_color();
init_pair(1, COLOR_BLUE, COLOR_BLACK);
}
// Get terminal window size
getmaxyx(stdscr, termSizeRows, termSizeCols);
// Seed my random number generator with the current time // Seed my random number generator with the current time
srand(time(NULL)); srand(time(NULL));
@ -71,13 +81,13 @@ void nmsexec(void) {
while ((c = display[n++]) != '\0') { while ((c = display[n++]) != '\0') {
if (c == NEWLINE) { if (c == NEWLINE) {
++y; ++y;
x = 1; x = 0;
} else if (c == TAB && x + 4 <= termSizeCols) { } else if (c == TAB && x + 4 <= termSizeCols) {
x += 4; x += 4;
} else if (isspace(c)) { } else if (isspace(c)) {
if (++x > termSizeCols) { if (++x > termSizeCols) {
++y; ++y;
x = 1; x = 0;
} }
} else { } else {
if (first) { if (first) {
@ -106,7 +116,7 @@ void nmsexec(void) {
if (++x > termSizeCols) { if (++x > termSizeCols) {
++y; ++y;
x = 1; x = 0;
} }
} }
} }
@ -114,16 +124,14 @@ void nmsexec(void) {
// Freeing the display character array // Freeing the display character array
free(display); free(display);
clearTermWindow(termSizeRows, termSizeCols);
// Initially display the characters in the terminal with a 'type effect'. // Initially display the characters in the terminal with a 'type effect'.
ms = 5; // miliseconds, used for usleep() ms = 5; // miliseconds, used for usleep()
list_pointer = start; list_pointer = start;
while (list_pointer != NULL && list_pointer->row <= termSizeRows) { while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask); mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
refresh();
list_pointer->mask = getMaskChar(); list_pointer->mask = getMaskChar();
list_pointer = list_pointer->next; list_pointer = list_pointer->next;
fflush(stdout);
usleep(ms * 1000); usleep(ms * 1000);
} }
@ -138,11 +146,11 @@ void nmsexec(void) {
while (x < (ls * 1000) / ms) { while (x < (ls * 1000) / ms) {
list_pointer = start; list_pointer = start;
while (list_pointer != NULL && list_pointer->row <= termSizeRows) { while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask); mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
refresh();
list_pointer->mask = getMaskChar(); list_pointer->mask = getMaskChar();
list_pointer = list_pointer->next; list_pointer = list_pointer->next;
} }
fflush(stdout);
usleep(ms * 1000); usleep(ms * 1000);
++x; ++x;
} }
@ -168,37 +176,38 @@ void nmsexec(void) {
list_pointer->mask = getMaskChar(); list_pointer->mask = getMaskChar();
} else { } else {
list_pointer->mask = list_pointer->source; list_pointer->mask = list_pointer->source;
printf(KCYN); //printf(KCYN);
} }
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask); mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
printf(KNRM); refresh();
list_pointer = list_pointer->next; list_pointer = list_pointer->next;
} }
fflush(stdout);
usleep(ms * 1000); usleep(ms * 1000);
} }
printf("\n");
// Printing remaining characters from list if we stopped short due to // Printing remaining characters from list if we stopped short due to
// a terminal row limitation. i.e. the number of textual rows in the input // a terminal row limitation. i.e. the number of textual rows in the input
// stream were greater than the number of rows in the terminal. // stream were greater than the number of rows in the terminal.
/* TODO - Handle overflow data via ncurses in some way
int prevRow; int prevRow;
if (list_pointer != NULL) { if (list_pointer != NULL) {
prevRow = list_pointer->row; prevRow = list_pointer->row;
printf(KCYN); //printf(KCYN);
while (list_pointer != NULL) { while (list_pointer != NULL) {
while (list_pointer->row > prevRow) { while (list_pointer->row > prevRow) {
printf("\n"); //printf("\n");
++prevRow; ++prevRow;
} }
printf("\033[%i;%iH%c", termSizeRows, list_pointer->col, list_pointer->source); mvaddch(list_pointer->row, list_pointer->col, list_pointer->source);
list_pointer = list_pointer->next; list_pointer = list_pointer->next;
} }
printf(KNRM); //printf(KNRM);
printf("\n"); //printf("\n");
} }
*/
// End curses mode
endwin();
// Freeing the list. // Freeing the list.
list_pointer = start; list_pointer = start;
@ -209,34 +218,6 @@ void nmsexec(void) {
} }
} }
int getTermSizeRows(void) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_row;
}
int getTermSizeCols(void) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
}
void clearTermWindow(int pRows, int pCols) {
int row, col;
// Clearing window
for (row = 1; row <= pRows; ++row) {
for (col = 1; col <= pCols; ++col)
printf("\033[%i;%iH%c", row, col, SPACE);
printf("\033[%i;%iH%c", row, col, NEWLINE);
}
// Position cursor at the top
printf("\033[%i;%iH", 1, 1);
}
char getMaskChar(void) { char getMaskChar(void) {
char *maskChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" char *maskChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"