-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowls.py
executable file
·70 lines (60 loc) · 1.85 KB
/
cowls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
''' April fools! Your normal ls binary is at /bin/ls.real '''
import os
import pexpect
import subprocess as sp
import sys
REAL_LS = '/bin/ls'
if sys.argv[0] == REAL_LS:
REAL_LS = REAL_LS + '.real'
def find_max_width(lines):
return max(line_width(line) for line in lines)
def line_width(line):
w = 0
chars = [x for x in line]
while chars:
char = chars.pop(0)
if char == "\t":
w += 8 - (w%8)
elif ord(char) == 0x1b:
while chars and chars.pop(0) != "m": pass
else:
w += 1
return w
def fallback_to_real_ls():
sys.exit(sp.call([REAL_LS] + sys.argv[1:]))
if not sys.stdout.isatty():
fallback_to_real_ls()
rows, cols = os.popen('stty size', 'r').read().strip().split()
cols = int(cols) - 12
if cols < 30:
fallback_to_real_ls()
class HackedSpawn(pexpect.spawn):
def setwinsize(self, dont_know, dont_care):
pexpect.spawn.setwinsize(self, int(rows), int(cols))
proc = HackedSpawn(sp.list2cmdline([REAL_LS] + sys.argv[1:]))
out = proc.read()
lines = out.splitlines()
if not lines:
lines = ["Moo..."]
width = find_max_width(lines)
if width > cols + 12:
fallback_to_real_ls()
if len(lines) == 1:
sides = ["<>"]
else:
sides = ["/\\"] + ["||"] * (len(lines)-2) + ["\\/"]
border_len = 4*((width+8)/4) - 1
print " " * 5 + "_" * border_len
for line, edges in zip(lines, sides):
padding = width - line_width(line.rstrip())
prefix = " " * 4 + edges[0] + "\t" + line.rstrip() + " " * padding
padding = 4*((line_width(prefix)+4)/4) - line_width(prefix)
right_edge = " " * padding + edges[1]
print prefix + right_edge
print " " * 5 + "^" * border_len
print (" \\ ^__^\n"
" \\ (oo)\_______\n"
" (__)\\ )\\/\\\n"
" ||----w |\n"
" || ||")