progbar core / poc #1202
parent
6bff720122
commit
1d74979718
@ -0,0 +1,47 @@
|
||||
#include <string.h>
|
||||
#include "internal.h"
|
||||
|
||||
ncprogbar* ncprogbar_create(ncplane* n, const ncprogbar_options* opts){
|
||||
ncprogbar_options default_opts;
|
||||
if(opts == NULL){
|
||||
memset(&default_opts, 0, sizeof(default_opts));
|
||||
opts = &default_opts;
|
||||
// FIXME need sensible default channels
|
||||
}
|
||||
if(opts->flags > (NCPROGBAR_OPTION_FORCE_VERTICAL << 1u)){
|
||||
logwarn(ncplane_notcurses(n), "Invalid flags %016lx\n", opts->flags);
|
||||
}
|
||||
ncprogbar* ret = malloc(sizeof(*ret));
|
||||
ret->ncp = n;
|
||||
ret->maxchannels = opts->maxchannels;
|
||||
ret->minchannels = opts->minchannels;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ncplane* ncprogbar_plane(ncprogbar* n){
|
||||
return n->ncp;
|
||||
}
|
||||
|
||||
static int
|
||||
progbar_redraw(ncprogbar* n){
|
||||
// FIXME
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ncprogbar_set_progress(ncprogbar* n, double p){
|
||||
if(p < 0 || p > 1){
|
||||
logerror(ncplane_notcurses(ncprogbar_plane(n)), "Invalid progress %g\n", p);
|
||||
return -1;
|
||||
}
|
||||
n->progress = p;
|
||||
return progbar_redraw(n);
|
||||
}
|
||||
|
||||
double ncprogbar_progress(const ncprogbar* n){
|
||||
return n->progress;
|
||||
}
|
||||
|
||||
void ncprogbar_destroy(ncprogbar* n){
|
||||
ncplane_destroy(n->ncp);
|
||||
free(n);
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
#include <stdlib.h>
|
||||
#include <notcurses/notcurses.h>
|
||||
|
||||
static uint64_t
|
||||
ts_to_ns(const struct timespec* ts){
|
||||
return ts->tv_sec * 1000000000 + ts->tv_nsec;
|
||||
}
|
||||
|
||||
static const uint64_t delay = 10000000000ull;
|
||||
|
||||
static int
|
||||
pbar_fill(struct notcurses* nc, struct ncprogbar* pbar){
|
||||
struct timespec cur;
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur);
|
||||
const uint64_t startns = ts_to_ns(&cur);
|
||||
const uint64_t deadline = startns + delay;
|
||||
do{
|
||||
uint64_t curns = ts_to_ns(&cur);
|
||||
if(ncprogbar_set_progress(pbar, (curns - startns) / delay)){
|
||||
return -1;
|
||||
}
|
||||
notcurses_render(nc);
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur);
|
||||
}while(ts_to_ns(&cur) < deadline);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
struct notcurses* nc = notcurses_init(NULL, NULL);
|
||||
if(nc == NULL){
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
int dimy, dimx;
|
||||
struct ncplane* std = notcurses_stddim_yx(nc, &dimy, &dimx);
|
||||
struct ncplane_options nopts = {
|
||||
.y = dimy / 2,
|
||||
.x = NCALIGN_CENTER,
|
||||
.rows = 1,
|
||||
.cols = dimx - 20,
|
||||
.name = "pbar",
|
||||
.flags = NCPLANE_OPTION_HORALIGNED,
|
||||
};
|
||||
struct ncplane* pbar = ncplane_create(std, &nopts);
|
||||
if(pbar == NULL){
|
||||
notcurses_stop(nc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
struct ncprogbar* ncp = ncprogbar_create(pbar, NULL);
|
||||
if(ncp == NULL){
|
||||
notcurses_stop(nc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if(pbar_fill(nc, ncp)){
|
||||
notcurses_stop(nc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
ncprogbar_destroy(ncp);
|
||||
notcurses_stop(nc);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue