/
viewfile.cs
537 lines (464 loc) · 10.7 KB
/
viewfile.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//
// File viewer
//
// The file viewer can be used as a widget (ViewWidget) or as a
// command that drives the whole viewing system (FullView -- a
// full screen Container)
//
// Supports multiple encodings + raw viewing.
using System;
using System.IO;
using Mono.Terminal;
using System.Text;
namespace MouselessCommander {
public class ViewWidget : Widget {
// When in "cooked" mode, this reads encoded text
StreamReader reader;
// Encoding used to read
Encoding encoding;
// When in raw mode, we read directly from this stream
Stream source;
// The top byte displayed by the view
long top_byte;
// The first byte in the file that has contents (skipping the BOM)
long first_file_byte;
// Whether we are doing a Raw rendering, or a processed one.
bool Raw;
// Dirty management
DateTime dirtystart;
int dirty;
bool wrap = true;
public ViewWidget (int x, int y, int w, int h, bool raw, Stream source) : base (x, y, w, h)
{
this.source = source;
this.Raw = raw;
CanFocus = true;
DetectEncoding ();
top_byte = first_file_byte;
}
static Encoding utf8 = new UTF8Encoding (false, false);
static Encoding utf16le = new UnicodeEncoding (false, false);
static Encoding utf16be = new UnicodeEncoding (true, false);
static Encoding utf32le = new UTF32Encoding (false, false);
static Encoding utf32be = new UTF32Encoding (true, false);
int GetChar ()
{
if (Raw)
return source.ReadByte ();
else
return reader.Read ();
}
void DetectEncoding ()
{
if (Raw)
return;
first_file_byte = 0;
encoding = utf8;
// Try to detect the encoding
byte [] buffer = new byte [4];
var n = source.Read (buffer, 0, 4);
if (n == -1)
return;
if (n > 1 && buffer [0] == 0xfe && buffer [1] == 0xff){
encoding = utf16be;
first_file_byte = 2;
} else if (n > 1 && buffer [0] == 0xff && buffer [1] == 0xfe){
if (n > 3 && buffer [2] == 0 && buffer [3] == 0){
encoding = utf32le;
first_file_byte = 4;
} else {
encoding = utf16le;
first_file_byte = 2;
}
} else if (n > 3 && buffer [0] == 0 && buffer [1] == 0 && buffer [2] == 0xfe && buffer [3] == 0xff){
encoding = utf32be;
first_file_byte = 4;
}
if (n > 2 && buffer [0] == 0xef && buffer [1] == 0xbb && buffer [2] == 0xbf){
encoding = utf8;
first_file_byte = 3;
}
source.Position = first_file_byte;
reader = new StreamReader (source, encoding);
}
void SetPosition (long position)
{
source.Position = position;
if (Raw)
return;
reader.DiscardBufferedData ();
}
// Fills with blanks from the current column/row
// until the end of the widget area
public void ClearToEnd (int ccol, int crow)
{
for (int r = crow; r < h; r++){
Move (r+y, ccol+x);
for (int c = ccol; c < w; c++)
Curses.addch (' ');
ccol = 0;
}
}
public void ClearToEnd (int ccol)
{
for (int c = ccol; c < w; c++)
Curses.addch (' ');
}
void DrawStatus ()
{
Move (y, x);
Curses.attrset (Container.ContainerColorFocus);
for (int i = 0; i < w; i++)
Curses.addch (' ');
Move (y, x);
Curses.addstr ("File: FOOBAR");
}
public override void Redraw ()
{
DrawStatus ();
DrawView ();
}
void DrawView ()
{
int col = 0;
bool skip_until_newline = false;
Curses.attrset (Container.ContainerColorNormal);
Move (y+1, x);
SetPosition (top_byte);
for (int row = 0; row < h; ){
int c = GetChar ();
switch (c){
/* End of file */
case -1:
ClearToEnd (col, row);
row = h;
continue;
case 10:
ClearToEnd (col);
col = 0;
row++;
skip_until_newline = false;
Move (y+row, x+col);
continue;
case 9:
for (int nc = (col/8+1) * 8; col < nc; col++)
Curses.addch (' ');
continue;
case 13:
continue;
}
// Control chars or unicode > 0xffff
if (c < 32 || c > 0xffff)
c = '.';
if (skip_until_newline)
continue;
Curses.addch ((char) c);
col++;
if (col > w){
if (Wrap){
col = 0;
row++;
} else
skip_until_newline = true;
}
}
}
public bool Wrap {
get {
return wrap;
}
set {
wrap = value;
Redraw ();
}
}
int ReadChar ()
{
if (Raw || encoding == utf8)
return source.ReadByte ();
var a = source.ReadByte ();
var b = source.ReadByte ();
if (a == -1 || b == -1)
return -1;
if (encoding == utf16le)
return b << 8 | a;
else if (encoding == utf16be)
return a << 8 | b;
var c = source.ReadByte ();
var d = source.ReadByte ();
if (c == -1 || d == -1)
return -1;
if (encoding == utf32be)
return (a << 24) | (b << 16) | (c << 8) | d;
else
return (d << 24) | (c << 16) | (b << 8) | a;
}
int ReadChar (long position)
{
source.Position = position;
return ReadChar ();
}
int ScanSize ()
{
if (Raw || encoding == utf8)
return 1;
if (encoding == utf16be || encoding == utf16le)
return 2;
if (encoding == utf32be || encoding == utf32le)
return 4;
return 1;
}
// We can not use the StreamReader here
//
// Returns the new file offset where we start displaying, or -1 if we can not
// scroll further.
long ScanForward (long start, int lines)
{
SetPosition (start);
for (int line = 0; line < lines; ){
int b = ReadChar ();
if (b == -1)
return -1;
if (b == '\n')
line++;
}
return source.Position;
}
long WrappedScanForward (long start, int lines)
{
SetPosition (start);
int col = 0;
for (int line = 0; line < lines; ){
int b = ReadChar ();
switch (b){
case -1:
return -1;
case '\n':
col = 0;
line++;
break;
case '\r':
// ignore;
break;
case '\t':
col = (col/8+1)*8;
if (col > w){
line++;
col = col-w;
}
break;
default:
col++;
break;
}
if (col == w){
col = 0;
line++;
}
}
return source.Position;
}
// We can not use the StreamReader here
//
// Returns the new file offset where we start displaying, or -1 if we can not
// scroll further.
long ScanBackward (long start, int lines)
{
int scan_size = ScanSize ();
long scan = start-scan_size;
for (int line = 0; line < lines && scan > first_file_byte; ){
while (scan > first_file_byte){
int b = ReadChar (scan-scan_size);
if (b == -1)
return first_file_byte;
if (b == '\n'){
if (++line == lines)
return scan;
scan -= scan_size;
break;
}
scan -= scan_size;
}
}
return scan;
}
// Counts the lenght of the string between [start,end) assuming that
// @start is the first column and that there are no new-lines embedded
int CountLength (long start, long end)
{
int len = 0;
for (long pos = start; pos < end; pos++){
int b = ReadChar (pos);
switch (b){
case -1:
return len;
case '\r':
case '\n': // this really should not be called with newlines in the range
continue;
case '\t':
len = (len/8+1)*8;
continue;
default:
len++;
break;
}
}
return len;
}
long WrappedScanBackward (long start, int lines)
{
int scan_size = ScanSize ();
long scan = start;
for (int line = 0; line < lines && scan > first_file_byte; ){
var previous_line = ScanBackward (scan, 1);
int chars = CountLength (previous_line, scan);
int linecount = chars == 0 ? 1 : (chars+w-1)/w;
if (line + linecount <= lines){
scan = previous_line;
line += linecount;
continue;
}
return WrappedScanForward (previous_line, linecount-(lines-line));
}
return scan;
}
void SetTopByte (long newpos)
{
if (dirty == 0)
dirtystart = DateTime.UtcNow;
dirty++;
top_byte = newpos;
}
// Invoke this method instead of Redraw+refresh, as this takes care of updating while
// scrolling fast.
void UpdateView ()
{
if (Application.MainLoop.EventsPending ()){
if (dirty < 10 && (DateTime.UtcNow-dirtystart) < TimeSpan.FromMilliseconds (500)){
Log ("Skipped update, dirty={0} time={1}", dirty, DateTime.UtcNow-dirtystart);
return;
}
}
// to test the dirty system:
// System.Threading.Thread.Sleep (500);
Redraw ();
Curses.refresh ();
dirty = 0;
}
long Scan (long from, int lines)
{
if (!Raw)
reader.DiscardBufferedData ();
if (lines > 0)
return Wrap ? WrappedScanForward (from, lines) : ScanForward (from, lines);
else
return Wrap ? WrappedScanBackward (from, -lines) : ScanBackward (from, -lines);
}
void Scroll (int lines)
{
Log ("Scroll: {0}", lines);
long newpos = Scan (top_byte, lines);
Log ("Scroll: {0} top_byte={1} newpos={2}", lines, top_byte, newpos);
if (newpos == -1)
return;
SetTopByte (newpos);
UpdateView ();
}
void GoTop ()
{
SetTopByte (first_file_byte);
Redraw ();
Curses.refresh ();
}
void GoBottom ()
{
long last = source.Length;
SetPosition (last);
var newtop = Scan (last > 0 ? last-1 : 0, -h+1);
if (newtop == -1)
return;
SetTopByte (newtop);
Redraw ();
Curses.refresh ();
}
public override bool ProcessKey (int key)
{
switch (key){
// page down: space bar, control-v, page down:
case 32:
case 22:
case Curses.KeyNPage:
Scroll (h-1);
break;
// down-arrow, control-n
case Curses.KeyDown:
case 14:
Scroll (1);
break;
// backspace, page-up, Alt-V
case 8:
case Curses.KeyPPage:
case Curses.KeyAlt + 'v':
Scroll (-(h-1));
break;
// cursor, control-p
case Curses.KeyUp:
case 16:
Scroll (-1);
break;
case Curses.KeyHome:
case Curses.KeyAlt + '<':
GoTop ();
break;
case Curses.KeyEnd:
case Curses.KeyAlt + '>':
GoBottom ();
break;
default:
return false;
}
return true;
}
}
public class FullView : Container {
ViewWidget view;
ButtonBar bar;
string [] bar_labels = new string [] {
"", "Wrap", "Quit", "", "", "", "", "", "", "Quit"
};
void SetWrap (bool wrap)
{
if (view.Wrap){
view.Wrap = false;
bar_labels [1] = "Wrap";
} else {
view.Wrap = true;
bar_labels [1] = "Unwrap";
}
Curses.refresh ();
}
public FullView (Stream source) : base (0, 0, Application.Cols, Application.Lines)
{
view = new ViewWidget (0, 0, Application.Cols, Application.Lines-1, false, source);
bar = new ButtonBar (bar_labels);
bar.Action += delegate (int n){
switch (n){
case 2:
SetWrap (!view.Wrap);
break;
case 3:
case 10:
Running = false;
break;
}
};
Add (view);
Add (bar);
}
static public void Show (Stream source)
{
var full = new FullView (source);
Application.Run (full);
}
}
}