notcurses_term_dimensions() + unit test

pull/7/head
nick black 5 years ago
parent 1fcae27627
commit c1f4219dc1
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -7,8 +7,12 @@ extern "C" {
const char* notcurses_version(void);
int notcurses_init(void);
int notcurses_stop(void);
struct notcurses;
struct notcurses* notcurses_init(void);
int notcurses_stop(struct notcurses* nc);
int notcurses_term_dimensions(struct notcurses* n, int* rows, int* cols);
#ifdef __cplusplus
} // extern "C"

@ -1,11 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <notcurses.h>
int main(void){
if(notcurses_init()){
struct notcurses* nc;
int x, y;
if((nc = notcurses_init()) == NULL){
return EXIT_FAILURE;
}
if(notcurses_stop()){
if(notcurses_term_dimensions(nc, &y, &x)){
notcurses_stop(nc);
return EXIT_FAILURE;
}
printf("Dimensions: %d rows x %d cols\n", y, x);
if(notcurses_stop(nc)){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;

@ -1,6 +1,16 @@
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include "notcurses.h"
#include "version.h"
typedef struct notcurses {
int ttyfd; // file descriptor for controlling tty
} notcurses;
static const char NOTCURSES_VERSION[] =
notcurses_VERSION_MAJOR "."
notcurses_VERSION_MINOR "."
@ -10,12 +20,35 @@ const char* notcurses_version(void){
return NOTCURSES_VERSION;
}
int notcurses_init(void){
int ret = 0;
int notcurses_term_dimensions(notcurses* n, int* rows, int* cols){
struct winsize ws;
int i = ioctl(n->ttyfd, TIOCGWINSZ, &ws);
if(i < 0){
fprintf(stderr, "TIOCGWINSZ failed on %d (%s)\n", n->ttyfd, strerror(errno));
return -1;
}
*rows = ws.ws_row;
*cols = ws.ws_col;
return 0;
}
notcurses* notcurses_init(void){
notcurses* ret = malloc(sizeof(*ret));
if(ret == NULL){
return ret;
}
return ret;
}
int notcurses_stop(void){
int notcurses_stop(notcurses* nc){
int ret = 0;
if(nc){
if(close(nc->ttyfd)){
fprintf(stderr, "Error closing TTY file descriptor %d (%s)\n",
nc->ttyfd, strerror(errno));
ret = -1;
}
free(nc);
}
return ret;
}

@ -1,7 +1,28 @@
#include <string>
#include <cstdlib>
#include <notcurses.h>
#include "main.h"
TEST(Notcurses, BasicLifetime) {
ASSERT_EQ(0, notcurses_init());
EXPECT_EQ(0, notcurses_stop());
struct notcurses* nc = notcurses_init();
ASSERT_NE(nullptr, nc);
EXPECT_EQ(0, notcurses_stop(nc));
}
TEST(Notcurses, TermDimensions) {
struct notcurses* nc = notcurses_init();
int x, y;
ASSERT_NE(nullptr, nc);
EXPECT_EQ(0, notcurses_term_dimensions(nc, &y, &x));
auto stry = getenv("LINES");
if(stry){
auto envy = std::stoi(stry, nullptr);
EXPECT_EQ(envy, y);
}
auto strx = getenv("COLUMNS");
if(stry){
auto envx = std::stoi(strx, nullptr);
EXPECT_EQ(envx, x);
}
EXPECT_EQ(0, notcurses_stop(nc));
}

Loading…
Cancel
Save