-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtduint.c
835 lines (708 loc) · 19 KB
/
tduint.c
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
* tduint.c
* This file is part of tdu, a text-mode disk usage visualization utility.
*
* Copyright (C) 2004-2012 Darren Stuart Embry.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307$
*/
#include "tduint.h"
#include "node.h"
#include "nowrap.h"
#include <stdlib.h>
#include <curses.h>
#include <stdio.h>
#include <signal.h>
#include <sys/ioctl.h>
/*
#include <pty.h>
*/
#include <string.h>
#include <errno.h>
node_s *root_node; /* root node of tree being displayed */
int cursor_line; /* line # in tree where "cursor" is located */
int start_line; /* line # in tree located at top of screen */
int prev_start_line; /* used in updating after cursor is moved */
int visible_lines; /* # lines on screen */
/* For more information about line numbers: see find_node_numbered() and
find_node_number_in() functions in node.c */
WINDOW *tdu_window;
WINDOW *main_window;
WINDOW *status_window;
static char *tree_chars_string[] = {
"`- ", /* IAM_LAST == 0 */
"+- ", /* IAM_NOTLAST == 1 */
" ", /* PARENT_LAST == 2 */
"| " /* PARENT_NOTLAST == 3 */
};
int ascii_tree_chars = 0;
int show_descendents = 0;
/* Generic function to display "tree branch" characters for a node and
its parents. */
void
display_tree_chars (node_s *node, /* programs specify node to display */
int levelsleft, /* # levels to go up. */
int thisisit) /* is this the node we're displaying? */
{
tree_chars_enum tc;
if (node->parent && levelsleft > 0) {
display_tree_chars(node->parent,
(levelsleft < 0) ? -1 : (levelsleft - 1),
0);
}
if (!levelsleft) return;
tc = (thisisit
? (node->is_last_child ? IAM_LAST : IAM_NOTLAST)
: (node->is_last_child ? PARENT_LAST : PARENT_NOTLAST));
if (ascii_tree_chars) {
wprintw_nowrap(main_window, tree_chars_string[tc]);
}
else {
switch (tc) {
case IAM_LAST:
waddch_nowrap(main_window, ACS_LLCORNER);
waddch_nowrap(main_window, ACS_HLINE);
break;
case IAM_NOTLAST:
waddch_nowrap(main_window, ACS_LTEE);
waddch_nowrap(main_window, ACS_HLINE);
break;
case PARENT_LAST:
waddch_nowrap(main_window, ' ');
waddch_nowrap(main_window, ' ');
break;
case PARENT_NOTLAST:
waddch_nowrap(main_window, ACS_VLINE);
waddch_nowrap(main_window, ' ');
break;
}
waddch_nowrap(main_window, ' ');
}
}
/* Generic function to display a node from a tree on a specific line
of the display. */
void
display_node (int line, /* line number on screen */
node_s *node, /* node to display */
int level) /* amount of indentation */
{
wmove(main_window, line, 0);
wclrtoeol(main_window);
if (!node) return;
wprintw_nowrap(main_window, "%11ld ", node->size);
if (show_descendents)
wprintw_nowrap(main_window, "%11ld ", node->descendents);
display_tree_chars(node, level, 1);
wprintw_nowrap(main_window, "%s", node->name);
if (node->nchildren && !node->expanded) {
wprintw_nowrap(main_window, " ...");
}
}
/* Generic function to display nodes from a tree on the screen. */
int /* returns number of lines displayed */
display_nodes (int line, /* starting line number on screen */
int lines, /* number of lines to display on screen */
node_s *node, /* node of parent */
long nodeline, /* line # within tree to start displaying */
long cursor) /* line # within tree where cursor is
located */
{
int nodes = display_nodes_(line, lines, node, nodeline, cursor, 0);
line += nodes;
lines -= nodes;
for (; lines > 0; ++line, --lines) {
display_node(line, NULL, 0);
}
return nodes;
}
/* This function does the "dirty work" for display_nodes(). */
/* === THIS IS A RECURSIVE FUNCTION. === */
int /* returns number of lines dsplayed */
display_nodes_ (int line, /* starting line number on screen */
int lines, /* number of lines to display on screen */
node_s *node, /* node of parent */
long nodeline, /* line # within tree to start displaying */
long cursor, /* line # within tree where cursor is loc'd */
int level) /* level of indentation */
{
long ret = 0; int i; long l;
if (!(node && nodeline >= 0 &&
nodeline < (1 + node->expanded) && lines)) return 0;
/* Do we start displaying the tree at this node? */
if (nodeline == 0) {
display_node(line, node, level);
++ret; ++line; --lines; /* a line was displayed */
--cursor; /* one node closer to cursor */
}
else {
/* skip parent node */
--nodeline; /* one node closer to start displaying */
--cursor; /* one node closer to cursor */
}
if (node->expanded && node->children && node->nchildren && lines) {
/* Now traverse the children to discover which of
those children nodeline refers to a node inside. */
i = 0; /* child number */
while (i < node->nchildren
&& nodeline >= (l = 1 + node->children[i]->expanded)) {
nodeline -= l;
cursor -= l;
++i;
}
/* Now that nodeline is less than the number of
visible nodes, we start displaying nodes from
inside node->children[i]. We may have to display nodes
from one or more of the next children as well. */
while (i < node->nchildren && lines > 0) {
l = display_nodes_(line, lines, node->children[i],
nodeline, cursor, level+1);
ret += l; line += l; lines -= l;
nodeline = 0; /* continue at top of next
child's visible tree */
cursor -= (1 + node->children[i]->expanded);
++i;
}
}
return ret;
}
void
tdu_hide_cursor ()
{
curs_set(0);
}
void
tdu_show_cursor ()
{
wmove(main_window, cursor_line - start_line, 0);
curs_set(1);
wrefresh(main_window);
}
void
tdu_interface_finish (int sig)
{
endwin();
if (sig >= 0) exit(0);
}
/* ensure that cursor is in a location on the screen, adjusting
viewport if necessary, and refresh the display. This function is
usually invoked before the next character is read. */
void
tdu_interface_refresh ()
{
int lines;
if (prev_start_line < 0) {
display_nodes(0, visible_lines, root_node, start_line,
cursor_line);
}
else {
/* make sure cursor_line is within reasonable range */
if (cursor_line < 0)
cursor_line = 0;
else if (cursor_line > root_node->expanded)
cursor_line = root_node->expanded;
/* position start_line such that
cursor_line is on the screen */
if (cursor_line < start_line)
/* up off the screen? */
start_line = cursor_line;
else if (cursor_line > (start_line+visible_lines-1))
/* down off the screen? */
start_line = cursor_line - (visible_lines - 1);
/* make sure start_line is within reasonable range */
if (start_line < 0)
start_line = 0;
else if (start_line > root_node->expanded)
start_line = root_node->expanded;
/* do we have to scroll the screen? */
lines = start_line - prev_start_line;
/* if we scroll too much, just redisplay the whole thing */
if (lines <= -visible_lines || lines >= visible_lines)
display_nodes(0, visible_lines, root_node,
start_line, cursor_line);
/* do we need to scroll down? */
else if (lines < 0) {
wscrl(main_window, lines);
display_nodes(0, -lines, root_node,
start_line, cursor_line);
}
/* do we need to scroll up? */
else if (lines > 0) {
wscrl(main_window, lines);
display_nodes(visible_lines - lines, lines, root_node,
start_line + visible_lines - lines,
cursor_line);
}
}
wrefresh(status_window);
wrefresh(main_window);
tdu_show_cursor();
prev_start_line = start_line;
}
/* Re-display the entire screen */
void
tdu_interface_display ()
{
display_nodes(0, visible_lines, root_node, start_line, cursor_line);
tdu_interface_refresh();
}
/* Expand the tree at the cursor and redraw whichever portions of the
screen need to be redrawn (or redraw the cursor line and all lines
below if redraw is nonzero). Always redraws cursor line and all
lines below if levels > 1. */
void
tdu_interface_expand (int levels, int redraw)
{
node_s *n;
long scrolllines;
n = find_node_numbered(root_node, cursor_line);
if (!n) return;
scrolllines = expand_tree(n, levels);
if (!scrolllines) return;
if (!redraw && (levels > 1))
redraw = 1;
if (redraw) {
display_nodes(cursor_line - start_line,
visible_lines - (cursor_line - start_line),
root_node, cursor_line, cursor_line);
tdu_interface_refresh();
}
else {
long maxlines = visible_lines - (cursor_line - start_line);
if (scrolllines >= maxlines - 1) {
display_nodes(cursor_line - start_line,
visible_lines - (cursor_line
- start_line),
root_node, cursor_line, cursor_line);
}
else {
tdu_interface_refresh();
winsdelln(main_window, scrolllines);
display_nodes(cursor_line - start_line,
scrolllines + 1,
root_node, cursor_line, cursor_line);
}
tdu_interface_refresh();
}
}
/* Collapse the tree at the cursor and redraw whichever portions of
the screen need to be redrawn (or redraw the cursor line and all
lines below if redraw is nonzero) */
void
tdu_interface_collapse (int redraw)
{
node_s *n;
long scrolllines;
n = find_node_numbered(root_node, cursor_line);
if (!n) return;
scrolllines = collapse_tree(n);
if (!scrolllines) return;
if (redraw) {
display_nodes(cursor_line - start_line,
visible_lines - (cursor_line - start_line),
root_node, cursor_line, cursor_line);
tdu_interface_refresh();
}
else {
long maxlines = visible_lines - (cursor_line - start_line);
if (scrolllines >= maxlines - 1) {
display_nodes(cursor_line - start_line,
visible_lines - (cursor_line
- start_line),
root_node, cursor_line, cursor_line);
}
else {
tdu_interface_refresh();
winsdelln(main_window, -scrolllines);
display_nodes(cursor_line - start_line, 1,
root_node, cursor_line, cursor_line);
display_nodes(visible_lines - scrolllines, scrolllines,
root_node,
cursor_line + maxlines - scrolllines,
cursor_line);
}
tdu_interface_refresh();
}
}
void
tdu_interface_move_up (int n)
{
cursor_line -= n;
tdu_interface_refresh();
}
void
tdu_interface_move_down (int n)
{
cursor_line += n;
tdu_interface_refresh();
}
void
tdu_interface_move_to (int n)
{
cursor_line = n;
tdu_interface_refresh();
}
void
tdu_interface_page_up ()
{
cursor_line -= (visible_lines - 1);
tdu_interface_refresh();
}
void
tdu_interface_page_down ()
{
cursor_line += (visible_lines - 1);
tdu_interface_refresh();
}
void
status_line_message (char *message)
{
tdu_hide_cursor();
wmove(status_window, 0, 0);
wclrtoeol(status_window);
if (message && *message) {
wattron(status_window, A_REVERSE);
wprintw_nowrap(status_window, "%s", message);
wattroff(status_window, A_REVERSE);
}
wrefresh(status_window);
}
void
tdu_interface_sort (node_sort_fp fp, bool reverse, bool isrecursive)
{
node_s *n = find_node_numbered(root_node, cursor_line);
if (n && n->expanded) {
long lines = n->expanded;
long maxlines = visible_lines - (cursor_line - start_line) - 1;
status_line_message("sorting...");
tree_sort(n, fp, reverse, isrecursive);
status_line_message(NULL);
if (lines > maxlines) lines = maxlines;
display_nodes(cursor_line - start_line + 1,
lines,
root_node,
cursor_line + 1,
cursor_line);
tdu_interface_refresh();
}
}
void
tdu_interface_get_screen_size (int *lines, int *columns)
{
FILE *tty;
struct winsize ws;
tty = fopen("/dev/tty", "r+");
if (!tty) {
int e = errno;
endwin();
errno = e;
perror("fopen /dev/tty");
exit(1);
}
if (ioctl(fileno(tty), TIOCGWINSZ, &ws)) {
int e = errno;
endwin();
errno = e;
perror("ioctl TIOCGWINSZ");
exit(1);
}
fclose(tty);
*lines = ws.ws_row;
*columns = ws.ws_col;
}
void
tdu_interface_compute_visible_lines ()
{
int bx, by, ex, ey;
getbegyx(main_window, by, bx);
getmaxyx(main_window, ey, ex);
visible_lines = ey - by;
}
void
tdu_interface_resize_handler (int sig)
{
int lines, columns;
tdu_interface_get_screen_size(&lines, &columns);
resize_term(lines, columns);
delwin(main_window);
delwin(status_window);
wrefresh(curscr); /* according to ncurses/test/view.c,
linux needs this. */
main_window = newwin(LINES - 1, COLS, 0, 0);
status_window = newwin(1, COLS, LINES - 1, 0);
keypad(main_window, TRUE);
scrollok(main_window, 1);
tdu_interface_compute_visible_lines();
prev_start_line = -1; /* force complete refresh */
if (cursor_line > (start_line + visible_lines - 1)) {
start_line = cursor_line - (visible_lines - 1);
}
tdu_interface_display();
}
void
tdu_interface_init_ncurses ()
{
if (!freopen("/dev/tty", "r", stdin)) {
perror("fopen /dev/tty");
exit(1);
}
tdu_window = initscr();
keypad(tdu_window, TRUE);
nonl();
cbreak();
noecho();
scrollok(tdu_window, 1);
main_window = newwin(LINES - 1, COLS, 0, 0);
status_window = newwin(1, COLS, LINES - 1, 0);
keypad(main_window, TRUE);
scrollok(main_window, 1);
tdu_interface_compute_visible_lines();
prev_start_line = -1; /* force complete refresh on initialization */
start_line = 0;
cursor_line = 0;
tdu_interface_display();
}
void
tdu_interface_help (char *message)
{
werase(main_window);
wmove(main_window, 0, 0);
while (*message) {
int len, show;
int maxy, maxx, y, x;
len = strcspn(message, "\r\n");
show = (len < COLS) ? len : COLS - 1;
getmaxyx(main_window, maxy, maxx);
getyx(main_window, y, x);
wclrtoeol(main_window);
waddnstr(main_window, message, show);
message += len;
message = strchr(message, '\n');
if (!message) break;
message += 1;
if (*message) {
if (y >= (maxy - 1)) {
wrefresh(main_window);
status_line_message("More... (press any key)");
wgetch(main_window);
status_line_message(NULL);
werase(main_window);
wmove(main_window, 0, 0);
}
else {
waddstr(main_window, "\n");
}
}
}
wrefresh(main_window);
status_line_message("Press any key to continue.");
wgetch(main_window);
status_line_message(NULL);
prev_start_line = -1; /* force complete refresh */
tdu_interface_display();
}
void
tdu_interface_keypress (int key)
{
int sortrecursive;
static int expandlevel = 0;
static int lastkey = -1;
sortrecursive = (lastkey == '=');
switch (key) {
case ERR:
break;
case KEY_RESIZE:
return; /* don't want to set lastkey here. */
case '=':
break;
case 3: /* C-c */
case 27: /* ESC */
case 'Q':
case 'q':
case 'X':
case 'x':
tdu_interface_finish(-1);
exit(0);
case '?':
tdu_interface_help(TDU_ONLINE_HELP);
break;
case 'c':
case 'C':
tdu_interface_help(TDU_COPYRIGHT_INFO);
break;
case 16: /* C-p */
case 'K':
case 'k':
case KEY_PREVIOUS:
case KEY_UP:
tdu_hide_cursor();
tdu_interface_move_up(1);
break;
case 14: /* C-n */
case 'j':
case 'J':
case KEY_NEXT:
case KEY_DOWN:
tdu_hide_cursor();
tdu_interface_move_down(1);
break;
case '<':
case KEY_HOME:
tdu_hide_cursor();
tdu_interface_move_to(0);
break;
case '>':
case KEY_END:
tdu_hide_cursor();
tdu_interface_move_to(root_node->expanded);
break;
case KEY_SPREVIOUS:
tdu_hide_cursor();
tdu_interface_move_up(10);
break;
case KEY_SNEXT:
tdu_hide_cursor();
tdu_interface_move_down(10);
break;
case KEY_PPAGE:
tdu_hide_cursor();
tdu_interface_page_up();
break;
case KEY_NPAGE:
tdu_hide_cursor();
tdu_interface_page_down();
break;
case 'P':
case 'p':
{
node_s *node = find_node_numbered(root_node, cursor_line);
node_s *parent = node ? node->parent : NULL;
if (parent) {
tdu_hide_cursor();
tdu_interface_move_to(find_node_number_in(parent,
root_node));
}
break;
}
case 'u':
tdu_interface_sort(node_cmp_unsort,
0, sortrecursive); break;
case 'U':
tdu_interface_sort(node_cmp_unsort,
1, sortrecursive); break;
case 's':
tdu_interface_sort(node_cmp_size,
0, sortrecursive); break;
case 'S':
tdu_interface_sort(node_cmp_size,
1, sortrecursive); break;
case 'n':
tdu_interface_sort(node_cmp_name,
0, sortrecursive); break;
case 'N':
tdu_interface_sort(node_cmp_name,
1, sortrecursive); break;
case 'd':
tdu_interface_sort(node_cmp_descendents,
0, sortrecursive); break;
case 'D':
tdu_interface_sort(node_cmp_descendents,
1, sortrecursive); break;
case 'l':
case 'L':
case '1':
case 6: /* C-f */
case KEY_RIGHT:
key = KEY_RIGHT; /* so lastkey can be checked for repeats */
if (lastkey == KEY_RIGHT || (lastkey >= '2' && lastkey <= '9'))
++expandlevel;
else
expandlevel = 1;
tdu_interface_expand(expandlevel, expandlevel > 1);
break;
case 12: /* C-l */
start_line = cursor_line - visible_lines / 2;
tdu_interface_refresh();
break;
case 18: /* C-r */
clearok(curscr, 1);
wrefresh(status_window);
wrefresh(main_window);
break;
case '#':
show_descendents = !show_descendents;
prev_start_line = -1;
tdu_interface_display();
break;
case 'a':
case 'A':
ascii_tree_chars = !ascii_tree_chars;
prev_start_line = -1;
tdu_interface_display();
break;
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
expandlevel = key - '0';
tdu_interface_expand(expandlevel, 1);
break;
case '*':
tdu_interface_expand(-1 /* fully */, 1);
break;
case 'h':
case 'H':
case '0':
case 2: /* C-b */
case KEY_LEFT:
expandlevel = 0;
tdu_interface_collapse(0);
break;
default:
beep();
break;
}
lastkey = key;
}
void
tdu_interface_run (node_s *node)
{
int key;
int clear_status_line;
root_node = node;
if (root_node->nchildren == 1) {
root_node = root_node->children[0];
}
signal(SIGINT, tdu_interface_finish);
signal(SIGWINCH, tdu_interface_resize_handler);
tdu_interface_init_ncurses();
status_line_message("This is tdu. Type ? for help. "
"Type C for license terms.");
clear_status_line = 1;
tdu_show_cursor();
tdu_show_cursor();
while (1) {
key = wgetch(main_window);
if (clear_status_line) {
status_line_message(NULL);
tdu_show_cursor();
clear_status_line = 0;
}
tdu_interface_keypress(key);
}
}