This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Update Curses HOWTO for 3.4
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: akuchling, docs@python, eric.araujo, ezio.melotti, python-dev, vstinner
Priority: normal Keywords:

Created on 2013-04-11 22:34 by akuchling, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
update-curses-howto.txt akuchling, 2013-04-11 22:34 Patch updating the curses howto review
update-curses-howto.txt akuchling, 2013-04-12 14:19 Patch v2 review
update-curses-howto.txt akuchling, 2013-04-29 21:41 review
Messages (12)
msg186599 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-04-11 22:34
Here's a patch updating the Curses HOWTO for 3.4 that can be applied to the default branch.  Changes:

* curses.wrapper.wrapper is now just curses.wrapper
* Mention window.encoding and its effect on Unicode.
* Describe getkey() as well as getch()
* Improve some examples.
* Use 4-space indentation.
* Add links to urwid, an alternate UI library.
* Various edits & rewording.
msg186606 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-04-12 03:45
Do the changes (e.g. curses.wrapper new name) not apply to 3.3?
msg186635 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-04-12 14:19
Updated version of the patch:

* Correct the errors reported in the review.
* Restore 3-space indents.
* Mention the LINES and COLS variables.
* Add more links to the final section.

I believe everything in the howto also applies to 3.3; it would be fine if you want to apply it to the 3.3 branch as well.
msg188108 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-04-29 21:41
Updated version of the patch, applying many changes suggested by merwok:

* use ~curses.funcname notation for links.
* use 3-hyphen em-dash
* minor fixes to various examples
* rewrap long paragraphs (this makes the diff larger -- sorry!)
msg188110 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-04-29 21:47
window.get_wch() and curses.unget_wch(ch) should be used instead of window.getch() and curses.ungetch(ch), when available. They work much better with non-ASCII characters.
msg188119 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-04-29 23:46
Victor: I would like to add a section about using Unicode characters with curses, but found little material about doing that in either Python or the underlying C API.  Do you have any suggested references or example code that I could look at?

(It's probably better to commit the current patch if it's suitable, and commit the wide-character changes in a separate patch once they're ready.)
msg188774 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-05-09 12:37
I've just verified that I still have commit access to the repo, so unless someone has any further suggested changes to the patch, I'll go ahead and commit it to trunk.  Should I also commit it to the 3.3 branch?

Victor: I'm willing to improve the HOWTO's discussion of Unicode/wide characters if you can point me at some references.
msg188776 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-05-09 12:45
Patch LGTM.
You should commit it to 3.3 and then merge it with default (unless the patch contains parts that are specific to 3.4).
msg188797 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-05-10 00:14
New changeset 1fa70b797973 by Andrew Kuchling in branch '3.3':
#17700: update the curses HOWTO for 3.x
http://hg.python.org/cpython/rev/1fa70b797973

New changeset 70f530161b9b by Andrew Kuchling in branch 'default':
#17700: merge with 3.3
http://hg.python.org/cpython/rev/70f530161b9b
msg188798 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-05-10 00:16
Applied to 3.3 and 3.4.  I'll leave this issue open for a week so that Victor can comment on Unicode/wide-characters.
msg189409 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-05-16 20:36
> Applied to 3.3 and 3.4.  I'll leave this issue open
> for a week so that Victor can comment on Unicode/wide-characters.

I don't know (n)curses, but I tried to improve the curses module of Python. I added an encoding attribute which is the locale encoding by default, so it should work without special configuration. But it's good to know what the default encoding is. (It was UTF-8 on older Python 3 release, maybe in Python < 3.3.)

I also added functions like unget_wch() and get_wch().

The problem is that unget_wch() and get_wch() are not always available, it depends on the platform. If I remember correctly, it depends if libreadline is linked to libncursesw (and not libncurses).

Ah yes, and the Python curses modules uses Unicode versions of C curses functions if available. It is still possible to use thes bytes versions if you pass bytes strings. For example:

 * window.addstr("abc"): use *add_wstr() functions if available, *addstr() otherwise
 * window.addstr(b"abc"): always use *addstr() functions

I don't know enough the C libncursesw library to write an HOWTO or something like that.

I just would like to say that you should prefer get_wch() over getch() if get_wch() is available. But I don't understand exactly how curses behave with control characters ("keys"?) like "up" or "left".
msg190093 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2013-05-26 14:56
I can't make sense of unget_wch.  I used this test program:

--------

import curses

def main(stdscr):
    stdscr.addstr(0,0, "Key")
    stdscr.refresh()
    curses.ungetch(0x0149)
    while True:
        ch = stdscr.get_wch()
        stdscr.addstr(1,1, repr(ch) + '      ')
        if ch == 'q':
            break
        
curses.wrapper(main)

--------

If I use curses.unget_wch(chr(0x0149)), the following get_wch() call returns -119, not 0x149.

The whole area of wide character support in curses is just a mystery, and I don't know how to resolve my questions.  I'll close this item, since I have nothing further to add to the curses howto.
History
Date User Action Args
2022-04-11 14:57:44adminsetgithub: 61900
2013-05-26 14:56:39akuchlingsetstatus: open -> closed
resolution: fixed
messages: + msg190093

stage: patch review -> resolved
2013-05-16 20:36:42vstinnersetmessages: + msg189409
2013-05-10 00:16:54akuchlingsetmessages: + msg188798
2013-05-10 00:14:50python-devsetnosy: + python-dev
messages: + msg188797
2013-05-09 12:45:52ezio.melottisetmessages: + msg188776
2013-05-09 12:37:50akuchlingsetmessages: + msg188774
2013-04-29 23:46:07akuchlingsetmessages: + msg188119
2013-04-29 21:47:08vstinnersetnosy: + vstinner
messages: + msg188110
2013-04-29 21:41:30akuchlingsetfiles: + update-curses-howto.txt

messages: + msg188108
2013-04-13 16:45:55ezio.melottisetnosy: + ezio.melotti

versions: + Python 3.3
2013-04-12 14:19:28akuchlingsetfiles: + update-curses-howto.txt

messages: + msg186635
2013-04-12 03:45:18eric.araujosetnosy: + eric.araujo

messages: + msg186606
versions: + Python 3.4
2013-04-11 22:34:47akuchlingcreate