progbar: get vertical progbars working #1209

This commit is contained in:
nick black 2020-12-13 10:56:34 -05:00
parent cd3fd17174
commit ca547bc521
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
3 changed files with 68 additions and 10 deletions

View File

@ -29,8 +29,8 @@ pbar_make(struct notcurses* nc, int row){
int dimx, dimy;
struct ncplane* std = notcurses_stddim_yx(nc, &dimy, &dimx);
struct ncplane_options nopts = {
.y = row < 0 ? 2 : row,
.x = row < 0 ? 2 : NCALIGN_CENTER,
.y = row < 0 ? 4 : row,
.x = row < 0 ? 1 : NCALIGN_CENTER,
.rows = row < 0 ? dimy - 5 : 1,
.cols = row < 0 ? 1 : dimx - 20,
.name = "pbar",
@ -268,10 +268,10 @@ int unicodeblocks_demo(struct notcurses* nc){
if(ncplane_set_fg_rgb8(n, 0x40, 0xc0, 0x40)){
return -1;
}
if(ncplane_cursor_move_yx(n, 6 + BLOCKSIZE / CHUNKSIZE, 0)){
if(ncplane_cursor_move_yx(n, 6 + BLOCKSIZE / CHUNKSIZE, 3)){
return -1;
}
if(ncplane_printf(n, "%*.*s", maxx, maxx, "") <= 0){
if(ncplane_printf(n, "%*.*s", maxx - 6, maxx - 6, "") <= 0){
return -1;
}
if(ncplane_printf_aligned(n, 6 + BLOCKSIZE / CHUNKSIZE, NCALIGN_CENTER, "%s", description) <= 0){

View File

@ -27,23 +27,30 @@ progbar_redraw(ncprogbar* n){
int dimy, dimx;
ncplane_dim_yx(ncprogbar_plane(n), &dimy, &dimx);
const bool horizontal = dimx > dimy;
int delt, range;
int range, delt;
if(horizontal){
range = dimx;
delt = -1;
}else{
range = dimy;
delt = 1;
delt = -1;
}
ncplane_set_channels(ncprogbar_plane(n), n->channels);
double progress = n->progress * range;
if(n->retrograde){
progress = range - progress;
delt *= -1;
}
ncplane_set_channels(ncprogbar_plane(n), n->channels);
//fprintf(stderr, "progress: %g range: %d delt: %d\n", progress, range, delt);
while(progress > 0 && progress < range){
if(ncplane_putegc_yx(ncprogbar_plane(n), 0, progress, "", NULL) <= 0){
return -1;
if(horizontal){
if(ncplane_putegc_yx(ncprogbar_plane(n), 0, progress, "", NULL) <= 0){
return -1;
}
}else{
if(ncplane_putegc_yx(ncprogbar_plane(n), range - progress, 0, "", NULL) <= 0){
return -1;
}
}
progress += delt;
}
@ -51,6 +58,7 @@ progbar_redraw(ncprogbar* n){
}
int ncprogbar_set_progress(ncprogbar* n, double p){
//fprintf(stderr, "PROGRESS: %g\n", p);
if(p < 0 || p > 1){
logerror(ncplane_notcurses(ncprogbar_plane(n)), "Invalid progress %g\n", p);
return -1;

View File

@ -6,7 +6,7 @@ ts_to_ns(const struct timespec* ts){
return ts->tv_sec * 1000000000 + ts->tv_nsec;
}
static const uint64_t delay = 10000000000ull;
static const uint64_t delay = 2 * 1000000000ull;
static int
pbar_fill(struct notcurses* nc, struct ncprogbar* pbar){
@ -25,6 +25,43 @@ pbar_fill(struct notcurses* nc, struct ncprogbar* pbar){
return 0;
}
static struct ncprogbar*
hbar_make(struct notcurses* nc, uint64_t flags){
int dimy, dimx;
struct ncplane* std = notcurses_stddim_yx(nc, &dimy, &dimx);
struct ncplane_options nopts = {
.y = 1,
.x = NCALIGN_CENTER,
.rows = dimy - 4,
.cols = 1,
.name = "pbar",
.flags = NCPLANE_OPTION_HORALIGNED,
};
struct ncplane* pbar = ncplane_create(std, &nopts);
if(pbar == NULL){
return NULL;
}
int posy, posx, pdimy, pdimx;
ncplane_yx(pbar, &posy, &posx);
ncplane_dim_yx(pbar, &pdimy, &pdimx);
ncplane_cursor_move_yx(std, posy - 1, posx - 1);
uint64_t channels = 0;
channels_set_fg_rgb8(&channels, 0, 0xde, 0xde);
if(ncplane_rounded_box(std, 0, channels, posy + pdimy, posx + pdimx, 0)){
ncplane_destroy(pbar);
return NULL;
}
struct ncprogbar_options popts = {
.flags = flags,
};
channels_set_fg_rgb8(&popts.channels, 0x80, 0x22, 0x22);
struct ncprogbar* ncp = ncprogbar_create(pbar, &popts);
if(ncp == NULL){
return NULL;
}
return ncp;
}
static struct ncprogbar*
pbar_make(struct notcurses* nc, uint64_t flags){
int dimy, dimx;
@ -86,6 +123,19 @@ int main(void){
notcurses_stop(nc);
return EXIT_FAILURE;
}
ncprogbar_destroy(ncp);
ncp = hbar_make(nc, NCPROGBAR_OPTION_RETROGRADE);
if(pbar_fill(nc, ncp)){
notcurses_stop(nc);
return EXIT_FAILURE;
}
ncprogbar_destroy(ncp);
ncp = hbar_make(nc, 0);
if(pbar_fill(nc, ncp)){
notcurses_stop(nc);
return EXIT_FAILURE;
}
ncprogbar_destroy(ncp);
notcurses_stop(nc);
return EXIT_SUCCESS;
}