-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
277 lines (234 loc) · 7.69 KB
/
reader.c
File metadata and controls
277 lines (234 loc) · 7.69 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
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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define MAX_FILES 10000
#define MAX_PATH 4096
#define MAX_LINE 512
typedef struct {
char name[MAX_PATH];
int is_dir;
} FileEntry;
struct termios orig_termios;
void disable_raw_mode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void enable_raw_mode() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disable_raw_mode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int get_key() {
char c;
if (read(STDIN_FILENO, &c, 1) != 1) return -1;
if (c == 27) {
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1) return 27;
if (read(STDIN_FILENO, &seq[1], 1) != 1) return 27;
if (seq[0] == '[') {
switch (seq[1]) {
case 'A': return 1000; // UP
case 'B': return 1001; // DOWN
case 'C': return 1002; // RIGHT
case 'D': return 1003; // LEFT
}
}
return 27;
}
return c;
}
void list_dir(const char *path, FileEntry *files, int *count) {
DIR *dir = opendir(path);
struct dirent *entry;
*count = 0;
if (!dir) {
printf("Cannot open directory: %s\r\n", path);
return;
}
while ((entry = readdir(dir)) != NULL && *count < MAX_FILES) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
strncpy(files[*count].name, entry->d_name, MAX_PATH - 1);
files[*count].name[MAX_PATH - 1] = '\0';
files[*count].is_dir = entry->d_type == DT_DIR;
(*count)++;
}
closedir(dir);
}
int get_terminal_height() {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
return 24;
}
return w.ws_row;
}
void read_file(const char *path) {
FILE *f = fopen(path, "r");
if (!f) {
printf("\033[2J\033[H");
printf("Cannot open file: %s\r\n", path);
printf("Press any key to go back...\r\n");
get_key();
return;
}
// Count total lines
int total_lines = 0;
char line[MAX_LINE];
while (fgets(line, sizeof(line), f)) {
total_lines++;
}
rewind(f);
if (total_lines == 0) {
printf("\033[2J\033[H");
printf("File is empty: %s\r\n", path);
printf("Press any key to go back...\r\n");
fclose(f);
get_key();
return;
}
// Allocate array of line pointers
char **lines = malloc(total_lines * sizeof(char*));
if (!lines) {
printf("\033[2J\033[H");
printf("Memory allocation failed!\r\n");
printf("Press any key to go back...\r\n");
fclose(f);
get_key();
return;
}
// Read all lines into memory
int line_count = 0;
while (fgets(line, sizeof(line), f) && line_count < total_lines) {
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
lines[line_count] = malloc(len + 1);
if (!lines[line_count]) {
printf("\033[2J\033[H");
printf("Memory allocation failed!\r\n");
for (int j = 0; j < line_count; j++) free(lines[j]);
free(lines);
fclose(f);
get_key();
return;
}
strcpy(lines[line_count], line);
line_count++;
}
fclose(f);
// Display with scrolling
int scroll_pos = 0;
int term_height = get_terminal_height();
int display_lines = term_height - 4;
while (1) {
printf("\033[2J\033[H");
printf("File: %s (lines %d-%d of %d)\r\n",
path, scroll_pos + 1,
scroll_pos + display_lines > line_count ? line_count : scroll_pos + display_lines,
line_count);
printf("─────────────────────────────────────\r\n");
for (int i = 0; i < display_lines && (scroll_pos + i) < line_count; i++) {
printf("%s\r\n", lines[scroll_pos + i]);
}
printf("─────────────────────────────────────\r\n");
printf("↑↓ scroll | Space/b page | g/G top/bottom | q quit\r\n");
int key = get_key();
if (key == 'q' || key == 'Q') break;
// Arrow key scrolling
if (key == 1000 && scroll_pos > 0) scroll_pos--;
if (key == 1001 && scroll_pos < total_lines - display_lines) scroll_pos++;
// Page down with space
if (key == ' ' && scroll_pos < total_lines - display_lines) {
scroll_pos += display_lines;
if (scroll_pos > total_lines - display_lines)
scroll_pos = total_lines - display_lines;
}
// Page up with 'b'
if (key == 'b' && scroll_pos > 0) {
scroll_pos -= display_lines;
if (scroll_pos < 0) scroll_pos = 0;
}
// Home/End
if (key == 'g') scroll_pos = 0;
if (key == 'G') {
scroll_pos = total_lines - display_lines;
if (scroll_pos < 0) scroll_pos = 0;
}
}
// Free all allocated memory
for (int j = 0; j < line_count; j++) {
free(lines[j]);
}
free(lines);
}
void browse(const char *path) {
// ALLOCATE ON HEAP, NOT STACK!
FileEntry *files = malloc(MAX_FILES * sizeof(FileEntry));
if (!files) {
printf("Memory allocation failed!\r\n");
return;
}
int count, selected = 0;
char fullpath[MAX_PATH];
while (1) {
list_dir(path, files, &count);
if (count == 0) {
printf("\033[2J\033[H");
printf("Directory: %s\r\n\n", path);
printf("(Empty directory)\r\n\n");
printf("Press 'q' to go back\r\n");
int key = get_key();
if (key == 'q' || key == 'Q') {
free(files);
return;
}
continue;
}
printf("\033[2J\033[H");
printf("Directory: %s (%d items)\r\n\n", path, count);
for (int i = 0; i < count; i++) {
if (i == selected) printf("> ");
else printf(" ");
printf("%s%s\r\n", files[i].name, files[i].is_dir ? "/" : "");
}
printf("\r\n↑↓ navigate | Enter select | q back\r\n");
int key = get_key();
if (key == 1000 && selected > 0) selected--;
if (key == 1001 && selected < count - 1) selected++;
if (key == 'q' || key == 'Q') {
free(files);
return;
}
if (key == '\r' || key == '\n' || key == 1002) {
int len = snprintf(fullpath, MAX_PATH, "%s/%s", path, files[selected].name);
if (len >= MAX_PATH) {
printf("\033[2J\033[H");
printf("Path too long!\r\n");
printf("Path: %s/%s\r\n", path, files[selected].name);
printf("Press any key to continue...\r\n");
get_key();
continue;
}
if (files[selected].is_dir) {
browse(fullpath);
selected = 0;
} else {
read_file(fullpath);
}
}
}
}
int main(int argc, char *argv[]) {
enable_raw_mode();
char *start_path = argc > 1 ? argv[1] : ".";
browse(start_path);
printf("\033[2J\033[H");
printf("Exiting.\r\n");
return 0;
}