move outfp out of notcurses_options #130

This commit is contained in:
nick black 2019-12-12 07:59:48 -05:00
parent 21c4a9a2eb
commit 1821867e35
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
13 changed files with 45 additions and 36 deletions

View File

@ -87,7 +87,7 @@ foreach(f ${POCSRCS})
PRIVATE include "${TERMINFO_INCLUDE_DIR}" PRIVATE include "${TERMINFO_INCLUDE_DIR}"
) )
target_link_libraries(${fe} target_link_libraries(${fe}
PRIVATE "${TERMINFO_LIBRARIES}" PRIVATE notcurses "${TERMINFO_LIBRARIES}"
) )
target_link_directories(${fe} target_link_directories(${fe}
PRIVATE "${TERMINFO_LIBRARY_DIRS}" PRIVATE "${TERMINFO_LIBRARY_DIRS}"

View File

@ -118,10 +118,6 @@ typedef struct notcurses_options {
// the environment variable TERM is used. Failure to open the terminal // the environment variable TERM is used. Failure to open the terminal
// definition will result in failure to initialize notcurses. // definition will result in failure to initialize notcurses.
const char* termtype; const char* termtype;
// An open FILE* for this terminal, on which we will generate output. If
// not attached to a sufficiently capable terminal, notcurses will refuse
// to start. You'll usually want stdout.
FILE* outfp;
// If smcup/rmcup capabilities are indicated, notcurses defaults to making // If smcup/rmcup capabilities are indicated, notcurses defaults to making
// use of the "alternate screen". This flag inhibits use of smcup/rmcup. // use of the "alternate screen". This flag inhibits use of smcup/rmcup.
bool inhibit_alternate_screen; bool inhibit_alternate_screen;
@ -140,9 +136,10 @@ typedef struct notcurses_options {
FILE* renderfp; FILE* renderfp;
} notcurses_options; } notcurses_options;
// Initialize a notcurses context, corresponding to a connected terminal. // Initialize a notcurses context on the connected terminal at 'fp'. 'fp' must
// Returns NULL on error, including any failure to initialize terminfo. // be a tty. You'll usually want stdout. Returns NULL on error, including any
struct notcurses* notcurses_init(const notcurses_options* opts); // failure to initialize terminfo.
API struct notcurses* notcurses_init(const notcurses_options* opts, FILE* fp);
// Destroy a notcurses context. // Destroy a notcurses context.
int notcurses_stop(struct notcurses* nc); int notcurses_stop(struct notcurses* nc);

View File

@ -102,10 +102,6 @@ typedef struct notcurses_options {
// the environment variable TERM is used. Failure to open the terminal // the environment variable TERM is used. Failure to open the terminal
// definition will result in failure to initialize notcurses. // definition will result in failure to initialize notcurses.
const char* termtype; const char* termtype;
// An open FILE* for this terminal, on which we will generate output. If
// not attached to a sufficiently capable terminal, notcurses will refuse
// to start. You'll usually want stdout.
FILE* outfp;
// If smcup/rmcup capabilities are indicated, notcurses defaults to making // If smcup/rmcup capabilities are indicated, notcurses defaults to making
// use of the "alternate screen". This flag inhibits use of smcup/rmcup. // use of the "alternate screen". This flag inhibits use of smcup/rmcup.
bool inhibit_alternate_screen; bool inhibit_alternate_screen;
@ -124,9 +120,10 @@ typedef struct notcurses_options {
FILE* renderfp; FILE* renderfp;
} notcurses_options; } notcurses_options;
// Initialize a notcurses context, corresponding to a connected terminal. // Initialize a notcurses context on the connected terminal at 'fp'. 'fp' must
// Returns NULL on error, including any failure to initialize terminfo. // be a tty. You'll usually want stdout. Returns NULL on error, including any
API struct notcurses* notcurses_init(const notcurses_options* opts); // failure to initialize terminfo.
API struct notcurses* notcurses_init(const notcurses_options* opts, FILE* fp);
// Destroy a notcurses context. // Destroy a notcurses context.
API int notcurses_stop(struct notcurses* nc); API int notcurses_stop(struct notcurses* nc);

View File

@ -157,7 +157,6 @@ static const char*
handle_opts(int argc, char** argv, notcurses_options* opts){ handle_opts(int argc, char** argv, notcurses_options* opts){
int c; int c;
memset(opts, 0, sizeof(*opts)); memset(opts, 0, sizeof(*opts));
opts->outfp = stdout;
while((c = getopt(argc, argv, "hkd:f:")) != EOF){ while((c = getopt(argc, argv, "hkd:f:")) != EOF){
switch(c){ switch(c){
case 'h': case 'h':
@ -210,7 +209,7 @@ int main(int argc, char** argv){
} }
demos = DEFAULT_DEMO; demos = DEFAULT_DEMO;
} }
if((nc = notcurses_init(&nopts)) == NULL){ if((nc = notcurses_init(&nopts, stdout)) == NULL){
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if((ncp = notcurses_stdplane(nc)) == NULL){ if((ncp = notcurses_stdplane(nc)) == NULL){

View File

@ -56,8 +56,7 @@ int main(void){
return EXIT_FAILURE; return EXIT_FAILURE;
} }
notcurses_options opts{}; notcurses_options opts{};
opts.outfp = stdout; if((nc = notcurses_init(&opts, stdout)) == nullptr){
if((nc = notcurses_init(&opts)) == nullptr){
return EXIT_FAILURE;; return EXIT_FAILURE;;
} }
struct ncplane* n = notcurses_stdplane(nc); struct ncplane* n = notcurses_stdplane(nc);

View File

@ -647,7 +647,7 @@ make_nonblocking(FILE* fp){
return fcntl(fd, F_SETFL, flags | O_NONBLOCK); return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
} }
notcurses* notcurses_init(const notcurses_options* opts){ notcurses* notcurses_init(const notcurses_options* opts, FILE* outfp){
const char* encoding = nl_langinfo(CODESET); const char* encoding = nl_langinfo(CODESET);
if(encoding == NULL || strcmp(encoding, "UTF-8")){ if(encoding == NULL || strcmp(encoding, "UTF-8")){
fprintf(stderr, "Encoding (\"%s\") wasn't UTF-8, refusing to start\n", fprintf(stderr, "Encoding (\"%s\") wasn't UTF-8, refusing to start\n",
@ -666,7 +666,7 @@ notcurses* notcurses_init(const notcurses_options* opts){
memset(&ret->stats, 0, sizeof(ret->stats)); memset(&ret->stats, 0, sizeof(ret->stats));
ret->stats.render_min_ns = 1ul << 62u; ret->stats.render_min_ns = 1ul << 62u;
ret->stats.render_min_bytes = 1ul << 62u; ret->stats.render_min_bytes = 1ul << 62u;
ret->ttyfp = opts->outfp; ret->ttyfp = outfp;
ret->renderfp = opts->renderfp; ret->renderfp = opts->renderfp;
ret->inputescapes = NULL; ret->inputescapes = NULL;
ret->ttyinfp = stdin; // FIXME ret->ttyinfp = stdin; // FIXME
@ -678,7 +678,7 @@ notcurses* notcurses_init(const notcurses_options* opts){
ret->inputbuf_valid_starts = 0; ret->inputbuf_valid_starts = 0;
ret->inputbuf_write_at = 0; ret->inputbuf_write_at = 0;
if((ret->ttyfd = fileno(ret->ttyfp)) < 0){ if((ret->ttyfd = fileno(ret->ttyfp)) < 0){
fprintf(stderr, "No file descriptor was available in opts->outfp\n"); fprintf(stderr, "No file descriptor was available in outfp %p\n", outfp);
free(ret); free(ret);
return NULL; return NULL;
} }

View File

@ -51,8 +51,7 @@ int main(int argc, char** argv){
usage(std::cerr, argv[0], EXIT_FAILURE); usage(std::cerr, argv[0], EXIT_FAILURE);
} }
notcurses_options opts{}; notcurses_options opts{};
opts.outfp = stdout; auto nc = notcurses_init(&opts, stdout);
auto nc = notcurses_init(&opts);
if(nc == nullptr){ if(nc == nullptr){
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -11,8 +11,9 @@ class CellTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
n_ = notcurses_stdplane(nc_); n_ = notcurses_stdplane(nc_);
ASSERT_NE(nullptr, n_); ASSERT_NE(nullptr, n_);
@ -22,10 +23,12 @@ class CellTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
struct notcurses* nc_{}; struct notcurses* nc_{};
struct ncplane* n_{}; struct ncplane* n_{};
FILE* outfp_{};
}; };
TEST_F(CellTest, SetItalic) { TEST_F(CellTest, SetItalic) {

View File

@ -11,8 +11,9 @@ class FadeTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
n_ = notcurses_stdplane(nc_); n_ = notcurses_stdplane(nc_);
ASSERT_NE(nullptr, n_); ASSERT_NE(nullptr, n_);
@ -40,10 +41,12 @@ class FadeTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
struct notcurses* nc_{}; struct notcurses* nc_{};
struct ncplane* n_{}; struct ncplane* n_{};
FILE* outfp_{};
}; };
TEST_F(FadeTest, FadeOut) { TEST_F(FadeTest, FadeOut) {

View File

@ -10,8 +10,9 @@ class LibavTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
ncp_ = notcurses_stdplane(nc_); ncp_ = notcurses_stdplane(nc_);
ASSERT_NE(nullptr, ncp_); ASSERT_NE(nullptr, ncp_);
@ -21,10 +22,12 @@ class LibavTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
notcurses* nc_{}; notcurses* nc_{};
ncplane* ncp_{}; ncplane* ncp_{};
FILE* outfp_{};
}; };
TEST_F(LibavTest, LoadImage) { TEST_F(LibavTest, LoadImage) {

View File

@ -11,8 +11,9 @@ class NcplaneTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
n_ = notcurses_stdplane(nc_); n_ = notcurses_stdplane(nc_);
ASSERT_NE(nullptr, n_); ASSERT_NE(nullptr, n_);
@ -23,10 +24,12 @@ class NcplaneTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
struct notcurses* nc_{}; struct notcurses* nc_{};
struct ncplane* n_{}; struct ncplane* n_{};
FILE* outfp_{};
}; };
// Starting position ought be 0, 0 (the origin) // Starting position ought be 0, 0 (the origin)

View File

@ -13,8 +13,9 @@ class NotcursesTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
} }
@ -22,9 +23,11 @@ class NotcursesTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
struct notcurses* nc_{}; struct notcurses* nc_{};
FILE* outfp_{};
}; };
TEST_F(NotcursesTest, NotcursesVersionString) { TEST_F(NotcursesTest, NotcursesVersionString) {

View File

@ -10,8 +10,9 @@ class PanelReelTest : public :: testing::Test {
} }
notcurses_options nopts{}; notcurses_options nopts{};
nopts.inhibit_alternate_screen = true; nopts.inhibit_alternate_screen = true;
nopts.outfp = fopen("/dev/tty", "wb"); outfp_ = fopen("/dev/tty", "wb");
nc_ = notcurses_init(&nopts); ASSERT_NE(nullptr, outfp_);
nc_ = notcurses_init(&nopts, outfp_);
ASSERT_NE(nullptr, nc_); ASSERT_NE(nullptr, nc_);
n_ = notcurses_stdplane(nc_); n_ = notcurses_stdplane(nc_);
ASSERT_NE(nullptr, n_); ASSERT_NE(nullptr, n_);
@ -22,10 +23,12 @@ class PanelReelTest : public :: testing::Test {
if(nc_){ if(nc_){
EXPECT_EQ(0, notcurses_stop(nc_)); EXPECT_EQ(0, notcurses_stop(nc_));
} }
fclose(outfp_);
} }
struct notcurses* nc_{}; struct notcurses* nc_{};
struct ncplane* n_{}; struct ncplane* n_{};
FILE* outfp_{};
}; };