-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecentfiles.c
395 lines (358 loc) · 9.1 KB
/
recentfiles.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
#include "les.h"
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct {
time_t opened;
time_t closed;
int line;
const char *name;
int new;
int deleted;
} recent_t;
recent_t *recents;
size_t recents_len = 0;
size_t recents_size = 0;
int recents_loaded = 0;
char *home;
recent_t *add_recent () {
if (recents_len == recents_size) {
if (recents_size == 0) {
recents_size = 16;
recents = malloc(recents_size * sizeof (recent_t));
}
else {
recents_size *= 2;
recents = realloc(recents, recents_size * sizeof (recent_t));
}
}
recents_len++;
recent_t *r = recents + recents_len - 1;
r->new = 0;
r->deleted = 0;
return r;
}
int recentscmp (const void *a, const void *b) {
const recent_t *a2 = a;
const recent_t *b2 = b;
return strcmp(a2->name, b2->name);
}
void delete_prior_entry (recent_t *r) {
int i;
for (i = recents_len - 2; i >= 0; i--) {
recent_t *r2 = recents + i;
if (strcmp(r2->name, r->name) == 0) {
r2->deleted = 1;
break;
}
}
}
void add_recent_tab (tab_t *tabb) {
if (!tabb->fd || !tabb->realpath) {
return;
}
recent_t *r = add_recent();
r->opened = tabb->opened;
r->closed = time(NULL);
r->line = tabb->line;
r->name = strdup(tabb->realpath);
r->new = 1;
delete_prior_entry(r);
}
char *recents_file () {
char *home = getenv("HOME");
if (!home) {
return NULL;
}
static char file[256];
snprintf(file, sizeof file, "%s/.les_recents", home);
return file;
}
void save_recents_file () {
load_recents_file();
int i;
for (i = 0; i < tabs_len; i++) {
add_recent_tab(tabs[i]);
}
char *file = recents_file();
if (!file) {
return;
}
FILE *fp = fopen(file, "w");
if (!fp) {
fprintf(stderr, "Couldn't open %s: %s\n", file, strerror(errno));
exit(1);
}
for (i = 0; i < recents_len; i++) {
recent_t *r = recents + i;
if (r->deleted) {
continue;
}
fprintf(fp, "%ld %ld %d %s\n", r->opened, r->closed, r->line, r->name);
}
fclose(fp);
}
int ws (char *str) {
int i;
for (i = 0; str[i]; i++) {
if (str[i] != ' ' && str[i] != '\t') {
break;
}
}
return i;
}
int digits (char *str) {
int i;
for (i = 0; str[i]; i++) {
if (str[i] < '0' || str[i] > '9') {
break;
}
}
return i;
}
int therest (char *str) {
int i;
for (i = strlen(str) - 1; i >= 0; i--) {
if (!(str[i] == ' ' || str[i] == '\t' || str[i] == '\n')) {
break;
}
}
return i + 1;
}
void parse_recent_files_line (char *str) {
int i = 0;
int len;
// optional leading whitespace
len = ws(str + i);
i += len;
// opened timestamp
char *opened = str + i;
int opened_len = digits(str + i);
if (!opened_len) {
return;
}
i += opened_len;
// whitespace
len = ws(str + i);
if (!len) {
return;
}
i += len;
// closed timestamp
char *closed = str + i;
int closed_len = digits(str + i);
if (!closed_len) {
return;
}
i += closed_len;
// whitespace
len = ws(str + i);
if (!len) {
return;
}
i += len;
// last line you were on
char *line = str + i;
int line_len = digits(str + i);
if (!line_len) {
return;
}
i += line_len;
// whitespace
len = ws(str + i);
if (!len) {
return;
}
i += len;
// file name
char *name = str + i;
int name_len = therest(str + i);
if (!name_len) {
return;
}
recent_t *r = add_recent();
opened[opened_len] = '\0';
r->opened = atoll(opened);
closed[closed_len] = '\0';
r->closed = atoll(closed);
line[line_len] ='\0';
r->line = atoi(line);
name[name_len] = '\0';
r->name = strdup(name);
}
// Returns the most recent line from the last time you opened this file
void get_last_line () {
if (!tabb->realpath) {
tabb->realpath = realpath(tabb->name, NULL);
}
if (!tabb->realpath) {
return;
}
if (!recents_loaded) {
load_recents_file();
}
int i;
for (i = recents_len - 1; i >= 0; i--) {
recent_t *r = recents + i;
if (strcmp(r->name, tabb->realpath) == 0) {
tabb->last_line = r->line;
return;
}
}
}
void load_recents_file () {
recents_loaded = 1;
recent_t *recents2 = recents;
size_t recents2_len = recents_len;
size_t recents2_size = recents_size;
recents = NULL;
recents_len = 0;
recents_size = 0;
char *file = recents_file();
if (!file) {
return;
}
FILE *fp = fopen(file, "r");
if (!fp) {
return;
}
char str[512];
while (fgets(str, sizeof str, fp)) {
parse_recent_files_line(str);
}
fclose(fp);
int i;
for (i = 0; i < recents2_len; i++) {
recent_t *r2 = recents2 + i;
if (!r2->new) {
continue;
}
recent_t *r = add_recent();
r->opened = r2->opened;
r->closed = r2->closed;
r->line = r2->line;
r->name = r2->name;
r->new = r2->new;
delete_prior_entry(r);
}
free(recents2);
}
void add_recents_tab_line (recent_t *r) {
int len = 0;
static char str[32];
if (r->new) {
len = snprintf(str, sizeof str, "* ");
}
struct tm *opened = localtime(&(r->opened));
len += strftime(str + len, sizeof str - len, "%Y-%m-%d %I:%M%p", opened);
len = snprintf(tabb->buf + tabb->buf_len, tabb->buf_size - tabb->buf_len, "%-18s ", str);
tabb->buf_len += len;
int dur = r->closed - r->opened;
if (dur < 60) {
len = snprintf(str, sizeof str, "%ds", dur);
}
else if (dur < 60 * 60) {
len = snprintf(str, sizeof str, "%dm", dur / 60);
}
else {
dur /= (60 * 60);
len = snprintf(str, sizeof str, "%dh", dur / (60 * 60));
}
const char *name = r->name;
char *hdir = "";
if (home) {
size_t home_len = strlen(home);
size_t name_len = strlen(name);
if (name_len > home_len + 1 && strncmp(name, home, home_len) == 0 && name[home_len] == '/') {
name += home_len + 1;
hdir = "~/";
}
}
len = snprintf(tabb->buf + tabb->buf_len, tabb->buf_size - tabb->buf_len, "%-3s line %-5d %s%s\n", str, r->line, hdir, name);
tabb->buf_len += len;
tabb->nlines++;
}
void add_recents_tab () {
int i;
if (tabb->state & RECENTS) {
close_tab();
return;
}
for (i = 0; i < tabs_len; i++) {
if (tabs[i]->state & RECENTS) {
current_tab = i;
tabb = tabs[current_tab];
change_tab();
draw_tab();
return;
}
}
static char str[32];
time_t today = time(NULL);
struct tm *todaytm = malloc(sizeof (struct tm));
localtime_r(&today, todaytm);
todaytm->tm_sec = 0;
todaytm->tm_min = 0;
todaytm->tm_hour = 0;
todaytm->tm_isdst = -1;
today = mktime(todaytm);
struct tm *daytm = malloc(sizeof (struct tm));
home = getenv("HOME");
add_tab("[Recent Files]", 0, LOADED|RECENTS|SPECIAL);
current_tab = tabs_len - 1;
tabb = tabs[current_tab];
if (!recents_loaded) {
load_recents_file();
}
int day_line = 8;
for (i = 0; i < recents_len; i++) {
recent_t *r = recents + i;
if (r->deleted) {
continue;
}
if (tabb->buf_len + 512 > tabb->buf_size) {
tabb->buf_size *= 2;
tabb->buf = realloc(tabb->buf, tabb->buf_size);
}
if (day_line) {
memcpy(daytm, todaytm, sizeof (struct tm));
daytm->tm_mday -= day_line - 1;
daytm->tm_isdst = -1;
time_t day = mktime(daytm);
if (r->opened >= day) {
day_line--;
while (1) {
daytm->tm_mday++;
daytm->tm_isdst = -1;
day = mktime(daytm);
if (r->opened < day) {
break;
}
day_line--;
}
memcpy(daytm, todaytm, sizeof (struct tm));
daytm->tm_mday -= day_line;
daytm->tm_isdst = -1;
day = mktime(daytm);
strftime(str, sizeof str, "%A", daytm);
char *day_nickname = "";
if (day_line == 0) {
day_nickname = " (Today)";
}
else if (day_line == 1) {
day_nickname = " (Yesterday)";
}
tabb->buf_len += snprintf(tabb->buf + tabb->buf_len, tabb->buf_size - tabb->buf_len, "--------- %s%s ---------\n", str, day_nickname);
tabb->nlines++;
}
}
add_recents_tab_line(r);
}
free(daytm);
free(todaytm);
init_line1();
change_tab();
move_end();
}