Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse: terminal width is not detected properly #57250

Closed
zbysz mannequin opened this issue Sep 24, 2011 · 12 comments
Closed

argparse: terminal width is not detected properly #57250

zbysz mannequin opened this issue Sep 24, 2011 · 12 comments
Labels
3.8 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@zbysz
Copy link
Mannequin

zbysz mannequin commented Sep 24, 2011

BPO 13041
Nosy @florentx, @berkerpeksag
PRs
  • bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter #8459
  • Dependencies
  • bpo-13609: Add "os.get_terminal_size()" function
  • Files
  • issue13041.patch: use shutil.get_terminal_size()
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-07-25.15:24:20.395>
    created_at = <Date 2011-09-24.22:07:18.739>
    labels = ['3.8', 'type-feature', 'library']
    title = 'argparse: terminal width is not detected properly'
    updated_at = <Date 2018-07-25.15:24:20.393>
    user = 'https://bugs.python.org/zbysz'

    bugs.python.org fields:

    activity = <Date 2018-07-25.15:24:20.393>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-07-25.15:24:20.395>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2011-09-24.22:07:18.739>
    creator = 'zbysz'
    dependencies = ['13609']
    files = ['24602']
    hgrepos = []
    issue_num = 13041
    keywords = ['patch']
    message_count = 12.0
    messages = ['144507', '144521', '144523', '149531', '149587', '150728', '153961', '223222', '223298', '223809', '315274', '322365']
    nosy_count = 7.0
    nosy_names = ['bethard', 'zbysz', 'denilsonsa', 'flox', 'jmehnle', 'berker.peksag', 'paul.j3']
    pr_nums = ['8459']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13041'
    versions = ['Python 3.8']

    @zbysz
    Copy link
    Mannequin Author

    zbysz mannequin commented Sep 24, 2011

    COLUMNS is a shell variable (updated whenever the window size
    changes), that usually isn't exported to programs. Therefore checking
    for COLUMNS in sys.environ will not work in the majority of cases. The
    proper check is to use the TIOCGWINSZ ioctl on stdout.

    Why COLUMNS is not exported? Because it can change during the lifetime
    of a program. Therefore it is better to use the dynamic ioctl.

    @zbysz zbysz mannequin added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Sep 24, 2011
    @zbysz
    Copy link
    Mannequin Author

    zbysz mannequin commented Sep 25, 2011

    I see that adding a separate module was proposed in issue bpo-8408, which was rejected/closed. I don't have the rights to reopen, so I'll continue here.

    bpo-8408 was proposing a new module, which seems a bit overkill, since the implementation for unix and windows is about 20 lines.

    I'm attaching a second version of the patch which works on windows (tested with python3.2.2 on XP). Thanks to techtonik for pointing to a windows imlementation.

    @denilsonsa
    Copy link
    Mannequin

    denilsonsa mannequin commented Sep 25, 2011

    bpo-8408 was proposing a new module, which seems a bit overkill

    If a module seems overkill, then maybe add this useful function to os module. Don't leave it private to argparse module. Maybe something along these lines:

        >>> import os
        >>> print(os.get_terminal_size())
        (80, 25)

    Why do I believe a module could be better? Because I'd also like some way to detect when the terminal size has changed (without probing it all the time).

    @bethard
    Copy link
    Mannequin

    bethard mannequin commented Dec 15, 2011

    I'd feel more comfortable with the argparse fix if it were simply calling "os.get_terminal_size()". I recommend that you:

    • Create a new issue called, say "add os.get_terminal_size()" proposing just the single method.

    • Add that issue to the Dependencies of this issue.

    Once that is fixed, then the argparse fix should be simple.

    @denilsonsa
    Copy link
    Mannequin

    denilsonsa mannequin commented Dec 16, 2011

    Issue bpo-13609 created, but I don't have permission to edit the dependencies.

    @zbysz
    Copy link
    Mannequin Author

    zbysz mannequin commented Jan 6, 2012

    New version to use after bpo-13609 is implemented: patch2.diff

    @zbysz
    Copy link
    Mannequin Author

    zbysz mannequin commented Feb 22, 2012

    OK, I guess that this could now be closed, since 13609 has been commited.
    (It is currently reopened, but the proposed tweaks wouldn't influence
    the usage in argparse, even if accepted).

    I'm attaching a patch which updates the tests to the new $COLUMNS logic.
    Previously, unsetting COLUMNS would fix the width on 80, now setting
    COLUMNS=80 is the proper way to do this.

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 16, 2014

    @paul the attached patch is extremely simple and follows the work on bpo-13609. Is it okay with you if the patch was committed?

    @berkerpeksag berkerpeksag added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Jul 16, 2014
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jul 16, 2014

    The latest patch, using _shutil.get_terminal_size(), looks fine.

    It lets environ['COLUMNS'] have priority over the end user's terminal width, as demonstrated by the change to test_argparse. test_argparse doesn't test changing the actual terminal size, but I imagine that would be a pain to implement.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jul 24, 2014

    For now the user could add this to his module:

        import os, shutil
        os.environ['COLUMNS'] = str(shutil.get_terminal_size().columns)

    @jmehnle
    Copy link
    Mannequin

    jmehnle mannequin commented Apr 13, 2018

    What's holding up the merging of this patch?

    @berkerpeksag
    Copy link
    Member

    New changeset 74102c9 by Berker Peksag in branch 'master':
    bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter (GH-8459)
    74102c9

    @berkerpeksag berkerpeksag added the 3.8 (EOL) end of life label Jul 25, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant