Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not get terminal dimensions when we're piping to someone #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions coloredlogcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@

HEADER_SIZE = USER_WIDTH + PROCESS_WIDTH + TAG_WIDTH + PRIORITY_WIDTH + 4

# enable interactive mode only if we're printing to a tty, not if we're piping to someone
interactive = sys.stdout.isatty()

# unpack the current terminal width/height
data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
HEIGHT, WIDTH = struct.unpack('hh',data)
if interactive:
data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
HEIGHT, WIDTH = struct.unpack('hh',data)

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

Expand Down Expand Up @@ -143,6 +147,7 @@ def millis_color(match):
try:
line = input.readline()
except KeyboardInterrupt:
sys.stdout.flush()
break

line = line.expandtabs(4)
Expand Down Expand Up @@ -201,7 +206,8 @@ def millis_color(match):
message = retime.sub(millis_color, message)

# insert line wrapping as needed
message = indent_wrap(message, HEADER_SIZE, WIDTH)
if interactive:
message = indent_wrap(message, HEADER_SIZE, WIDTH)

linebuf.write(message)
print linebuf.getvalue()