initial
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.ccls-cache/
|
||||
*.o
|
||||
nctest
|
||||
14
Makefile
Normal file
14
Makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
include config.mk
|
||||
|
||||
OBJS = main.o window.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
rm -f *.o $(TARGET)
|
||||
3
config.mk
Normal file
3
config.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -std=c11 -lncurses
|
||||
TARGET = nctest
|
||||
22
hacks.h
Normal file
22
hacks.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef HACKS_H
|
||||
#define HACKS_H
|
||||
|
||||
/*
|
||||
* Ugly hack to print a message in the center column by taking half its length
|
||||
* and subtracting that from the desired X position.
|
||||
*/
|
||||
#define midprint(y, x, len, message) mvwprintw(stdscr, y, x - (len * 0.5), message)
|
||||
|
||||
/*
|
||||
* @brief string pointer with manual length stuck to it
|
||||
*/
|
||||
typedef struct {
|
||||
char *s;
|
||||
int l;
|
||||
} lstring_t;
|
||||
|
||||
struct xy {
|
||||
int x, y;
|
||||
};
|
||||
|
||||
#endif
|
||||
12
main.c
Normal file
12
main.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <curses.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "hacks.h"
|
||||
#include "window.h"
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
return homewin();
|
||||
}
|
||||
|
||||
108
window.c
Normal file
108
window.c
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <curses.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "hacks.h"
|
||||
|
||||
/*
|
||||
* Execute actual code and draw on the window.
|
||||
* Should be called by a function that creates a window.
|
||||
* Returns 0 if successful.
|
||||
*/
|
||||
int
|
||||
dostuff(void)
|
||||
{
|
||||
/*
|
||||
* Using unsafe strings right now, but should change this later to use
|
||||
* malloc(), strcpy, the like.
|
||||
*/
|
||||
lstring_t msg;
|
||||
|
||||
struct xy max;
|
||||
struct xy center;
|
||||
|
||||
getmaxyx(stdscr, max.y, max.x);
|
||||
|
||||
center.y = max.y * 0.5;
|
||||
center.x = max.x * 0.5;
|
||||
|
||||
msg.s = "Hello, World!";
|
||||
msg.l = 14;
|
||||
//mvwprintw(stdscr, center.y, center.x - (msg.l * 0.5), msg.s); // gross
|
||||
midprint(center.y, center.x, msg.l, msg.s);
|
||||
refresh();
|
||||
|
||||
sleep(3);
|
||||
|
||||
msg.s = "You can leave in 3...";
|
||||
msg.l = 21;
|
||||
midprint(center.y, center.x, msg.l, msg.s);
|
||||
refresh();
|
||||
|
||||
sleep(1);
|
||||
|
||||
msg.s = "You can leave in 2...";
|
||||
msg.l = 21;
|
||||
midprint(center.y, center.x, msg.l, msg.s);
|
||||
refresh();
|
||||
|
||||
sleep(1);
|
||||
|
||||
msg.s = "You can leave in 1...";
|
||||
msg.l = 21;
|
||||
midprint(center.y, center.x, msg.l, msg.s);
|
||||
refresh();
|
||||
|
||||
sleep(1);
|
||||
|
||||
msg.s = "Press Any Key to Leave";
|
||||
msg.l = 22;
|
||||
midprint(center.y, center.x, msg.l, msg.s);
|
||||
refresh();
|
||||
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Full lifetime of main window happens here.
|
||||
* Returns 0 if successful after window closes.
|
||||
*/
|
||||
int
|
||||
homewin(void)
|
||||
{
|
||||
int err, ret;
|
||||
err = ret = 0;
|
||||
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr, TRUE);
|
||||
curs_set(0);
|
||||
|
||||
if (!has_colors()) {
|
||||
endwin();
|
||||
puts("err: no color");
|
||||
return 1;
|
||||
}
|
||||
|
||||
start_color();
|
||||
init_pair(1, COLOR_BLACK, COLOR_MAGENTA);
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
|
||||
/* Window Title */
|
||||
mvwprintw(stdscr, 2, 2, "Test Program");
|
||||
|
||||
err = dostuff();
|
||||
|
||||
attroff(COLOR_PAIR(1));
|
||||
endwin();
|
||||
|
||||
if (err) {
|
||||
puts("err in dostuff()");
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user