1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

text wrap now adapts to terminal

This commit is contained in:
Brian Coca 2015-08-06 17:13:39 -04:00
parent 80cb69214b
commit 25e67bb716

View file

@ -19,6 +19,7 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import fcntl
import textwrap
import os
import random
@ -27,6 +28,8 @@ import sys
import time
import logging
import getpass
from struct import unpack, pack
from termios import TIOCGWINSZ
from multiprocessing import Lock
from ansible import constants as C
@ -57,6 +60,7 @@ class Display:
def __init__(self, verbosity=0):
self.columns = None
self.verbosity = verbosity
# list of all deprecation messages to prevent duplicate display
@ -68,6 +72,7 @@ class Display:
self.noncow = os.getenv("ANSIBLE_COW_SELECTION",None)
self.set_cowsay_info()
self._set_column_width()
def set_cowsay_info(self):
@ -161,8 +166,8 @@ class Display:
else:
raise AnsibleError("[DEPRECATED]: %s. Please update your playbooks." % msg)
wrapped = textwrap.wrap(new_msg, 79, replace_whitespace=False, drop_whitespace=False)
new_msg = "\n".join(wrapped)
wrapped = textwrap.wrap(new_msg, self.columns, replace_whitespace=False, drop_whitespace=False)
new_msg = "\n".join(wrapped) + "\n"
if new_msg not in self._deprecations:
self.display(new_msg.strip(), color='purple', stderr=True)
@ -170,7 +175,7 @@ class Display:
def warning(self, msg):
new_msg = "\n[WARNING]: %s" % msg
wrapped = textwrap.wrap(new_msg, 79)
wrapped = textwrap.wrap(new_msg, self.columns)
new_msg = "\n".join(wrapped) + "\n"
if new_msg not in self._warns:
self.display(new_msg, color='bright purple', stderr=True)
@ -194,7 +199,7 @@ class Display:
#FIXME: make this dynamic on tty size (look and ansible-doc)
msg = msg.strip()
star_len = (80 - len(msg))
star_len = (self.columns + 1 - len(msg))
if star_len < 0:
star_len = 3
stars = "*" * star_len
@ -217,7 +222,7 @@ class Display:
def error(self, msg, wrap_text=True):
if wrap_text:
new_msg = "\n[ERROR]: %s" % msg
wrapped = textwrap.wrap(new_msg, 79)
wrapped = textwrap.wrap(new_msg, self.columns)
new_msg = "\n".join(wrapped) + "\n"
else:
new_msg = msg
@ -239,3 +244,11 @@ class Display:
msg = to_bytes(msg)
return msg
def _set_column_width(self):
if os.isatty(0):
tty_size = unpack('HHHH', fcntl.ioctl(0, TIOCGWINSZ, pack('HHHH', 0, 0, 0, 0)))[1]
else:
tty_size = 0
self.columns = max(79, tty_size)