Commit 25e67bb7 by Brian Coca

text wrap now adapts to terminal

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