ncdirect_printf_aligned() added #750

This commit is contained in:
nick black 2020-07-01 00:45:38 -04:00 committed by Nick Black
parent 4889e9b391
commit 33cb21d917
4 changed files with 55 additions and 23 deletions

View File

@ -267,23 +267,14 @@ drawpalette(struct ncdirect* nc){
return 0;
}
/*static int
static int
infoplane(struct ncdirect* nc, const fetched_info* fi){
// FIXME look for an area without background logo in it. pick the one
// closest to the center horizontally, and lowest vertically. if none
// can be found, just center it on the bottom as we do now
const int dimy = ncdirect_dim_y(nc);
const int planeheight = 8;
const int planewidth = 60;
struct ncplane* infop = ncplane_aligned(notcurses_stdplane(nc),
planeheight, planewidth,
dimy - (planeheight + 1),
NCALIGN_CENTER, NULL);
if(infop == NULL){
return -1;
}
ncplane_set_fg_rgb(infop, 0xd0, 0xd0, 0xd0);
ncplane_set_attr(infop, NCSTYLE_UNDERLINE);
ncdirect_fg_rgb(nc, 0xd0, 0xd0, 0xd0);
ncdirect_bg_rgb(nc, 0x50, 0x50, 0x50);
ncdirect_styles_on(nc, NCSTYLE_UNDERLINE);
/*
ncplane_printf_aligned(infop, 1, NCALIGN_LEFT, " %s %s", fi->kernel, fi->kernver);
if(fi->distro_pretty){
ncplane_printf_aligned(infop, 1, NCALIGN_RIGHT, "%s ", fi->distro_pretty);
@ -349,11 +340,9 @@ infoplane(struct ncdirect* nc, const fetched_info* fi){
fi->username, fi->hostname) < 0){
return -1;
}
channels_set_fg_rgb(&channels, 0, 0, 0);
channels_set_bg_rgb(&channels, 0x50, 0x50, 0x50);
ncplane_set_base(infop, " ", 0, channels);
*/
return 0;
}*/
}
struct marshal {
struct ncdirect* nc;

View File

@ -359,3 +359,44 @@ int ncdirect_fg_palindex(ncdirect* nc, int pidx){
int ncdirect_bg_palindex(ncdirect* nc, int pidx){
return term_emit("setab", tiparm(nc->tcache.setab, pidx), nc->ttyfp, false);
}
static inline int
ncdirect_align(const struct ncdirect* n, ncalign_e align, int c){
if(align == NCALIGN_LEFT){
return 0;
}
int cols = ncdirect_dim_x(n);
if(c > cols){
return 0;
}
if(align == NCALIGN_CENTER){
return (cols - c) / 2;
}else if(align == NCALIGN_RIGHT){
return cols - c;
}
return INT_MAX;
}
int ncdirect_vprintf_aligned(ncdirect* n, int y, ncalign_e align, const char* fmt, va_list ap){
char* r = ncplane_vprintf_prep(fmt, ap);
if(r == NULL){
return -1;
}
const size_t len = strlen(r);
const int x = ncdirect_align(n, align, len);
if(ncdirect_cursor_move_yx(n, y, x)){
free(r);
return -1;
}
int ret = vprintf(fmt, ap);
free(r);
return ret;
}
int ncdirect_printf_aligned(ncdirect* n, int y, ncalign_e align, const char* fmt, ...){
va_list va;
va_start(va, fmt);
int ret = ncdirect_vprintf_aligned(n, y, align, fmt, va);
va_end(va);
return ret;
}

View File

@ -769,6 +769,9 @@ ncplane* ncplane_create(notcurses* nc, ncplane* n, int rows, int cols,
int yoff, int xoff, void* opaque);
void free_plane(ncplane* p);
// heap-allocated formatted output
char* ncplane_vprintf_prep(const char* format, va_list ap);
// Resize the provided ncviusal to the specified 'rows' x 'cols', but do not
// change the internals of the ncvisual. Uses oframe.
nc_err_e ncvisual_blit(struct ncvisual* ncv, int rows, int cols,

View File

@ -1404,9 +1404,8 @@ unsigned ncplane_styles(const ncplane* n){
// i hate the big allocation and two copies here, but eh what you gonna do?
// well, for one, we don't need the huge allocation FIXME
static char*
ncplane_vprintf_prep(ncplane* n, const char* format, va_list ap){
const size_t size = n->lenx + 1; // healthy estimate, can embiggen below
char* ncplane_vprintf_prep(const char* format, va_list ap){
const size_t size = BUFSIZ; // healthy estimate, can embiggen below
char* buf = malloc(size);
if(buf == NULL){
return NULL;
@ -1434,7 +1433,7 @@ ncplane_vprintf_prep(ncplane* n, const char* format, va_list ap){
}
int ncplane_vprintf_yx(ncplane* n, int y, int x, const char* format, va_list ap){
char* r = ncplane_vprintf_prep(n, format, ap);
char* r = ncplane_vprintf_prep(format, ap);
if(r == NULL){
return -1;
}
@ -1445,7 +1444,7 @@ int ncplane_vprintf_yx(ncplane* n, int y, int x, const char* format, va_list ap)
int ncplane_vprintf_aligned(ncplane* n, int y, ncalign_e align,
const char* format, va_list ap){
char* r = ncplane_vprintf_prep(n, format, ap);
char* r = ncplane_vprintf_prep(format, ap);
if(r == NULL){
return -1;
}