bzr doesn't detect correct terminal width

Bug #3507 reported by Martin Pool
4
Affects Status Importance Assigned to Milestone
Bazaar
Fix Released
Low
Martin Pool

Bug Description

bzr needs to know the terminal width for e.g. drawing the progress bar and (in some contexts) truncating log messages to a single line. It often doesn't do so on Unix and falls back to the default of 80 columns.

Most modern shells/terminals will have $COLUMNS set to the right value, but it is sometimes not exported, so Python can't see it. One fix is to tell people to export it from their rc file.

Another approach is to use curses to query the window size.

Revision history for this message
Martin Pool (mbp) wrote :

A good workaround for the moment is to just

 export COLUMNS

Revision history for this message
Martin Pool (mbp) wrote :

<Kamion> mpool: the way I get the terminal width in C without relying on COLUMNS being exported is approximately

'struct winsize wsz;
if
               (!ioctl(fileno(stdin), TIOCGWINSZ, &wsz) && wsz.ws_col) line_length = wsz.ws_col;'

Revision history for this message
Robert Collins (lifeless) wrote :

Kamions fix looks useful, just need to turn it into python.

Changed in bzr:
status: Unconfirmed → Confirmed
Revision history for this message
Martin Pool (mbp) wrote :

Looks like it can be done using fcntl.ioctl and termios.TIOCGWINSZ without needing a C extension.

Changed in bzr:
assignee: nobody → mbp
Revision history for this message
Martin Pool (mbp) wrote : obtain terminal_width via curses

 From: <email address hidden>
 Subject: [PATCH] obtain terminal_width via curses
 Date: 25 April 2006 8:24:57 AM
 To: <email address hidden>

The attached patch modifies "bzr log --log-format line" to
use the actual terminal width instead of a hard coded 79.

Further osutils.py:terminal_width() is modified to use the
curses module to get the current terminal width.

In order to avoid flickering due to subsequent calls to
curses.initsrc(), the width is saved in a global variable.
It should not change the line buffering of stdout, but still
seems to do it on my terminal/curses/python combination for
the output of "bzr log ..."

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Here is the code I use in Smart PM:

def get_screen_width():
    s = struct.pack('HHHH', 0, 0, 0, 0)
    try:
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
    except IOError:
        return 80
    return struct.unpack('HHHH', x)[1]

Revision history for this message
John A Meinel (jameinel) wrote :

I'm not sure exactly when this was fixed, but at least the test suite uses the real terminal width.

Changed in bzr:
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.