notcurses/src/poc/selector.c

137 lines
4.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
#include <notcurses/notcurses.h>
#include "version.h"
static struct ncselector_item items[] = {
#define SITEM(short, long) { short, long, 0, 0, }
SITEM("first", "this is the first option"),
SITEM("2nd", "this is the second option"),
SITEM("3", "third, third, third option am i"),
SITEM("fourth", "i have another option here"),
SITEM("five", "golden rings"),
SITEM("666", "now it is time for me to REIGN IN BLOOD"),
SITEM("7seven7", "this monkey's gone to heaven"),
SITEM("8 8 8", "the chinese 平仮名平平仮名仮名love me, i'm told"),
SITEM("nine", "nine, nine, nine 'cause you left me"),
SITEM("ten", "stunning and brave"),
#undef SITEM
};
2020-02-01 19:01:20 +00:00
static void
run_selector(struct notcurses* nc, struct ncselector* ns){
static int item = 0;
++item;
if(ns == NULL){
notcurses_stop(nc);
fprintf(stderr, "Error creating selector %d\n", item);
exit(EXIT_FAILURE);
}
2020-02-01 19:01:20 +00:00
notcurses_render(nc);
char32_t keypress;
ncinput ni;
while((keypress = notcurses_getc_blocking(nc, &ni)) != (char32_t)-1){
if(!ncselector_offer_input(ns, &ni)){
switch(keypress){
case NCKEY_ENTER: ncselector_destroy(ns, NULL); return;
case 'M': case 'J': if(ni.ctrl){ ncselector_destroy(ns, NULL); return; }
}
if(keypress == 'q'){
break;
}
2020-02-01 19:01:20 +00:00
}
notcurses_render(nc);
}
ncselector_destroy(ns, NULL);
2020-02-01 19:01:20 +00:00
}
int main(void){
if(!setlocale(LC_ALL, "")){
return EXIT_FAILURE;
}
notcurses_options opts = {
.flags = NCOPTION_INHIBIT_SETLOCALE,
};
struct notcurses* nc = notcurses_init(&opts, NULL);
if(nc == NULL){
return EXIT_FAILURE;
}
2020-02-23 09:14:48 +00:00
if(notcurses_mouse_enable(nc)){
notcurses_stop(nc);
return EXIT_FAILURE;
}
ncselector_options sopts;
memset(&sopts, 0, sizeof(sopts));
sopts.maxdisplay = 4;
sopts.items = items;
sopts.itemcount = sizeof(items) / sizeof(*items);
2020-02-01 01:38:58 +00:00
sopts.title = "this is truly, absolutely an awfully long example of a selector title";
2020-01-31 03:16:03 +00:00
sopts.secondary = "pick one (you will die regardless)";
sopts.footer = "press q to exit (there is no exit)";
sopts.defidx = 5;
2020-01-31 03:16:03 +00:00
channels_set_fg(&sopts.boxchannels, 0x20e040);
channels_set_fg(&sopts.opchannels, 0xe08040);
channels_set_fg(&sopts.descchannels, 0x80e040);
2020-02-23 14:38:58 +00:00
channels_set_bg(&sopts.opchannels, 0);
channels_set_bg(&sopts.descchannels, 0);
2020-01-31 03:16:03 +00:00
channels_set_fg(&sopts.footchannels, 0xe00040);
channels_set_fg(&sopts.titlechannels, 0xffff80);
channels_set_fg(&sopts.bgchannels, 0x002000);
channels_set_bg(&sopts.bgchannels, 0x002000);
channels_set_fg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND);
channels_set_bg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND);
2020-02-23 14:38:58 +00:00
struct ncplane* n = notcurses_stdplane(nc);
if(notcurses_canopen_images(nc)){
2020-05-09 07:50:27 +00:00
nc_err_e err;
Fully general ncvisual layer (#647) This represents an essentially complete rewrite of ncvisual and associated code. It had two major goals: Improve the ncvisual API based off lessons learned, pursuant to the upcoming API freeze. In particular, I wanted to: decouple ncvisuals from ncplanes. It should be possible to render a ncvisual to multiple planes, with different scaling each time. It should be possible to create an ncvisual without a plane, etc. normalize the various ways of constructing an ncvisual -- file, memory, plane, etc. Support multiple blitters, from 7-bit ASCII to Sixel. This required writing the blitters in several cases, and they're not yet in their final implementations (but the API is fine) I have not yet unified Plots and Visuals, and might not, given that the Plot code works fine. We could at this point implement Plots in terms of Visuals, though -- the blitter backend range has been unified. Sixel is not yet implemented, though it is listed. There is a new POC tool, blitter. It renders its arguments using all possible blitter+scaling combinations. Another new POC, resize, displays its argument, then resizes it to the screen size and displays that, explicitly making use of ncvisual_resize() rather than a scaling parameter to ncvisual_render(). This also eliminates some memory leaks and bugs we were seeing in trunk, and brings in Sixel scaffolding. The C++ wrapper will also need patching back up; I cut most of it down while wrestling with this crap, urk. Closes #638, #562, and #622.
2020-05-29 01:16:58 +00:00
struct ncvisual* ncv = ncvisual_from_file("../data/changes.jpg", &err);
2020-05-09 07:50:27 +00:00
if(!ncv){
goto err;
}
Fully general ncvisual layer (#647) This represents an essentially complete rewrite of ncvisual and associated code. It had two major goals: Improve the ncvisual API based off lessons learned, pursuant to the upcoming API freeze. In particular, I wanted to: decouple ncvisuals from ncplanes. It should be possible to render a ncvisual to multiple planes, with different scaling each time. It should be possible to create an ncvisual without a plane, etc. normalize the various ways of constructing an ncvisual -- file, memory, plane, etc. Support multiple blitters, from 7-bit ASCII to Sixel. This required writing the blitters in several cases, and they're not yet in their final implementations (but the API is fine) I have not yet unified Plots and Visuals, and might not, given that the Plot code works fine. We could at this point implement Plots in terms of Visuals, though -- the blitter backend range has been unified. Sixel is not yet implemented, though it is listed. There is a new POC tool, blitter. It renders its arguments using all possible blitter+scaling combinations. Another new POC, resize, displays its argument, then resizes it to the screen size and displays that, explicitly making use of ncvisual_resize() rather than a scaling parameter to ncvisual_render(). This also eliminates some memory leaks and bugs we were seeing in trunk, and brings in Sixel scaffolding. The C++ wrapper will also need patching back up; I cut most of it down while wrestling with this crap, urk. Closes #638, #562, and #622.
2020-05-29 01:16:58 +00:00
struct ncvisual_options vopts = {
.scaling = NCSCALE_STRETCH,
.n = n,
};
if(ncvisual_render(nc, ncv, &vopts) == NULL){
2020-05-09 07:50:27 +00:00
goto err;
}
2020-02-23 14:38:58 +00:00
}
ncplane_set_fg(n, 0x40f040);
ncplane_putstr_aligned(n, 0, NCALIGN_RIGHT, "selector widget demo");
struct ncselector* ns = ncselector_create(n, 3, 0, &sopts);
2020-02-01 19:01:20 +00:00
run_selector(nc, ns);
2020-02-01 19:01:20 +00:00
sopts.title = "short round title";
ns = ncselector_create(n, 3, 0, &sopts);
2020-02-01 19:01:20 +00:00
run_selector(nc, ns);
sopts.title = "short round title";
sopts.secondary = "now this secondary is also very, very, very outlandishly long, you see";
ns = ncselector_create(n, 3, 0, &sopts);
run_selector(nc, ns);
sopts.title = "the whole world is watching";
sopts.secondary = NULL;
sopts.footer = "now this FOOTERFOOTER is also very, very, very outlandishly long, you see";
ns = ncselector_create(n, 3, 0, &sopts);
run_selector(nc, ns);
sopts.title = "chomps";
sopts.secondary = NULL;
sopts.footer = NULL;
ns = ncselector_create(n, 3, 0, &sopts);
run_selector(nc, ns);
if(notcurses_stop(nc)){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
2020-02-23 14:38:58 +00:00
err:
notcurses_stop(nc);
return EXIT_FAILURE;
}