curses and use_default_colors()

Discussion in 'Python' started by Brian Victor, Jul 29, 2003.

  1. Brian Victor

    Brian Victor Guest

    I am attempting to write a curses-based program. I would like to use
    the default terminal background rather than a black one (a significant
    difference with transluscent terminals, regardless of one's opinion of
    them).

    It appears that in the C ncurses interface, this is accomplished via
    use_default_colors() and assume_default_colors(). However, these
    functions don't seem to have parallels in the python binding. Is there
    some way to accomplish what I want?

    Thanks,

    --
    Brian
    Brian Victor, Jul 29, 2003
    #1
    1. Advertising

  2. Brian Victor

    Chris Reay Guest

    Brian Victor <> wrote in message news:<>...
    > I am attempting to write a curses-based program. I would like to use
    > the default terminal background rather than a black one (a significant
    > difference with transluscent terminals, regardless of one's opinion of
    > them).

    <snip>

    I don't know how much this'll help, but my home-brewed curses TextApp
    class has a white-on-blue default, and TextApp.startWin() contains
    these lines (inter alia) ...

    # ... blah, blah, blah.
    curses.start_color()
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
    # ... blah, blah, blah ...
    self.scrn = self.stdscr.subwin(self.height, self.width, 0, 0)
    self.scrn.bkgd(curses.color_pair(1))
    # Blah, blah, blah ...

    Fwiw

    Chris
    Chris Reay, Jul 30, 2003
    #2
    1. Advertising

  3. Brian Victor

    Brian Victor Guest

    Chris Reay wrote:
    > Brian Victor <> wrote in message
    > news:<>...
    >> I am attempting to write a curses-based program. I would like to use
    >> the default terminal background rather than a black one (a significant
    >> difference with transluscent terminals, regardless of one's opinion of
    >> them).

    > I don't know how much this'll help, but my home-brewed curses TextApp
    > class has a white-on-blue default, and TextApp.startWin() contains
    > these lines (inter alia) ...

    [snip]

    Thanks, but that's not quite what I'm looking for. Getting a solid
    background isn't a problem. Getting a subtly textured background to
    show through (or other windows on Mac) seems to be a bit tricker.

    Still hoping someone will hop in with "just use this curses extension"
    or "you can write a wrapper around that one function and integrate it
    with python's curses like this." But thanks for the input, anyway!

    --
    Brian
    Brian Victor, Jul 30, 2003
    #3
  4. Brian Victor

    Brian Victor Guest

    Brian Victor wrote:
    > Still hoping someone will hop in with "just use this curses extension"
    > or "you can write a wrapper around that one function and integrate it
    > with python's curses like this." But thanks for the input, anyway!


    I guess that someone is me. I love replying to my own posts...

    I just hacked this bit of code up and it seems to do the trick.
    importing usedefault and calling usedefault.use_default_colors() sets
    color pair 0 to use the default colors for the terminal. This allows
    translucency on terminals that support it. The packaging could use some
    work, but the following should be enough to get future readers going.

    This is the first C extension I've written, and it's almost straight
    from the docs. If anyone has any pointers, I'd be happy to hear them.

    usedefault.c
    #v+
    #include <Python.h>
    #include <ncurses.h>

    static PyObject *
    pyuse_default_colors(PyObject *self, PyObject *args)
    {
    use_default_colors();
    Py_INCREF(Py_None);
    return Py_None;
    }

    static PyMethodDef SpamMethods[] = {
    {"use_default_colors", pyuse_default_colors, METH_VARARGS,
    "Use Default Colors."},
    {NULL, NULL, 0, NULL} /* Sentinel */
    };

    /* PyMODINIT_FUNC */
    /* The docs say to use the above, but it doesn't exist in my copy of
    * Python.h. */
    void initusedefault()
    {
    (void) Py_InitModule("usedefault", SpamMethods);
    }
    #v-

    setup.py
    #v+
    from distutils.core import setup, Extension

    module1 = Extension('usedefault',
    libraries = ["ncurses"],
    sources = ['usedefault.c'])

    setup (name = 'usedefault',
    version = '1.0',
    description = 'Use default colors in curses',
    ext_modules = [module1])
    #v-

    --
    Brian
    Brian Victor, Jul 30, 2003
    #4
  5. On Wed, 30 Jul 2003 17:34:29 GMT,
    Brian Victor <> wrote:
    > static PyObject *
    > pyuse_default_colors(PyObject *self, PyObject *args)
    > {
    > use_default_colors();
    > Py_INCREF(Py_None);
    > return Py_None;
    > }
    >
    > static PyMethodDef SpamMethods[] = {
    > {"use_default_colors", pyuse_default_colors, METH_VARARGS,
    > "Use Default Colors."},
    > {NULL, NULL, 0, NULL} /* Sentinel */


    Use METH_NOARGS instead of METH_VARARGS; as written, this code will accept
    any number of arguments to the Python use_default_colors() method and not
    raise any error. An alternative would be to leave METH_VARARGS and put "if
    (!PyArg_NoArgs(args)) return NULL;" in pyuse_default_colors(); this would
    work with older versions of Python at the cost of being slightly slower.

    I'll add this function to the curses module, so at least it'll be in 2.4;
    thanks for writing it. Anyone know if it's possible to test it on MacOS X?
    Does the Terminal support transparency?

    --amk

    > };
    >
    > /* PyMODINIT_FUNC */
    > /* The docs say to use the above, but it doesn't exist in my copy of
    > * Python.h. */
    > void initusedefault()
    > {
    > (void) Py_InitModule("usedefault", SpamMethods);
    > }
    > #v-
    >
    > setup.py
    > #v+
    > from distutils.core import setup, Extension
    >
    > module1 = Extension('usedefault',
    > libraries = ["ncurses"],
    > sources = ['usedefault.c'])
    >
    > setup (name = 'usedefault',
    > version = '1.0',
    > description = 'Use default colors in curses',
    > ext_modules = [module1])
    > #v-
    >
    A.M. Kuchling, Jul 31, 2003
    #5
  6. Brian Victor

    Brian Victor Guest

    Brian Victor wrote:
    > Interestingly, both Terminal and iTerm seem to make everything
    > transparent, so use_default_colors has no significant effect. That is,
    > a black background is indistinguishable from a transparent/default
    > background.


    I forgot to mention that I hit a compile error on OSX's ncurses header
    file. I believe it came from the December dev tools. It dealt with
    redefining wchar_t:

    #ifndef _WCHAR_T
    typedef unsigned long wchar_t;
    #endif /* _WCHAR_T */

    I have no idea why the mac version does that (fink's does also). My
    linux copy of the headers has nothing like that. #defining _WCHAR_T
    before including ncurses.h fixed that, but I don't know if that's a
    permanent solution. FWIW, I was doing this with python 2.3a1.

    --
    Brian
    Brian Victor, Aug 1, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skeleton Man

    curses and color

    Skeleton Man, May 25, 2004, in forum: Perl
    Replies:
    0
    Views:
    607
    Skeleton Man
    May 25, 2004
  2. Edmond Ho

    curses! trouble with pads and panels

    Edmond Ho, Sep 2, 2003, in forum: Python
    Replies:
    1
    Views:
    390
    Thomas Dickey
    Sep 2, 2003
  3. Pierluigi Fabbris

    Curses and Events and Other

    Pierluigi Fabbris, Dec 12, 2003, in forum: Python
    Replies:
    0
    Views:
    271
    Pierluigi Fabbris
    Dec 12, 2003
  4. Matt Garman
    Replies:
    1
    Views:
    300
    Matt Garman
    Nov 10, 2004
  5. Jean-Paul Calderone
    Replies:
    2
    Views:
    421
    Joel Hedlund
    Feb 9, 2006
Loading...

Share This Page