# Path: src/rosrepo/util.py
# def isatty(fd):
#     return hasattr(fd, "isatty") and fd.isatty()
# 
# def get_terminal_size():
#     global _cached_terminal_size
#     if _cached_terminal_size is not None:
#         return _cached_terminal_size
#     try:
#         with open(os.ctermid(), "rb") as f:
#             cr = struct.unpack('hh', fcntl.ioctl(f.fileno(), termios.TIOCGWINSZ, '1234'))
#     except (IOError, struct.error):
#         raise OSError("Cannot determine terminal size")
#     _cached_terminal_size = int(cr[1]), int(cr[0])
#     return _cached_terminal_size
# 
# class UserError(RuntimeError):
#     pass
# 
# Path: src/rosrepo/terminal_color.py
# def fmt(msg, use_color=True, reset=True):
#     """Replaces color annotations with ansi escape sequences"""
#     global _ansi
#     msg = msg.replace('@!', '@{boldon}')
#     msg = msg.replace('@/', '@{italicson}')
#     msg = msg.replace('@_', '@{ulon}')
#     msg = msg.replace('@|', '@{reset}')
#     t = ColorTemplate(msg)
#     return t.safe_substitute(_ansi if use_color else _no_ansi) + (ansi('reset') if reset and use_color else '')


import sys
import re
from getpass import getpass
    from itertools import izip_longest as zip_longest
    from itertools import zip_longest
from .util import isatty, get_terminal_size, UserError
from .terminal_color import fmt as color_fmt
    from .terminal_color import ansi
        from .resolver import get_system_package_manager
        actual_width = sum(width) + 3 * len(width) - 1
        try:
            total_width = get_terminal_size()[0]
        except OSError:
            total_width = None
        if fd is None:
            fd = sys.stdout
        if use_color is None:
            use_color = isatty(fd)
        if total_width is not None:
            if self.expand and actual_width < total_width:
                width[-1] += total_width - actual_width
                actual_width = total_width
            while actual_width > total_width:
                max_width = max(width)
                if max_width == 1:
                    break
                for i in range(len(width)):
                    if width[i] == max_width:
                        width[i] -= 1
                        actual_width -= 1
                        break
        if self.columns:
            fmt = color_fmt(" " + (" @{pf}" + HSEP + "@| ").join(["%s"] * len(width)) + "\n", use_color=use_color)
            fd.write(color_fmt("@{pf}" + HTHICK + THEAD.join([HTHICK * w for w in width]) + HTHICK + "\n", use_color=use_color))
            sep = color_fmt("@{pf}" + HTHIN + XSEP.join([HTHIN * w for w in width]) + HTHIN + "\n", use_color=use_color)
            end = color_fmt("@{pf}" + HTHICK + TFOOT.join([HTHICK * w for w in width]) + HTHICK + "\n", use_color=use_color)
            fd.write(fmt % tuple(pad_ansi_text(color_fmt("@!%s" % c, use_color=use_color), w) for c, w in zip(self.columns, width)))
        else:
            fmt = " %s   %s\n"

# Path: src/rosrepo/util.py
# def isatty(fd):
#     return hasattr(fd, "isatty") and fd.isatty()
# 
# def get_terminal_size():
#     global _cached_terminal_size
#     if _cached_terminal_size is not None:
#         return _cached_terminal_size
#     try:
#         with open(os.ctermid(), "rb") as f:
#             cr = struct.unpack('hh', fcntl.ioctl(f.fileno(), termios.TIOCGWINSZ, '1234'))
#     except (IOError, struct.error):
#         raise OSError("Cannot determine terminal size")
#     _cached_terminal_size = int(cr[1]), int(cr[0])
#     return _cached_terminal_size
# 
# class UserError(RuntimeError):
#     pass
# 
# Path: src/rosrepo/terminal_color.py
# def fmt(msg, use_color=True, reset=True):
#     """Replaces color annotations with ansi escape sequences"""
#     global _ansi
#     msg = msg.replace('@!', '@{boldon}')
#     msg = msg.replace('@/', '@{italicson}')
#     msg = msg.replace('@_', '@{ulon}')
#     msg = msg.replace('@|', '@{reset}')
#     t = ColorTemplate(msg)
#     return t.safe_substitute(_ansi if use_color else _no_ansi) + (ansi('reset') if reset and use_color else '')


# Path: src/rosrepo/ui.py
import sys
import re
from getpass import getpass
    from itertools import izip_longest as zip_longest
    from itertools import zip_longest
from .util import isatty, get_terminal_size, UserError
from .terminal_color import fmt as color_fmt
    from .terminal_color import ansi
        from .resolver import get_system_package_manager

        actual_width = sum(width) + 3 * len(width) - 1
        try:
            total_width = get_terminal_size()[0]
        except OSError:
            total_width = None
        if fd is None:
            fd = sys.stdout
        if use_color is None:
            use_color = isatty(fd)
        if total_width is not None:
            if self.expand and actual_width < total_width:
                width[-1] += total_width - actual_width
                actual_width = total_width
            while actual_width > total_width:
                max_width = max(width)
                if max_width == 1:
                    break
                for i in range(len(width)):
                    if width[i] == max_width:
                        width[i] -= 1
                        actual_width -= 1
                        break
        if self.columns:
            fmt = color_fmt(" " + (" @{pf}" + HSEP + "@| ").join(["%s"] * len(width)) + "\n", use_color=use_color)
            fd.write(color_fmt("@{pf}" + HTHICK + THEAD.join([HTHICK * w for w in width]) + HTHICK + "\n", use_color=use_color))
            sep = color_fmt("@{pf}" + HTHIN + XSEP.join([HTHIN * w for w in width]) + HTHIN + "\n", use_color=use_color)
            end = color_fmt("@{pf}" + HTHICK + TFOOT.join([HTHICK * w for w in width]) + HTHICK + "\n", use_color=use_color)
            fd.write(fmt % tuple(pad_ansi_text(color_fmt("@!%s" % c, use_color=use_color), w) for c, w in zip(self.columns, width)))
        else:
            fmt = " %s   %s\n"

            sep = color_fmt("@{pf}" + (HTHIN * actual_width) + "@|\n", use_color=use_color)