From d070abd96b266d9bf0324656d4a96001155bfb8a Mon Sep 17 00:00:00 2001 From: igo95862 Date: Sat, 3 Apr 2021 17:51:45 +0300 Subject: [PATCH] python: Added ncstrwidth function --- python/notcurses/__init__.py | 4 ++-- python/notcurses/misc.c | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/python/notcurses/__init__.py b/python/notcurses/__init__.py index d4371e6c6..4b1ae3a97 100644 --- a/python/notcurses/__init__.py +++ b/python/notcurses/__init__.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .misc import notcurses_version, notcurses_version_components +from .misc import ncstrwidth, notcurses_version, notcurses_version_components __all__ = ( - 'notcurses_version', 'notcurses_version_components' + 'ncstrwidth', 'notcurses_version', 'notcurses_version_components' ) diff --git a/python/notcurses/misc.c b/python/notcurses/misc.c index 68e87aad7..452bc7ffa 100644 --- a/python/notcurses/misc.c +++ b/python/notcurses/misc.c @@ -34,9 +34,20 @@ python_notcurses_version_components(PyObject *Py_UNUSED(self), PyObject *Py_UNUS return Py_BuildValue("iiii", major, minor, patch, tweak); } +static PyObject* +python_ncstrwidth(PyObject *Py_UNUSED(self), PyObject *args) +{ + const char* s = NULL; + + PY_CHECK_INT(PyArg_Parse(args, "s", &s)); + + return Py_BuildValue("i", ncstrwidth(s)); +} + static PyMethodDef NotcursesMiscMethods[] = { {"notcurses_version", (PyCFunction)python_notcurses_version, METH_NOARGS, "Get a human-readable string describing the running Notcurses version."}, {"notcurses_version_components", (PyCFunction)python_notcurses_version_components, METH_NOARGS, "Get a tuple of major, minor, patch, tweak integer of the running Notcurses version."}, + {"ncstrwidth", (PyCFunction)python_ncstrwidth, METH_VARARGS, "Returns the number of columns occupied by a string, or -1 if a non-printable/illegal character is encountered."}, {NULL, NULL, 0, NULL}, };