-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
224 lines (185 loc) · 6.36 KB
/
Copy pathmain.c
File metadata and controls
224 lines (185 loc) · 6.36 KB
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
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "./lib/dynamicCharArray.h"
#include "./paints/components/menu.h"
#include "./termVisuals/termVisuals.h"
#define ICON_DIRECTORY ""
#define ICON_C_FILE ""
#define ICON_H_FILE ""
#define ICON_SH_FILE ""
#define ICON_LUA_FILE ""
#define ICON_PDF_FILE ""
#define ICON_MARKDOWN_FILE ""
#define ICON_COMPRESS_FILE ""
#define ICON_GENERIC ""
#define ICON_CONFIG_FOLDER ""
#define ICON_SIM_LINK ""
int Mauve[] = {203, 166, 247};
int Red[] = {243, 139, 168};
int Maroon[] = {235, 160, 172};
int Peach[] = {250, 179, 135};
int Yellow[] = {249, 226, 175};
int Green[] = {166, 227, 161};
int Teal[] = {148, 226, 213};
int Sky[] = {137, 220, 235};
int Sapphire[] = {116, 199, 236};
int Blue[] = {137, 180, 250};
int Lavander[] = {180, 190, 254};
int Text[] = {205, 214, 244};
int Surface1[] = {69, 71, 90};
int Surface0[] = {49, 50, 68};
int Base[] = { 30, 30, 46 };
int Crust[] = {220, 224, 232};
int Overlay0[] = {108, 112, 134};
int Mantle[] = { 24, 24, 37 };
#define COLOR_DIRECTORY Blue
struct _DirEntry {
char d_name[256];
unsigned char d_type;
};
int compareFileExtencion(char *fileName, char *extencion){
int fileNameLen = strlen(fileName);
int extencionLen = strlen(extencion);
if (fileNameLen < extencionLen){
return 0;
}
for (int i = 0; i < extencionLen; i++){
if (fileName[fileNameLen-i-1] != extencion[extencionLen-i-1]){
return 0;
}
}
return 1;
}
int coppyToPath(struct DynamicCharArray *charArray, char *str){
if (addString(charArray, str) <= 0){
return 0;
} else {
return 1;
}
}
void addDirectoryEntriesToMenu(Menu *menu, DIR *directory, struct DynamicCharArray *fileNameBuffer){
int test;
int *curDirEntryColor;
struct _DirEntry *curDirEntry;
curDirEntry = (struct _DirEntry *)malloc(sizeof(struct _DirEntry));
strcpy(curDirEntry->d_name, "..");
curDirEntry->d_type = DT_DIR;
addToMenu(menu, " ..", Text, curDirEntry);
struct dirent *dirEntry;
while ((dirEntry = readdir(directory))){
resetDynamicCharArray(fileNameBuffer);
curDirEntryColor = Text;
if ((dirEntry->d_name[0] != '.' || dirEntry->d_name[1] != '\0') && (dirEntry->d_name[0] != '.' || dirEntry->d_name[1] != '.' || dirEntry->d_name[2] != '\0')){
addChar(fileNameBuffer, ' ');
curDirEntry = (struct _DirEntry *)malloc(sizeof(struct _DirEntry));
strcpy(curDirEntry->d_name, dirEntry->d_name);
curDirEntry->d_type = dirEntry->d_type;
if (curDirEntry->d_type == DT_DIR){
if (compareFileExtencion(curDirEntry->d_name, ".config")){
addString(fileNameBuffer, ICON_CONFIG_FOLDER);
} else {
addString(fileNameBuffer, ICON_DIRECTORY);
}
curDirEntryColor = Blue;
} else if (curDirEntry->d_type == DT_REG){
if (compareFileExtencion(curDirEntry->d_name, ".c")){
addString(fileNameBuffer, ICON_C_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".lua")){
addString(fileNameBuffer, ICON_LUA_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".h")){
addString(fileNameBuffer, ICON_H_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".sh")){
addString(fileNameBuffer, ICON_SH_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".pdf")){
addString(fileNameBuffer, ICON_PDF_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".md")){
addString(fileNameBuffer, ICON_MARKDOWN_FILE);
} else if (compareFileExtencion(curDirEntry->d_name, ".tar.gz")){
addString(fileNameBuffer, ICON_COMPRESS_FILE);
} else {
addString(fileNameBuffer, ICON_GENERIC);
}
} else if (curDirEntry->d_type == DT_LNK){
addString(fileNameBuffer, ICON_SIM_LINK);
} else {
addString(fileNameBuffer, ICON_GENERIC);
}
addChar(fileNameBuffer, ' ');
test = addString(fileNameBuffer, curDirEntry->d_name);
addToMenu(menu, fileNameBuffer->buffer, curDirEntryColor, curDirEntry);
}
}
}
int selectionCallBack(Menu *menu){
struct MenuItem curItem = menu->itemBuffer->items[menu->itemBuffer->curSelectedItem];
struct _DirEntry *curDirEntry = (struct _DirEntry *)curItem.data;
fflush(stdout);
if (curDirEntry->d_type == DT_DIR){
return END_LOOP;
} else {
return 1;
}
}
int main(int argc, char *argv[]){
struct DynamicCharArray *path = initDynamicCharArray(50);
struct DynamicCharArray *fileNameBuffer = initDynamicCharArray(50);
int rv = addString(path, argv[1]);
struct TermWin *win = initTermWin();
setRawMode(win);
printf("\n");
int *cursorPosition = requestCursorPosition();
Menu *menu = initMenu(15, 40, cursorPosition);
char *loopSelection;
copyColorToBuffer(menu->paint->borderColor, Mauve);
copyColorToBuffer(menu->setings.backGroundcolor, Blue);
copyColorToBuffer(menu->setings.forgroundColor, Base);
displayMenu(menu);
int (*pEndFunction)(Menu *menu) = selectionCallBack;
fflush(stdout);
char c;
int continueMenu = 1;
int jumps = 0;
int lastItemLength = 1;
while (1){
DIR *directory = opendir(path->buffer);
if (directory == NULL){
perror("Error: Could Not Open The Directory");
return 1;
}
addDirectoryEntriesToMenu(menu, directory, fileNameBuffer);
displayMenu(menu);
continueMenu = selectionLoop(menu, pEndFunction);
if (continueMenu == 0){
break;
}
addChar(path, '/');
struct _DirEntry *selectedDirInfo = (struct _DirEntry *)menu->itemBuffer->items[menu->itemBuffer->curSelectedItem].data;
loopSelection = selectedDirInfo->d_name;
if (loopSelection[0] == '.' && loopSelection[1]){
if (jumps == 0){
addString(path, loopSelection);
} else {
delNChars(path, lastItemLength);
}
} else {
addString(path, loopSelection);
}
lastItemLength = strlen(loopSelection);
restarMenuItems(menu);
fflush(stdout);
closedir(directory);
}
moveCursorToCoordinate(menu->paint->corner[0]+menu->paint->height+2, 0);
endTermWin(win);
FILE *file = fopen("/home/narval/programing/petit_proyects/betterCd/temp/output.txt", "w");
if (file == NULL){
printf("file could not open\n");
return 1;
}
fprintf(file, "%s", path->buffer);
fclose(file);
return 0;
}