-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathn0.c
300 lines (234 loc) · 6.22 KB
/
n0.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
/*
_______ .___.__ __
____ \ _ \ ____ ____ __| _/|__|/ |_ ____ ______
/ \/ /_\ \ / \_/ __ \ / __ | | \ __\/ _ \_ __ \
| | \ \_/ \ | \ ___// /_/ | | || | ( <_> ) | \/
|___| /\_____ /___| /\___ >____ | |__||__| \____/|__|
\/ \/ \/ \/ \/
[ made by github.com/n0nexist ]
*/
#include <sys/ioctl.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include "n0config.h"
int number_of_files;
int window_offset = 0;
int woffset;
void goodbye(int code)
{
endwin();
system("clear");
printf("Exiting with code: %i\n", code);
exit(0);
}
int countFilesinDir(char *dirtoOpen){
struct dirent **namelist;
int n;
int number = 0;
n = scandir(dirtoOpen, &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
number += 1;
free(namelist[n]);
}
free(namelist);
}
return number;
}
int isDirectory(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
int listDir(WINDOW *win, int index, int fileIndex, char *dirtoOpen, char *operation, char* main_cwd, int statusbarindex){
struct dirent **namelist;
int n;
char *filename;
char *status;
char trashcmd[PATH_MAX];
char editorcmd[PATH_MAX];
char *selectedfilename;
int isdir;
chdir(dirtoOpen);
index += window_offset;
n = scandir(dirtoOpen, &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
if (index < statusbarindex){
if (index > 0){
filename = namelist[n]->d_name;
if (fileIndex == index+woffset){
if(operation == "list"){
wattron(win, COLOR_PAIR(2));
selectedfilename = filename;
}else if (operation == "goforward"){
isdir = isDirectory(filename);
if (isdir == 0){
sprintf(editorcmd, "%s %s",editor_cmd, filename);
system(editorcmd);
} else {
number_of_files = countFilesinDir(filename);
moveIndex(win, 0, fileIndex, filename, statusbarindex);
}
return 1;
}else if (operation == "delfile"){
sprintf(trashcmd, "%s %s",trash_cmd, filename);
system(trashcmd);
return 1;
}
}
}
}
mvwprintw(win, index, 1, filename);
wattron(win, COLOR_PAIR(1));
isdir = isDirectory(selectedfilename);
if(isdir != 0){
status = dir_string;
setStatus(win,status,1,statusbarindex);
} else {
status = file_string;
setStatus(win,status,1,statusbarindex);
}
wrefresh(win);
index += 1;
free(namelist[n]);
}
free(namelist);
}
return index;
}
void setTitle(WINDOW *win){
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
char mytitle[sizeof(cwd)];
sprintf(mytitle, "[%s]", cwd);
mvwprintw(win, 0, 1, mytitle);
}
void setStatus(WINDOW *win, char* status, int x, int y){
mvwprintw(win, y, x, status);
refresh();
}
void clearWindow(WINDOW *win){
wclear(win);
box(win,0,0);
refresh();
curs_set(0);
setTitle(win);
wattron(win, COLOR_PAIR(1));
}
void moveIndex(WINDOW *win, int index, int fileIndex, char *dirtoOpen, int statusbarindex){
clearWindow(win);
index = listDir(win,index,fileIndex,dirtoOpen,"list","",statusbarindex);
refresh();
}
void windowFunc(WINDOW *win, int wsrow, char* dirtoOpen, char* main_cwd){
chdir(dirtoOpen);
int index = 1;
int fileIndex = 1;
DIR *d;
DIR *directory;
int statusbarindex = wsrow-1;
char* selectedItem;
char* dirchanged;
curs_set(0);
moveIndex(win, index, fileIndex, dirtoOpen, statusbarindex);
for (;;)
{
int c = wgetch(win);
// goodbye(c);
switch (c){
case keyb_exit:
goodbye(c);
break;
case keyb_go_down:
if (fileIndex < (number_of_files-window_offset)){
fileIndex += 1;
} else {
fileIndex = 1;
}
break;
case keyb_go_up:
if (fileIndex > 1){
fileIndex -= 1;
}else {
fileIndex = number_of_files;
}
break;
case keyb_go_forw:
woffset = window_offset;
window_offset = 0;
index = listDir(win,index,fileIndex,dirtoOpen,"goforward", main_cwd, statusbarindex);
fileIndex = 1;
break;
case keyb_go_backw:
window_offset = 0;
dirchanged = "..";
number_of_files = countFilesinDir(dirchanged);
moveIndex(win, index, fileIndex, dirchanged, statusbarindex);
fileIndex = 1;
break;
case keyb_l_offset:
if (window_offset < 0){
window_offset ++;
fileIndex = 1;
}
break;
case keyb_u_offset:
window_offset --;
fileIndex = 1;
break;
case keyb_blank_file:
system(editor_cmd);
break;
case keyb_trash:
woffset = window_offset;
window_offset = 0;
index = listDir(win,index,fileIndex,dirtoOpen,"delfile", main_cwd, statusbarindex);
fileIndex = 1;
woffset = 0;
window_offset = 0;
break;
}
wclear(win);
moveIndex(win, index, fileIndex, dirtoOpen, statusbarindex);
}
}
int main(int argc, char *argv[])
{
(void) signal(SIGINT, goodbye);
(void) initscr();
keypad(stdscr, TRUE);
(void) nonl();
(void) cbreak();
(void) noecho();
if (!has_colors())
{
goodbye(2);
}
char* main_cwd;
getcwd(main_cwd, PATH_MAX);
start_color();
use_default_colors();
init_pair(1, fg_color, bg_color);
init_pair(2, fg_color_selected, bg_color_selected);
curs_set(0);
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
WINDOW *win = newwin(w.ws_row, w.ws_col, 0, 0);
char* dirtoOpen = ".";
number_of_files = countFilesinDir(dirtoOpen);
windowFunc(win, w.ws_row, dirtoOpen, main_cwd);
goodbye(0);
}