blob: a5eb99d0ebca2f00c598775773b0a0d7e1d3914c (
plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
import java.io.*;
public class Truncater
{
public static void main(final String... args) throws IOException
{
final int width;
if ((width = getWidth()) > 15) //sanity
{
final OutputStream stdout = new BufferedOutputStream(System.out);
OutputStream out = new OutputStream()
{
/**
* The number of column on the current line
*/
private int x = 0;
/**
* Escape sequence state
*/
private int esc = 0;
/**
* Last bytes as written
*/
private boolean ok = true;
/**
* {@inheritDoc}
*/
@Override
public void write(final int b) throws IOException
{
if (this.esc == 0)
{
if (b == '\n')
{
if (x >= width)
{
write('\033');
write('[');
write('4');
write('9');
write('m');
}
this.x = -1;
}
else if (b == '\t')
{
int nx = 8 - (x & 7);
for (int i = 0; i < nx; i++)
write(' ');
return; //(!)
}
else if (b == '\033')
this.esc = 1;
}
else if (this.esc == 1)
{
if (b == '[') this.esc = 2;
else if (b == ']') this.esc = 3;
else this.esc = 10;
}
else if (this.esc == 2)
{
if ((('a' <= b) && (b <= 'z')) || (('A' <= b) && (b <= 'Z')))
this.esc = 10;
}
else if ((this.esc == 3) && (b == 'P'))
{
this.esc = ~0;
}
else if (this.esc < 0)
{
this.esc--;
if (this.esc == ~7)
this.esc = 10;
}
else
this.esc = 10;
if ((x < width) || (this.esc != 0) || (ok && ((b & 0xC0) == 0x80)))
{
stdout.write(b);
if (this.esc == 0)
if ((b & 0xC0) != 0x80)
x++;
ok = true;
}
else
ok = false;
if (this.esc == 10)
this.esc = 0;
}
/**
* {@inheritDoc}
*/
@Override
public void flush() throws IOException
{
stdout.flush();
}
};
System.setOut(new PrintStream(out));
}
InputStream in = System.in;
OutputStream out = System.out;
for (int d; (d = in.read()) != -1;)
out.write(d);
out.flush();
}
/**
* Gets the width of the terminal
*
* @return The width of the terminal
*/
public static int getWidth()
{
try
{
Process process = (new ProcessBuilder("/bin/sh", "-c", "tput cols 2> " + (new File("/dev/stderr")).getCanonicalPath())).start();
String rcs = new String();
InputStream stream = process.getInputStream();
int c;
while (((c = stream.read()) != '\n') && (c != -1))
rcs += (char)c;
try
{
stream.close();
}
catch (final Throwable err)
{
//Ignore
}
return Integer.parseInt(rcs);
}
catch (final Throwable err)
{
return -1;
}
}
}
|