-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdus
executable file
·542 lines (416 loc) · 15.1 KB
/
dus
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
538
539
540
#!/usr/bin/env perl
# Copyright (c) 2008-2014, Kurt D. Starsinic <[email protected]>
# Patches welcome.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the Artistic License 2.0.
#
# This program is distributed in the hope that it will be useful, but it is
# provided "as is" and without any express or implied warranties.
# For details, see the full text of the license at
# <http://www.perlfoundation.org/artistic_license_2_0>.
# This program is short on documentation. This shall be remedied in the
# upcoming release.
# Basic usage:
#
# dus.pl [followed by any arguments you would pass to du(1) on your system]
#
# The "Page Up" and "Page Down" keys will do what you expect.
# At any time you can press "h" for help, "q" to quit, or 'r' to restart
# (e.g., after you've deleted some files).
#
# NOTE: It is assumed that your system's du(1), as called with the parameters
# you pass, returns lines in the format /^([0-9]+)\s+(.+)/, where $1 is the
# size of the directory (in any consistent units) and $2 is the name of the directory.
#
# In particular, the output of "du -h" or "du --si" (which outputs sizes as a number
# followed by units, e.g., "44K" or "1.4G") will not be properly handled by dus.
# TODO:
# Implement help
# Add a "find" mode
# Allow left/right scrolling
# Allow drill down to files in directory
# Allow delete and shell-out in directory
# Proper STDERR handler (e.g., an alternate screen)
# Allow expand/collapse of subdirectories
# Customization:
# Colors (per-terminal)?
# Perl expressions
use strict;
use warnings; no warnings 'numeric', 'once';
require File::Spec;
require Curses;
my $W = begin_curses();
### %Du and @Du are the master data structures.
###
### There is one entry in %Du for every directory we've seen so far, and all
### of those directories' parent directories, all the way up to the top of
### the hierarchy we're doing a "du" on. The keys of %Du are directory names,
### and the values are array references.
###
### The first element of the array is the directory's size -- either estimated
### or actual.
###
### If there are *any* following elements, they are the names of the
### directory's subdirectories that we've seen so far (and the first entry is
### an *estimated* size).
###
### If there are *no* following elements, then the first element is the
### *actual* size of the directory.
###
### There is one line in @Du for every line displayed on the screen. Each
### element of @Du is a directory name. @Du is always sorted in size order.
my (%Du, @Du, $Tot);
# TODO: Get smarter about identifying non-option args.
$_ = File::Spec->canonpath($_) for grep { not /^-/ } @ARGV; # Canonicalize non-option args:
my $NumWidth = 7;
my $TopLineNum = 0;
my (%RootDirs, %Marked, $Watched);
my %Collapsed;
my $Message = '';
my $CursorY = 0;
my $Status;
my %Mode = (
Paused => 0,
AutoReload => 0,
);
my $du;
RELOAD:
$Status = '';
$Mode{Paused} = 0;
$Du{$_} = [ undef ] for keys %Du;
my $CursorFile = undef;
close $du if $du;
### Figure out which of our command line arguments are directory names.
### Create an entry in %Du for each of them, and tag them as "root" nodes.
my @dirs = grep { not /^-/ } @ARGV;
foreach my $arg (@dirs ? @dirs : '.') {
$RootDirs{File::Spec->canonpath($arg)} = 1;
}
sub message { $Message = $_[0] }
message();
my %Handler = (
Curses::KEY_NPAGE() => [ 'page down', sub { pc_scroll(+1) } ],
Curses::KEY_PPAGE() => [ 'page up', sub { pc_scroll(-1) } ],
k => [ 'up', sub { pc_move(-1) } ],
j => [ 'down', sub { pc_move(+1) } ],
Curses::KEY_UP() => 'k',
Curses::KEY_DOWN() => 'j',
m => [ 'mark', \&mark ],
w => [ 'watch (keep directory on-screen)', \&watch ],
'-' => [ 'collapse (TODO)', \&collapse ],
'+' => [ 'expand (TODO)', \&expand ],
r => [ 'reload', sub { goto RELOAD } ],
R => [ 'toggle autoreload', toggle_mode_functor('AutoReload') ],
q => [ 'quit', sub { end_curses(); exit } ],
h => [ 'help', \&help ],
'?' => 'h',
' ' => [ 'toggle display pause', toggle_mode_functor('Paused') ],
);
### The main loop:
### Call "du", passing along the arguments we were called with. For each line
### of output from "du", update the data structures and re-paint the screen.
my $deadline;
my $reload_time = 30; # TODO make this configurable
open $du, '-|', 'du', @ARGV;
while (1) {
if (eof($du)) {
if ($Status ne 'done') {
$Status = 'done';
close($du);
$deadline = time() + $reload_time;
}
if ($Mode{AutoReload}) {
my $seconds = $deadline - time();
if ($seconds > 0) {
message("Reload in $seconds seconds");
} else {
goto RELOAD;
}
} else {
message('');
$deadline = time() + $reload_time;
}
} elsif (not $Mode{Paused}) {
my ($n, $s) = (<$du> =~ /^([0-9]+)\s+(.+)/);
$s = File::Spec->canonpath($s);
@{ $Du{$s} } = ( $n );
remove($s); # Take the entries for $s and its parents out of @Du
bubble($s); # Put them back, in their new correct locations
$Tot = scalar keys %Du; # TODO Can we get more efficient about calculating $Tot?
}
draw_screen();
process_input();
}
exit;
sub draw_screen {
# If we're watching a directory, and we're currently processing "du" output,
# then let's make sure that our watched directory remains on-screen, and
# let's keep the cursor pointing at it:
if (defined $Watched and not $Mode{Paused} and $Status ne 'done') {
foreach my $i (0 .. $#Du) {
if ($Du[$i] eq $Watched) {
if ($i < $TopLineNum) { pc_scroll(0, $i - $TopLineNum) }
elsif ($i > bottom_line_num()) { pc_scroll(0, $i - bottom_line_num()) }
$CursorY = $i - $TopLineNum;
last;
}
}
}
### Draw all the lines with fileinfo:
my @lines = build_output_lines();
foreach my $i (0 .. $#lines) {
my ($attrs, $string) = @{ $lines[$i] };
add_attr_string($attrs, $i, 0, $string);
}
### Draw the status line on the last row:
show_status();
### Wipe up the tiny little bit that's left:
$W->clrtobot;
### Draw the upper right hand corner last,
### because that interacts best with the way we draw the rest of the screen:
my $hdr = length($Message) ? "[$Message] " : "";
my ($top, $bot) = (1+$TopLineNum, 1+$TopLineNum+$#lines);
$hdr .= " $top -- $bot of $Tot ";
if (my @modes = sort grep { $Mode{$_} } keys %Mode) {
$hdr .= " (@modes)";
}
add_attr_string(1, 0, $Curses::COLS-length($hdr)-5, $hdr);
$W->refresh;
}
sub build_output_lines {
my @lines;
$CursorFile = $Du[$CursorY + $TopLineNum];
foreach my $y (0 .. c_files_per_page() - 1) {
my $i = $y + $TopLineNum;
last if $i > $#Du;
my $path = $Du[$i];
my $attrs;
if ($Marked{$path}) { $attrs = 3 }
elsif ($RootDirs{$path}) { $attrs = 1 }
elsif ($#{ $Du{$path} }) { $attrs = 0 } # size is a running estimate
else { $attrs = 2 }
my $size = $Du{$path}[0];
my $c = ($y == $CursorY) ? ($Collapsed{$path} ? '}' : '>') : ($Collapsed{$path} ? '-' : ' ');
my $fileinfo = sprintf "%1.1s%s %-*s", $c, formatted_size($size), $Curses::COLS, $path;
push @lines, [ $attrs, $fileinfo ];
}
return @lines;
}
### Given the newly-arrived entry for $here, insert it into the proper slot in @Du. Then re-calculate
### $here's parent directory's size, and recurse up the directory tree until we hit a root node.
### TODO: honor %Collapsed
sub bubble {
my ($here) = @_;
my @split = File::Spec->splitdir(File::Spec->canonpath($here));
pop @split if @split > 1;
my $dotdot = File::Spec->catdir(@split);
# Update the parent directory's entry, unless we're at the root of a tree:
if (not $RootDirs{$here} and $here ne $dotdot) {
if (not $Du{$dotdot}) { # We've never even seen $dotdot before
$Du{$dotdot} = [ $Du{$here}[0], $here ];
} elsif (not grep { $_ eq $here } @{ $Du{$dotdot} }[1 .. $#{ $Du{$dotdot} }]) { # We've never seen $here before
$Du{$dotdot}[0] += $Du{$here}[0];
push @{ $Du{$dotdot} }, $here;
} else { # Update our estimated size for $here
$Du{$dotdot}[0] = 0;
$Du{$dotdot}[0] += $Du{$_}[0] for @{ $Du{$dotdot} }[1 .. $#{ $Du{$dotdot} }];
}
}
# Find the entry to insert $here after:
my $size = $Du{$here}[0];
my $i;
for ($i = $#Du; $i >= 0; $i--) {
my $path = $Du[$i];
my $test = (defined $Du{$path}[0]) ? $Du{$path}[0] : 0;
last if ($test > $size); # Primary sort key: size descending
last if ($test == $size) && ($path lt $here); # Secondary sort key: pathname ascending
}
# Now insert $here into @Du. If we made it all the way through the above loop without finding
# a place for it, then $i is -1, and we'll insert $here at the beginning of the array:
splice @Du, $i+1, 0, $here;
# Keep @Du from growing without bound, because splice() doesn't scale.
# XXX We should be able to keep @Du usually very small, but rebuild it on
# the fly when the user pages down
pop @Du if (@Du > 500 && @Du > $TopLineNum + c_files_per_page() + 1); # XXX Is 500 the right number?
bubble($dotdot) if ((not $RootDirs{$here}) and $here ne $dotdot); # Lather, rinse, repeat
$NumWidth = maxdef($NumWidth, length $Du{$Du[0]}[0]);
}
### Page/Cursor related functions
sub pc_move { # Move the cursor $num_lines; return value indicates whether we've hit the wall
my ($num_lines) = @_;
$CursorY += $num_lines;
if ($CursorY < c_min_y()) {
$TopLineNum = $TopLineNum - c_min_y() + $CursorY;
if ($TopLineNum < 0) {
$TopLineNum = 0;
Curses::beep();
}
$CursorY = c_min_y();
return($TopLineNum != 0);
} elsif ($CursorY > c_max_y()) {
my $max_top_line = $Tot - c_files_per_page();
$TopLineNum = $TopLineNum - c_max_y() + $CursorY;
if ($TopLineNum > $max_top_line) {
$TopLineNum = $max_top_line;
Curses::beep();
}
$CursorY = c_max_y();
return($TopLineNum != $max_top_line);
}
return 1;
}
sub pc_scroll {
my ($num_pages, $num_lines) = @_;
if ($num_pages) {
$TopLineNum += ($num_pages * c_files_per_page());
}
if ($num_lines) {
$TopLineNum += $num_lines;
$CursorY = mindef(maxdef($CursorY - $num_lines, c_min_y()), c_max_y());
}
# TODO: If we're going to tweak $TopLineNum now, then let's tweak $CursorY in the other direction
my $max_top_line = $Tot - c_files_per_page();
if ($TopLineNum < 0) {
$TopLineNum = 0;
Curses::beep();
return 0;
} elsif ($TopLineNum > $max_top_line) {
$TopLineNum = $max_top_line;
Curses::beep();
return 0;
}
return 1;
}
sub c_min_y { 0 } # min row for the cursor (also top-of-screen)
sub c_max_y { $Curses::LINES - 2 } # max row for the cursor (one line above bottom-of-screen)
sub c_files_per_page { $Curses::LINES - 1 }
sub bottom_line_num { $TopLineNum + c_files_per_page() - 1 }
### END Page related functions
sub maxdef {
if (@_) {
my $max;
foreach my $val (map { defined($_) ? $_ : 0 } @_) {
$max = $val if (not defined $max or $val > $max);
}
return $max;
}
return 0;
}
sub mindef {
if (@_) {
my $min;
foreach my $val (map { defined($_) ? $_ : 0 } @_) {
$min = $val if (not defined $min or $val < $min);
}
return $min;
}
return 0;
}
sub remove {
my ($path) = @_;
# Remove $here from @Du, if present:
@Du = grep { $_ ne $path } @Du;
# Now remove its parent directory, unless we're at the top of our tree:
# TODO: Use File::Spec to inspect $path
if (not $RootDirs{$path} and $path =~ s:/[^/]*$::) {
remove($path);
}
}
sub process_input {
while ((my $char = $W->getch) ne Curses::ERR()) { # There was input
if (my $handler = handler_for_char($char)) {
#message($handler->[0]) if defined $handler->[0];
$handler->[1]->();
} else {
Curses::beep();
#message("<$char>");
}
}
}
sub handler_for_char {
my ($char) = @_;
if (my $handler = $Handler{$char}) {
$handler = $Handler{$handler} while not ref $handler; # Key alias dereferencing
return $handler;
}
return;
}
sub mark { $Marked{$CursorFile} = not $Marked{$CursorFile} }
sub watch {
if ($Watched and $Watched eq $CursorFile) {
undef $Watched;
} else {
$Watched = $CursorFile;
}
mark();
}
sub collapse { $Collapsed{$CursorFile} = 1 }
sub expand { $Collapsed{$CursorFile} = 0 }
sub help {
end_curses();
my %map = (
Curses::KEY_NPAGE() => 'PgDn',
Curses::KEY_PPAGE() => 'PdUp',
Curses::KEY_UP() => 'Up',
Curses::KEY_DOWN() => 'Dn',
' ' => 'Spc',
);
print "$0: show du(1) output sorted in a curses window.\n";
print "--- key bindings ---\n";
foreach my $char (sort keys %Handler) {
my $handler = handler_for_char($char);
my $show = $map{$char} || $char;
printf " %-4s %s\n", $show, $handler->[0];
}
exit;
}
sub toggle_mode_functor { my ($m) = @_; sub { $Mode{$m} = not $Mode{$m} } }
sub formatted_size { defined($_[0]) ? sprintf("%*d", $NumWidth, $_[0]) : sprintf("%*s", $NumWidth, '-') }
my @Colors;
sub begin_curses {
close STDERR;
#open STDERR, '>', "STDERR.txt";
my $w = Curses->new;
Curses::start_color();
my $bg = Curses::COLOR_BLUE();
Curses::init_pair(1, Curses::COLOR_CYAN(), $bg);
Curses::init_pair(2, Curses::COLOR_YELLOW(), $bg);
Curses::init_pair(3, Curses::COLOR_WHITE(), $bg);
Curses::init_pair(4, Curses::COLOR_GREEN(), $bg);
Curses::assume_default_colors(Curses::COLOR_WHITE(), $bg);
@Colors = (
Curses::COLOR_PAIR(1) | Curses::A_BOLD(),
Curses::COLOR_PAIR(2) | Curses::A_BOLD(),
Curses::COLOR_PAIR(3) | Curses::A_BOLD(),
Curses::COLOR_PAIR(4) | Curses::A_BOLD(),
);
$w->nodelay(1);
$w->keypad(1);
Curses::noecho();
return $w;
}
sub add_attr_string {
my ($attr, $y, $x, $string) = @_;
$W->attrset($Colors[$attr]);
$W->addstr($y, $x, $string);
}
sub show_status {
if ($Status and length $Status) {
my $status = $Status;
$status = " $status ";
# The maximum "safe" length of a string on the bottom row is $Curses::COLS-1.
# If we go all the way to the bottom-right cell, many terminals will wrap
# the cursor and scroll everything up one line.
#
# This will leave a space in the rightmost column . . .
$status = "-$status-" while length $status < $Curses::COLS-4;
# . . . so we'll add an extra space in the leftmost column for symmetry.
$status = " $status";
add_attr_string(1, $Curses::LINES - 1, 0, $status);
}
}
sub end_curses {
Curses::endwin();
die "end_curses: @_" if @_;
}