Red Hat BugZilla 584525 – “yum list” output wraps for no particular reason

This bug almost got fixed in 2010. But the bug report was closed without integrating the fix into yum, yum became obsolete, the fix was not carried over to dnf when it replaced yum, and since then the bug has been steadfastly ignored. Duplicate bug reports are regularly raised by people who continue to find the bug highly annoying. So it’s time the bug was squashed for good.

I’ve re-implemented the fix as a python dnf plugin, but it’d be nice to see this integrated into dnf so it’s available to everyone and doesn’t get left behind.

In the meantime, you can grab “ttywidth.py” from my GitHub repo and save it in your dnf plugin directory (eg on Fedora25: /usr/lib/python2.7/site-packages/dnf-plugins/). The plugin adds a width option to dnf (eg “--width=200“) so you can specify a wide terminal. This prevents variable line-wrapping caused by long package names or long repo names, so it’s useful when you pipe dnf output into grep or otherwise parse the output in a script.

Latest version of ttywidth.py from my GitHub

#!/usr/bin/python
# Copyright John Sincock, 2017

# Sets the TTY width (columns).
# Setting a sufficiently large tty width will prevent wrapping/line splitting even when the output contains long package/repo names.
#
# Inspired by old plugin for yum by James Antill, at:
#   https://james.fedorapeople.org/yum/plugins/ttysz.py
#
# Test with, eg:
# dnf --width=160 list | grep python3-django-rest-framework-composed-permissions.noarch

from __future__ import print_function
import dnf.cli.term

class ttyWidth(dnf.Plugin):
    name = 'ttyWidth'

    def __init__(self, base, cli):
        super(ttyWidth, self).__init__(base, cli)
        self.base = base
        self.cli = cli

        self.cli.optparser.main_parser.add_argument('-w', '--width', dest="ttywidth", type=int, metavar='width', help="Override the tty width (use large value to prevent wrapping of output lines).")
        #print("ttyWidth: finished init.")
        
    def config(self):
        opts = self.cli.optparser.parse_command_args(self.cli.command, self.base.args)
        sz = opts.ttywidth
        if sz is not None:
            dnf.cli.term.Term.columns = sz
            print("ttyWidth: setting tty width =",sz)

        #print("Finished ttyWidth config.")