-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduil.c
165 lines (134 loc) · 3.29 KB
/
duil.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
/**
* DUCK Terminal UI implementation for unix based system
*/
#include "dui.h"
#ifdef __unix__
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <ncurses.h>
static int scrollOffset = 0;
static int cursor;
static int curfetch;
static duioptions options;
/**
* Sets the cursor position if the cursor fetch flag is activated.
*/
void cursor_update(int pos)
{
if(!curfetch)
return;
cursor = MIN(pos + options.y, LINES);
scrollOffset = MAX(0, pos + options.y - LINES);
curfetch = 0;
}
void dui_init(duioptions doptions)
{
options = doptions;
cursor = 1 + options.y;
initscr();
if(has_colors())
{
use_default_colors();
start_color();
init_pair(1, -1, COLOR_GREEN);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
}
noecho();
keypad(stdscr, TRUE);
curs_set(0);
scrollok(stdscr, TRUE);
}
void dui_header()
{
attron(COLOR_PAIR(2));
printw("%s", headerMsg);
attroff(COLOR_PAIR(2));
}
void dui_print(dirtree *tree)
{
if(tree == NULL)
return;
/**
* Sets the cursor position to be on the same line with
* the highlighted file when navigating up or down the directory tree
*/
cursor_update(tree->selected_file + 1);
char out[310] = "";
char sz[10];
dui_tree_root_to_string(tree, out, options);
mvprintw(options.y, 0, "%s", out);
for(int i = 0 + scrollOffset; i < tree->nfiles; i++)
{
dirtree *d = tree->files[i];
// Size percentage for the current file
double percent = (d->size * 1.0) / tree->size;
size((double)d->size, sz);
// Change console text color to highlight this file if selected
if(i == tree->selected_file)
attron(COLOR_PAIR(1));
dui_tree_child_to_string(d, tree, out, options);
mvprintw(options.y + i - scrollOffset + 1, 0, "%s", out);
// Disable color
attroff(COLOR_PAIR(1));
}
}
void dui_clear(int mode)
{
if(mode & CLEAR_ALL)
{
move(options.y, 0);
clrtobot();
}
if(mode & CLEAR_CURSOR_OFFSET)
{
cursor = options.y + 1;
scrollOffset = 0;
// Enable the cursor fetch flag so it can be set on the next call of dui_print
curfetch = 1;
}
}
void dui_end()
{
curs_set(1);
endwin();
}
int dui_scroll_down()
{
if(cursor < LINES - 1)
{
// When the cursor is on the last line scroll
// by adding an offset when printing
// Also we need to clear the whole console when doing this to avoid
// artifacts, as lines can now be of different length
cursor++;
return CLEAR_ATTRIBUTES;
}
else
{
scrollOffset++;
return CLEAR_ALL;
}
}
int dui_scroll_up()
{
if(cursor + scrollOffset > LINES - 1)
{
// When the cursor is on the last line scroll
// by adding an offset when printing
// Also we need to clear the whole console when doing this to avoid
// artifacts, as lines can now be of different length
scrollOffset--;
return CLEAR_ALL;
}
else
{
cursor--;
return CLEAR_ATTRIBUTES;
}
}
#endif