[py] Basic box() function.

pull/2625/head
Alex Samuel 2 years ago
parent bec97315ad
commit 776680f5af

@ -35,6 +35,8 @@ from .notcurses import (
ncchannels_set_fg_rgb, ncchannels_set_fg_rgb8,
ncchannels_set_fg_rgb8_clipped, ncstrwidth, notcurses_version,
notcurses_version_components,
NCBOXASCII, NCBOXDOUBLE, NCBOXHEAVY, NCBOXLIGHT, NCBOXOUTER, NCBOXROUND,
box,
)
__all__ = (
@ -61,4 +63,6 @@ __all__ = (
'ncchannels_set_fg_rgb8_clipped',
'ncstrwidth', 'notcurses_version', 'notcurses_version_components',
'box',
)

@ -0,0 +1,90 @@
#include <Python.h>
#include <stdint.h>
#include "notcurses-python.h"
// TODO: function to construct channels: channel(None | pindex | color, alpha=0)
// TODO: split channels into two args
// TODO: perimeter version
// TODO: rationalize coordinate / size args
// TODO: provide a way to set channels for each corne
// TODO: docstring
// TODO: test
static PyObject*
pync_meth_box(PyObject* Py_UNUSED(self), PyObject* args, PyObject* kwargs) {
static char* keywords[] = {
"plane", "ystop", "xstop", "y", "x", "box_chars", "styles", "channels",
"ctlword", NULL
};
NcPlaneObject* plane_arg;
unsigned ystop;
unsigned xstop;
int y = -1;
int x = -1;
const char* box_chars = NCBOXASCII;
uint16_t styles = 0;
uint64_t channels = 0;
unsigned ctlword = 0;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "O!II|iis$HKI:box", keywords,
&NcPlane_Type, &plane_arg,
&ystop, &xstop, &y, &x, &box_chars, &styles, &channels, &ctlword))
return NULL;
if (!notcurses_canutf8(ncplane_notcurses(plane)))
// No UTF-8 support; force ASCII.
box_chars = NCBOXASCII;
struct ncplane* const plane = plane_arg->ncplane_ptr;
int ret;
nccell ul = NCCELL_TRIVIAL_INITIALIZER;
nccell ur = NCCELL_TRIVIAL_INITIALIZER;
nccell ll = NCCELL_TRIVIAL_INITIALIZER;
nccell lr = NCCELL_TRIVIAL_INITIALIZER;
nccell hl = NCCELL_TRIVIAL_INITIALIZER;
nccell vl = NCCELL_TRIVIAL_INITIALIZER;
ret = nccells_load_box(
plane, styles, channels, &ul, &ur, &ll, &lr, &hl, &vl, box_chars);
if (ret == -1) {
PyErr_Format(PyExc_RuntimeError, "nccells_load_box returned %i", ret);
return NULL;
}
if (y != 1 || x != -1) {
ret = ncplane_cursor_move_yx(plane, y, x);
if (ret < 0) {
PyErr_Format(PyExc_RuntimeError, "ncplane_cursor_move_yx returned %i", ret);
goto done;
}
}
ret = ncplane_box(plane, &ul, &ur, &ll, &lr, &hl, &vl, ystop, xstop, ctlword);
if (ret < 0)
PyErr_Format(PyExc_RuntimeError, "nplane_box returned %i", ret);
done:
nccell_release(plane, &ul);
nccell_release(plane, &ur);
nccell_release(plane, &ll);
nccell_release(plane, &lr);
nccell_release(plane, &hl);
nccell_release(plane, &vl);
if (ret < 0)
return NULL;
else
Py_RETURN_NONE;
}
struct PyMethodDef pync_methods[] = {
{
"box",
(void*) pync_meth_box,
METH_VARARGS | METH_KEYWORDS,
"FIXME: Docs."
},
{NULL, NULL, 0, NULL}
};

@ -15,6 +15,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
#include <Python.h>
#include "notcurses-python.h"
PyObject *traceback_format_exception = NULL;
@ -27,12 +29,14 @@ Notcurses_module_free(PyObject *Py_UNUSED(self))
Py_XDECREF(new_line_unicode);
}
extern PyMethodDef pync_methods[];
static struct PyModuleDef NotcursesMiscModule = {
PyModuleDef_HEAD_INIT,
.m_name = "Notcurses",
.m_doc = "Notcurses python module",
.m_size = -1,
.m_methods = NULL,
.m_methods = pync_methods,
.m_free = (freefunc)Notcurses_module_free,
};
@ -86,6 +90,14 @@ PyInit_notcurses(void)
GNU_PY_CHECK_INT(PyModule_AddIntMacro(py_module, NCALPHA_BLEND));
GNU_PY_CHECK_INT(PyModule_AddIntMacro(py_module, NCALPHA_OPAQUE));
// FIXME: Better, attributes of an object such as an enum.
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXASCII));
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXDOUBLE));
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXHEAVY));
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXLIGHT));
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXOUTER));
GNU_PY_CHECK_INT(PyModule_AddStringMacro(py_module, NCBOXROUND));
// if this bit is set, we are *not* using the default background color
GNU_PY_CHECK_INT(PyModule_AddIntMacro(py_module, NC_BGDEFAULT_MASK));
// extract these bits to get the background RGB value

@ -46,6 +46,7 @@ setup(
sources=[
'notcurses/channels.c',
'notcurses/context.c',
'notcurses/functions.c',
'notcurses/main.c',
'notcurses/misc.c',
'notcurses/plane.c',

Loading…
Cancel
Save