From 1821867e358c667fb49f2697d340e2f91a9d2620 Mon Sep 17 00:00:00 2001 From: nick black Date: Thu, 12 Dec 2019 07:59:48 -0500 Subject: [PATCH] move outfp out of notcurses_options #130 --- CMakeLists.txt | 2 +- README.md | 11 ++++------- include/notcurses.h | 11 ++++------- src/demo/demo.c | 3 +-- src/input/input.cpp | 3 +-- src/lib/notcurses.c | 6 +++--- src/view/view.cpp | 3 +-- tests/cell.cpp | 7 +++++-- tests/fade.cpp | 7 +++++-- tests/libav.cpp | 7 +++++-- tests/ncplane.cpp | 7 +++++-- tests/notcurses.cpp | 7 +++++-- tests/panelreel.cpp | 7 +++++-- 13 files changed, 45 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f64b08cfc..ee3761449 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,7 @@ foreach(f ${POCSRCS}) PRIVATE include "${TERMINFO_INCLUDE_DIR}" ) target_link_libraries(${fe} - PRIVATE "${TERMINFO_LIBRARIES}" + PRIVATE notcurses "${TERMINFO_LIBRARIES}" ) target_link_directories(${fe} PRIVATE "${TERMINFO_LIBRARY_DIRS}" diff --git a/README.md b/README.md index a2e440cdf..71612e59c 100644 --- a/README.md +++ b/README.md @@ -118,10 +118,6 @@ typedef struct notcurses_options { // the environment variable TERM is used. Failure to open the terminal // definition will result in failure to initialize notcurses. 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 // use of the "alternate screen". This flag inhibits use of smcup/rmcup. bool inhibit_alternate_screen; @@ -140,9 +136,10 @@ typedef struct notcurses_options { FILE* renderfp; } notcurses_options; -// Initialize a notcurses context, corresponding to a connected terminal. -// Returns NULL on error, including any failure to initialize terminfo. -struct notcurses* notcurses_init(const notcurses_options* opts); +// Initialize a notcurses context on the connected terminal at 'fp'. 'fp' must +// be a tty. You'll usually want stdout. Returns NULL on error, including any +// failure to initialize terminfo. +API struct notcurses* notcurses_init(const notcurses_options* opts, FILE* fp); // Destroy a notcurses context. int notcurses_stop(struct notcurses* nc); diff --git a/include/notcurses.h b/include/notcurses.h index fda6c92a5..722a9f0c9 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -102,10 +102,6 @@ typedef struct notcurses_options { // the environment variable TERM is used. Failure to open the terminal // definition will result in failure to initialize notcurses. 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 // use of the "alternate screen". This flag inhibits use of smcup/rmcup. bool inhibit_alternate_screen; @@ -124,9 +120,10 @@ typedef struct notcurses_options { FILE* renderfp; } notcurses_options; -// Initialize a notcurses context, corresponding to a connected terminal. -// Returns NULL on error, including any failure to initialize terminfo. -API struct notcurses* notcurses_init(const notcurses_options* opts); +// Initialize a notcurses context on the connected terminal at 'fp'. 'fp' must +// be a tty. You'll usually want stdout. Returns NULL on error, including any +// failure to initialize terminfo. +API struct notcurses* notcurses_init(const notcurses_options* opts, FILE* fp); // Destroy a notcurses context. API int notcurses_stop(struct notcurses* nc); diff --git a/src/demo/demo.c b/src/demo/demo.c index 163317e6e..4a2f508b0 100644 --- a/src/demo/demo.c +++ b/src/demo/demo.c @@ -157,7 +157,6 @@ static const char* handle_opts(int argc, char** argv, notcurses_options* opts){ int c; memset(opts, 0, sizeof(*opts)); - opts->outfp = stdout; while((c = getopt(argc, argv, "hkd:f:")) != EOF){ switch(c){ case 'h': @@ -210,7 +209,7 @@ int main(int argc, char** argv){ } demos = DEFAULT_DEMO; } - if((nc = notcurses_init(&nopts)) == NULL){ + if((nc = notcurses_init(&nopts, stdout)) == NULL){ return EXIT_FAILURE; } if((ncp = notcurses_stdplane(nc)) == NULL){ diff --git a/src/input/input.cpp b/src/input/input.cpp index f03efbd47..c51e8d1b7 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -56,8 +56,7 @@ int main(void){ return EXIT_FAILURE; } notcurses_options opts{}; - opts.outfp = stdout; - if((nc = notcurses_init(&opts)) == nullptr){ + if((nc = notcurses_init(&opts, stdout)) == nullptr){ return EXIT_FAILURE;; } struct ncplane* n = notcurses_stdplane(nc); diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 38fc85e0f..e62a9ee50 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -647,7 +647,7 @@ make_nonblocking(FILE* fp){ 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); if(encoding == NULL || strcmp(encoding, "UTF-8")){ 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)); ret->stats.render_min_ns = 1ul << 62u; ret->stats.render_min_bytes = 1ul << 62u; - ret->ttyfp = opts->outfp; + ret->ttyfp = outfp; ret->renderfp = opts->renderfp; ret->inputescapes = NULL; ret->ttyinfp = stdin; // FIXME @@ -678,7 +678,7 @@ notcurses* notcurses_init(const notcurses_options* opts){ ret->inputbuf_valid_starts = 0; ret->inputbuf_write_at = 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); return NULL; } diff --git a/src/view/view.cpp b/src/view/view.cpp index 5178c2266..426b016fd 100644 --- a/src/view/view.cpp +++ b/src/view/view.cpp @@ -51,8 +51,7 @@ int main(int argc, char** argv){ usage(std::cerr, argv[0], EXIT_FAILURE); } notcurses_options opts{}; - opts.outfp = stdout; - auto nc = notcurses_init(&opts); + auto nc = notcurses_init(&opts, stdout); if(nc == nullptr){ return EXIT_FAILURE; } diff --git a/tests/cell.cpp b/tests/cell.cpp index ac2ed7483..b07cc8d95 100644 --- a/tests/cell.cpp +++ b/tests/cell.cpp @@ -11,8 +11,9 @@ class CellTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); n_ = notcurses_stdplane(nc_); ASSERT_NE(nullptr, n_); @@ -22,10 +23,12 @@ class CellTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } struct notcurses* nc_{}; struct ncplane* n_{}; + FILE* outfp_{}; }; TEST_F(CellTest, SetItalic) { diff --git a/tests/fade.cpp b/tests/fade.cpp index 334078481..6510e1da0 100644 --- a/tests/fade.cpp +++ b/tests/fade.cpp @@ -11,8 +11,9 @@ class FadeTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); n_ = notcurses_stdplane(nc_); ASSERT_NE(nullptr, n_); @@ -40,10 +41,12 @@ class FadeTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } struct notcurses* nc_{}; struct ncplane* n_{}; + FILE* outfp_{}; }; TEST_F(FadeTest, FadeOut) { diff --git a/tests/libav.cpp b/tests/libav.cpp index 4998ec3b4..3858bca92 100644 --- a/tests/libav.cpp +++ b/tests/libav.cpp @@ -10,8 +10,9 @@ class LibavTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); ncp_ = notcurses_stdplane(nc_); ASSERT_NE(nullptr, ncp_); @@ -21,10 +22,12 @@ class LibavTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } notcurses* nc_{}; ncplane* ncp_{}; + FILE* outfp_{}; }; TEST_F(LibavTest, LoadImage) { diff --git a/tests/ncplane.cpp b/tests/ncplane.cpp index a904bad3b..2bc4d3557 100644 --- a/tests/ncplane.cpp +++ b/tests/ncplane.cpp @@ -11,8 +11,9 @@ class NcplaneTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); n_ = notcurses_stdplane(nc_); ASSERT_NE(nullptr, n_); @@ -23,10 +24,12 @@ class NcplaneTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } struct notcurses* nc_{}; struct ncplane* n_{}; + FILE* outfp_{}; }; // Starting position ought be 0, 0 (the origin) diff --git a/tests/notcurses.cpp b/tests/notcurses.cpp index 957820994..5f5c028e5 100644 --- a/tests/notcurses.cpp +++ b/tests/notcurses.cpp @@ -13,8 +13,9 @@ class NotcursesTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); } @@ -22,9 +23,11 @@ class NotcursesTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } struct notcurses* nc_{}; + FILE* outfp_{}; }; TEST_F(NotcursesTest, NotcursesVersionString) { diff --git a/tests/panelreel.cpp b/tests/panelreel.cpp index ef10dfee4..610ab7869 100644 --- a/tests/panelreel.cpp +++ b/tests/panelreel.cpp @@ -10,8 +10,9 @@ class PanelReelTest : public :: testing::Test { } notcurses_options nopts{}; nopts.inhibit_alternate_screen = true; - nopts.outfp = fopen("/dev/tty", "wb"); - nc_ = notcurses_init(&nopts); + outfp_ = fopen("/dev/tty", "wb"); + ASSERT_NE(nullptr, outfp_); + nc_ = notcurses_init(&nopts, outfp_); ASSERT_NE(nullptr, nc_); n_ = notcurses_stdplane(nc_); ASSERT_NE(nullptr, n_); @@ -22,10 +23,12 @@ class PanelReelTest : public :: testing::Test { if(nc_){ EXPECT_EQ(0, notcurses_stop(nc_)); } + fclose(outfp_); } struct notcurses* nc_{}; struct ncplane* n_{}; + FILE* outfp_{}; };