Home:ALL Converter>curses - addstr text is not visible in larger terminals

curses - addstr text is not visible in larger terminals

Ask Time:2015-02-23T16:55:05         Author:lightandlight

Json Formatter

I'm writing an IRC client that uses Python's curses library, but the server's response is not output properly on the screen.

Basically, the smaller my terminal, the closer the output is to what it should be:

Full-sized terminal

On a full-sized terminal (1366x768 resolution) there is basically no output

Half-sized terminal

On a half-sized terminal, there is much more visible output

Quarter-sized terminal

On a quarter-sized terminal, the program outputs everything that I would expect it to.

Based on this pattern, my best guess is some line-length issue, but I really have no idea what the problem is.

Source Code

import curses
import queue
import socket
import threading
import time

PAD_LENGTH = 1000

def main(stdscr):
    s = socket.create_connection(("chat.freenode.net",6667))
    s.settimeout(1.0)

    s.send(b"PASS hello\r\n")
    s.send(b"NICK testingclient\r\n")
    s.send(b"USER testingclient tclient tclient :Test Client\r\n")

    s.setblocking(False)

    curses.noecho()
    curses.cbreak()
    stdscr.nodelay(1)

    pad = curses.newpad(PAD_LENGTH,curses.COLS-1)

    top = 0

    while True:
        try:
            msg = s.recv(512).decode("latin-1")
            pad.addstr(msg)
        except BlockingIOError:
            pass

        kc = stdscr.getch()

        if kc != -1:
            k = chr(kc)
            if k == "q":
                break
            elif k == "j" and top < PAD_LENGTH:
                top += 1
            elif k == "k" and top > 0:
                top -= 1

        pad.refresh(top,0,0,0,curses.LINES-1,curses.COLS-1)

    s.close()
    curses.nocbreak()
    curses.echo()
    curses.endwin()

curses.wrapper(main)

Misc Notes

I'm using i3 and xterm on Arch Linux in VirtualBox

Author:lightandlight,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28669929/curses-addstr-text-is-not-visible-in-larger-terminals
Thomas Dickey :

The LINES and COLS values are not magic in any sense; they are initialized by the curses library based on the terminal size (from the operating system), and can be overridden (usually) by setting environment variables (named $LINES and $COLUMNS). For more information about that, read the manual page for use_env\n\nIf your application is more readable with a narrower format, then you can always check the value of COLS in your script and limit the value which you pass to curses.newpad here:\n\npad = curses.newpad(PAD_LENGTH,curses.COLS-1)\n\n\nsay, to 40. You should use your own variable (a copy of COLS) in that call and reuse it in the other reference to COLS here:\n\npad.refresh(top,0,0,0,curses.LINES-1,curses.COLS-1)\n\n\nManipulating this using environment variables is, as noted, something that you could do -- but it is not often used. Keep in mind that if you set $LINES and/or $COLUMNS to larger values than what the terminal provides, the result will not be good.\n\nRecap from comments (there are other pitfalls which may be relevant, but I do not have a suitable configuration for testing your script):\n\n\nThe pad isn't the same chunk of memory as stdscr. A getch on a given window tells curses to refresh that window. So if there are pending changes on the window, it overwrites any other window as the curses library copies data to curscr.\nAnother potential problem is if your addstr's write newlines. A newline in addch/addstr will clear the remainder of the line.\n",
2015-03-03T10:09:13
yy