tabbed janitorial work

clean up memory leak of tab names
ignore NCTYPE_RELEASE in tabbed PoC
constify strings in nctabbed_options
This commit is contained in:
nick black 2021-10-14 03:24:55 -04:00
parent b0b4c965a5
commit 99169aea42
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
5 changed files with 38 additions and 20 deletions

View File

@ -20,7 +20,7 @@ typedef struct nctabbed_options {
uint64_t selchan; // channel for the selected tab header
uint64_t hdrchan; // channel for unselected tab headers
uint64_t sepchan; // channel for the tab separator
char* separator; // separator string (copied by nctabbed_create())
const char* separator; // separator string (copied by nctabbed_create())
uint64_t flags; // bitmask of NCTABBED_OPTION_*
} nctabbed_options;

View File

@ -3646,7 +3646,7 @@ typedef struct nctabbed_options {
uint64_t selchan; // channel for the selected tab header
uint64_t hdrchan; // channel for unselected tab headers
uint64_t sepchan; // channel for the tab separator
char* separator; // separator string (copied by nctabbed_create())
const char* separator; // separator string (copied by nctabbed_create())
uint64_t flags; // bitmask of NCTABBED_OPTION_*
} nctabbed_options;
@ -3982,17 +3982,17 @@ typedef struct ncsubproc_options {
// see exec(2). p-types use $PATH. e-type passes environment vars.
API ALLOC struct ncsubproc* ncsubproc_createv(struct ncplane* n, const ncsubproc_options* opts,
const char* bin, char* const arg[],
const char* bin, char* const arg[],
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
__attribute__ ((nonnull (1)));
API ALLOC struct ncsubproc* ncsubproc_createvp(struct ncplane* n, const ncsubproc_options* opts,
const char* bin, char* const arg[],
const char* bin, char* const arg[],
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
__attribute__ ((nonnull (1)));
API ALLOC struct ncsubproc* ncsubproc_createvpe(struct ncplane* n, const ncsubproc_options* opts,
const char* bin, char* const arg[], char* const env[],
const char* bin, char* const arg[], char* const env[],
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
__attribute__ ((nonnull (1)));

View File

@ -230,18 +230,6 @@ typedef struct nctab {
struct nctab* next;
} nctab;
typedef struct nctabbed {
ncplane* ncp; // widget ncplane
ncplane* p; // tab content ncplane
ncplane* hp; // tab headers ncplane
// a doubly-linked circular list of tabs
nctab* leftmost; // the tab most to the left
nctab* selected; // the currently selected tab
int tabcount; // tab separator (can be NULL)
int sepcols; // separator with in columns
nctabbed_options opts; // copied in nctabbed_create()
} nctabbed;
// various moving parts within a notcurses context (and the user) might need to
// access the stats object, so throw a lock on it. we don't want the lock in
// the actual structure since (a) it's usually unnecessary and (b) it breaks

View File

@ -1,5 +1,25 @@
#include "internal.h"
typedef struct nctabbed_opsint {
uint64_t selchan; // channel for the selected tab header
uint64_t hdrchan; // channel for unselected tab headers
uint64_t sepchan; // channel for the tab separator
char* separator; // separator string (copied by nctabbed_create())
uint64_t flags; // bitmask of NCTABBED_OPTION_*
} nctabbed_opsint;
typedef struct nctabbed {
ncplane* ncp; // widget ncplane
ncplane* p; // tab content ncplane
ncplane* hp; // tab headers ncplane
// a doubly-linked circular list of tabs
nctab* leftmost; // the tab most to the left
nctab* selected; // the currently selected tab
int tabcount; // tab separator (can be NULL)
int sepcols; // separator with in columns
nctabbed_opsint opts; // copied in nctabbed_create()
} nctabbed;
void nctabbed_redraw(nctabbed* nt){
nctab* t;
int drawn_cols = 0;
@ -144,8 +164,12 @@ nctabbed* nctabbed_create(ncplane* n, const nctabbed_options* topts){
nt->leftmost = nt->selected = NULL;
nt->tabcount = 0;
memcpy(&nt->opts, topts, sizeof(*topts));
if(nt->opts.separator){
if((nt->opts.separator = strdup(nt->opts.separator)) == NULL){
nt->opts.selchan = topts->selchan;
nt->opts.hdrchan = topts->hdrchan;
nt->opts.sepchan = topts->sepchan;
nt->opts.flags = topts->flags;
if(topts->separator){
if((nt->opts.separator = strdup(topts->separator)) == NULL){
logerror("Couldn't allocate nctabbed separator");
free(nt);
return NULL;
@ -157,6 +181,7 @@ nctabbed* nctabbed_create(ncplane* n, const nctabbed_options* topts){
return NULL;
}
}else{
nt->opts.separator = NULL;
nt->sepcols = 0;
}
ncplane_dim_yx(n, &nrows, &ncols);
@ -267,6 +292,7 @@ int nctabbed_del(nctabbed* nt, nctab* t){
t->next->prev = t->prev;
t->prev->next = t->next;
}
free(t->name);
free(t);
--nt->tabcount;
return 0;

View File

@ -102,7 +102,11 @@ int main(int argc, char** argv){
}
int tabnameind = 0;
uint32_t c;
while((c = notcurses_getc_blocking(nc, NULL)) != 'q'){
ncinput ni;
while((c = notcurses_getc_blocking(nc, &ni)) != 'q'){
if(ni.evtype == NCTYPE_RELEASE){
continue;
}
switch(c){
case NCKEY_RIGHT:
nctabbed_next(nct);