From 5dc66bb4ca1576fecffcaf907c22626095d1868e Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 29 Apr 2010 10:49:50 +0200 Subject: [PATCH] make py.io.ansi_print and py.io.get_terminal_width() directly available. --HG-- branch : trunk --- py/__init__.py | 2 ++ py/_cmdline/pylookup.py | 4 +++- testing/io_/test_terminalwriter.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/py/__init__.py b/py/__init__.py index 5bf7769a7..c8e3faae9 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -138,6 +138,8 @@ py.apipkg.initpkg(__name__, dict( 'StdCapture' : '._io.capture:StdCapture', 'StdCaptureFD' : '._io.capture:StdCaptureFD', 'TerminalWriter' : '._io.terminalwriter:TerminalWriter', + 'ansi_print' : '._io.terminalwriter:ansi_print', + 'get_terminal_width' : '._io.terminalwriter:get_terminal_width', }, # small and mean xml/html generation diff --git a/py/_cmdline/pylookup.py b/py/_cmdline/pylookup.py index 047117f09..369ce90cd 100755 --- a/py/_cmdline/pylookup.py +++ b/py/_cmdline/pylookup.py @@ -9,7 +9,7 @@ prepended.""" import sys, os import py -from py._io.terminalwriter import ansi_print, terminal_width +from py.io import ansi_print, get_terminal_width import re def rec(p): @@ -21,6 +21,8 @@ parser.add_option("-i", "--ignore-case", action="store_true", dest="ignorecase", parser.add_option("-C", "--context", action="store", type="int", dest="context", default=0, help="How many lines of output to show") +terminal_width = get_terminal_width() + def find_indexes(search_line, string): indexes = [] before = 0 diff --git a/testing/io_/test_terminalwriter.py b/testing/io_/test_terminalwriter.py index a664aab20..b1c057da8 100644 --- a/testing/io_/test_terminalwriter.py +++ b/testing/io_/test_terminalwriter.py @@ -2,6 +2,10 @@ import py import os, sys from py._io import terminalwriter +def test_get_terminal_width(): + x = py.io.get_terminal_width + assert x == terminalwriter.get_terminal_width + def test_terminal_width_COLUMNS(monkeypatch): """ Dummy test for get_terminal_width """ @@ -147,3 +151,13 @@ def test_attr_hasmarkup(): tw.line("hello", bold=True) s = tw.stringio.getvalue() assert len(s) > len("hello") + +def test_ansi_print(): + # we have no easy way to construct a file that + # represents a terminal + f = py.io.TextIO() + f.isatty = lambda: True + py.io.ansi_print("hello", 0x32, file=f) + text2 = f.getvalue() + assert text2.find("hello") != -1 + assert len(text2) >= len("hello")