Skip to content

Commit 400a49b

Browse files
committed
Add window.chgat() method, submitted via e-mail by Fabian Kreutz
1 parent 781aef2 commit 400a49b

5 files changed

Lines changed: 84 additions & 4 deletions

File tree

Doc/lib/libcurses.tex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,16 @@ \subsection{Window Objects \label{curses-window-objects}}
646646
corner characters are always used by this function.
647647
\end{methoddesc}
648648

649+
\begin{methoddesc}[window]{chgat}{\optional{y, x, } \optional{num,} attr}
650+
Sets the attributes of \var{num} characters at the current cursor
651+
position, or at position \code{(\var{y}, \var{x})} if supplied. If no
652+
value of \var{num} is given or \var{num} = -1, the attribute will
653+
be set on all the characters to the end of the line.
654+
This function does not move the cursor. The changed line
655+
will be touched using the \method{touchline} method so that the
656+
contents will be redisplayed by the next window refresh.
657+
\end{methoddesc}
658+
649659
\begin{methoddesc}[window]{clear}{}
650660
Like \method{erase()}, but also causes the whole window to be repainted
651661
upon next call to \method{refresh()}.

Doc/whatsnew/whatsnew26.tex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ \section{New, Improved, and Deprecated Modules}
135135

136136
(Contributed by Raymond Hettinger.)
137137

138+
\item New method in the \module{curses} module:
139+
for a window, \method{chgat()} changes the display characters for a
140+
certain number of characters on a single line.
141+
142+
\begin{verbatim}
143+
# Boldface text starting at y=0,x=21
144+
# and affecting the rest of the line.
145+
stdscr.chgat(0,21, curses.A_BOLD)
146+
\end{verbatim}
147+
148+
(Contributed by Fabian Kreutz.)
149+
138150
\item New function in the \module{heapq} module:
139151
\function{merge(iter1, iter2, ...)}
140152
takes any number of iterables that return data

Lib/test/test_curses.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ def window_funcs(stdscr):
129129
stdscr.touchline(5,5,0)
130130
stdscr.vline('a', 3)
131131
stdscr.vline('a', 3, curses.A_STANDOUT)
132+
stdscr.chgat(5, 2, 3, curses.A_BLINK)
133+
stdscr.chgat(3, curses.A_BOLD)
134+
stdscr.chgat(5, 8, curses.A_UNDERLINE)
135+
stdscr.chgat(curses.A_BLINK)
136+
stdscr.refresh()
137+
132138
stdscr.vline(1,1, 'a', 3)
133139
stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
134140

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ Damon Kohler
367367
Joseph Koshy
368368
Bob Kras
369369
Holger Krekel
370+
Fabian Kreutz
370371
Hannu Krosing
371372
Andrew Kuchling
372373
Vladimir Kushnir

Modules/_cursesmodule.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ A number of SysV or ncurses functions don't have wrappers yet; if you need
3939
a given function, add it and send a patch. Here's a list of currently
4040
unsupported functions:
4141
42-
addchnstr addchstr chgat color_set define_key
42+
addchnstr addchstr color_set define_key
4343
del_curterm delscreen dupwin inchnstr inchstr innstr keyok
44-
mcprint mvaddchnstr mvaddchstr mvchgat mvcur mvinchnstr
45-
mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr mvwchgat
44+
mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
45+
mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
4646
mvwinchnstr mvwinchstr mvwinnstr newterm
4747
restartterm ripoffline scr_dump
4848
scr_init scr_restore scr_set scrl set_curterm set_term setterm
4949
tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
50-
vidattr vidputs waddchnstr waddchstr wchgat
50+
vidattr vidputs waddchnstr waddchstr
5151
wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
5252
5353
Low-priority:
@@ -620,6 +620,56 @@ int py_mvwdelch(WINDOW *w, int y, int x)
620620
}
621621
#endif
622622

623+
/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
624+
625+
static PyObject *
626+
PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
627+
{
628+
int rtn;
629+
int x, y;
630+
int num = -1;
631+
short color;
632+
attr_t attr = A_NORMAL;
633+
int use_xy = FALSE;
634+
635+
switch (PyTuple_Size(args)) {
636+
case 1:
637+
if (!PyArg_ParseTuple(args,"l;attr", &attr))
638+
return NULL;
639+
break;
640+
case 2:
641+
if (!PyArg_ParseTuple(args,"il;n,attr", &num, &attr))
642+
return NULL;
643+
break;
644+
case 3:
645+
if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &attr))
646+
return NULL;
647+
use_xy = TRUE;
648+
break;
649+
case 4:
650+
if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &attr))
651+
return NULL;
652+
use_xy = TRUE;
653+
break;
654+
default:
655+
PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
656+
return NULL;
657+
}
658+
659+
color = (short)((attr >> 8) & 0xff);
660+
attr = attr - (color << 8);
661+
662+
if (use_xy == TRUE) {
663+
rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
664+
touchline(self->win,y,1);
665+
} else {
666+
getyx(self->win,y,x);
667+
rtn = wchgat(self->win,num,attr,color,NULL);
668+
touchline(self->win,y,1);
669+
}
670+
return PyCursesCheckERR(rtn, "chgat");
671+
}
672+
623673

624674
static PyObject *
625675
PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
@@ -1428,6 +1478,7 @@ static PyMethodDef PyCursesWindow_Methods[] = {
14281478
{"attron", (PyCFunction)PyCursesWindow_wattron, METH_VARARGS},
14291479
{"attrset", (PyCFunction)PyCursesWindow_wattrset, METH_VARARGS},
14301480
{"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
1481+
{"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
14311482
{"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
14321483
{"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
14331484
{"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},

0 commit comments

Comments
 (0)