Skip to content

Commit

Permalink
Automatically call resize_term(0, 0) for get{ch,key,_wch}()
Browse files Browse the repository at this point in the history
Hack in the name of pragmatism. This makes resizing the Windows console
work like in ncurses, allowing resizing to work for Python code
developed on *nix without changes.

Calling resize_term(0, 0) twice is harmless, so PDCurses-adapted Python
code that already has it won't be affected.
ulfalizer committed Aug 5, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 0abaad4 commit 30ca08b
Showing 5 changed files with 88 additions and 4 deletions.
12 changes: 12 additions & 0 deletions py27/_cursesmodule.c
Original file line number Diff line number Diff line change
@@ -873,6 +873,12 @@ PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyInt_FromLong((long)rtn);
}

@@ -899,6 +905,12 @@ PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

if (rtn == ERR) {
/* getch() returns ERR in nodelay mode */
PyErr_CheckSignals();
20 changes: 19 additions & 1 deletion py35/_cursesmodule.c
Original file line number Diff line number Diff line change
@@ -1141,6 +1141,12 @@ PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong((long)rtn);
}

@@ -1167,6 +1173,12 @@ PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

if (rtn == ERR) {
/* getch() returns ERR in nodelay mode */
PyErr_CheckSignals();
@@ -1222,8 +1234,14 @@ PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyCursesError, "no input");
return NULL;
}
if (ct == KEY_CODE_YES)
if (ct == KEY_CODE_YES) {
// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong(rtn);
}
else
return PyUnicode_FromOrdinal(rtn);
}
20 changes: 19 additions & 1 deletion py36/_cursesmodule.c
Original file line number Diff line number Diff line change
@@ -1141,6 +1141,12 @@ PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong((long)rtn);
}

@@ -1167,6 +1173,12 @@ PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

if (rtn == ERR) {
/* getch() returns ERR in nodelay mode */
PyErr_CheckSignals();
@@ -1222,8 +1234,14 @@ PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyCursesError, "no input");
return NULL;
}
if (ct == KEY_CODE_YES)
if (ct == KEY_CODE_YES) {
// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong(rtn);
}
else
return PyUnicode_FromOrdinal(rtn);
}
20 changes: 19 additions & 1 deletion py37/_cursesmodule.c
Original file line number Diff line number Diff line change
@@ -1142,6 +1142,12 @@ PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong((long)rtn);
}

@@ -1168,6 +1174,12 @@ PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
return NULL;
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

if (rtn == ERR) {
/* getch() returns ERR in nodelay mode */
PyErr_CheckSignals();
@@ -1223,8 +1235,14 @@ PyCursesWindow_Get_WCh(PyCursesWindowObject *self, PyObject *args)
PyErr_SetString(PyCursesError, "no input");
return NULL;
}
if (ct == KEY_CODE_YES)
if (ct == KEY_CODE_YES) {
// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong(rtn);
}
else
return PyUnicode_FromOrdinal(rtn);
}
20 changes: 19 additions & 1 deletion py38/_cursesmodule.c
Original file line number Diff line number Diff line change
@@ -1260,6 +1260,12 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
else {
rtn = mvwgetch(self->win, y, x);
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

Py_END_ALLOW_THREADS

return rtn;
@@ -1297,6 +1303,12 @@ _curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
else {
rtn = mvwgetch(self->win, y, x);
}

// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

Py_END_ALLOW_THREADS

if (rtn == ERR) {
@@ -1364,8 +1376,14 @@ _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
PyErr_SetString(PyCursesError, "no input");
return NULL;
}
if (ct == KEY_CODE_YES)
if (ct == KEY_CODE_YES) {
// windows-curses hack to make resizing work the same as in ncurses. See
// PDCurses' documentation for resize_term().
if (rtn == KEY_RESIZE)
resize_term(0, 0);

return PyLong_FromLong(rtn);
}
else
return PyUnicode_FromOrdinal(rtn);
}

0 comments on commit 30ca08b

Please sign in to comment.