Skip to content

Commit 414180e

Browse files
godknowsiamgoodfrysee
authored andcommitted
minarch: show save timestamp in preview
1 parent 80a7fd9 commit 414180e

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

workspace/all/common/utils.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <math.h>
88
#include <ctype.h>
99
#include <sys/time.h>
10+
#include <sys/stat.h>
1011
#include "defines.h"
1112
#include "utils.h"
1213

@@ -415,6 +416,13 @@ void trimSortingMeta(char** str) { // eg. `001) `
415416
int exists(char* path) {
416417
return access(path, F_OK)==0;
417418
}
419+
int getFileMTime(char* path, time_t* out) {
420+
if (!path || !out) return 0;
421+
struct stat st;
422+
if (stat(path, &st) != 0) return 0;
423+
*out = st.st_mtime;
424+
return 1;
425+
}
418426
void touch(char* path) {
419427
close(open(path, O_RDWR|O_CREAT, 0777));
420428
}
@@ -514,4 +522,4 @@ int clamp(int x, int lower, int upper)
514522
double clampd(double x, double lower, double upper)
515523
{
516524
return min(upper, max(x, lower));
517-
}
525+
}

workspace/all/common/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stddef.h>
55
#include <stdint.h>
66
#include <stdbool.h>
7+
#include <time.h>
78

89
int prefixMatch(char* pre, const char* str);
910
int suffixMatch(char* suf,const char* str);
@@ -34,6 +35,7 @@ void trimTrailingNewlines(char* line);
3435
void trimSortingMeta(char** str);
3536

3637
int exists(char* path);
38+
int getFileMTime(char* path, time_t* out);
3739
void touch(char* path);
3840
int toggle(char *path); // creates or removes file
3941
void putFile(char *path, char *contents);

workspace/all/minarch/minarch.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5077,12 +5077,14 @@ static struct {
50775077
int slot;
50785078
int save_exists;
50795079
int preview_exists;
5080+
time_t save_mtime;
50805081
} menu = {
50815082
.bitmap = NULL,
50825083
.disc = -1,
50835084
.total_discs = 0,
50845085
.save_exists = 0,
50855086
.preview_exists = 0,
5087+
.save_mtime = 0,
50865088

50875089
.items = {
50885090
[ITEM_CONT] = "Continue",
@@ -6558,6 +6560,7 @@ static void Menu_initState(void) {
65586560

65596561
menu.save_exists = 0;
65606562
menu.preview_exists = 0;
6563+
menu.save_mtime = 0;
65616564
}
65626565
static void Menu_updateState(void) {
65636566
// LOG_info("Menu_updateState\n");
@@ -6576,11 +6579,26 @@ static void Menu_updateState(void) {
65766579

65776580
menu.save_exists = exists(save_path);
65786581
menu.preview_exists = menu.save_exists && exists(menu.bmp_path);
6582+
menu.save_mtime = 0;
6583+
if (menu.save_exists) {
6584+
time_t mtime = 0;
6585+
if (getFileMTime(save_path, &mtime)) {
6586+
menu.save_mtime = mtime;
6587+
}
6588+
}
65796589

65806590
// LOG_info("save_path: %s (%i)\n", save_path, menu.save_exists);
65816591
// LOG_info("bmp_path: %s txt_path: %s (%i)\n", menu.bmp_path, menu.txt_path, menu.preview_exists);
65826592
}
65836593

6594+
static int Menu_formatSaveTime(char *out, size_t out_size, time_t when) {
6595+
if (!out || out_size == 0 || when == 0) return 0;
6596+
struct tm tm = *localtime(&when);
6597+
if (CFG_getClock24H())
6598+
return strftime(out, out_size, "%Y-%m-%d %H:%M", &tm) > 0;
6599+
return strftime(out, out_size, "%Y-%m-%d %-I:%M %p", &tm) > 0;
6600+
}
6601+
65846602
typedef struct {
65856603
char* pixels;
65866604
char* path;
@@ -6999,6 +7017,31 @@ static void Menu_loop(void) {
69997017
else GFX_blitMessage(font.large, "Empty Slot", screen, &preview_rect);
70007018
}
70017019

7020+
// save time overlay
7021+
if (menu.save_exists && menu.save_mtime) {
7022+
char save_time[32];
7023+
if (Menu_formatSaveTime(save_time, sizeof(save_time), menu.save_mtime)) {
7024+
int tw = 0;
7025+
int th = 0;
7026+
TTF_SizeUTF8(font.tiny, save_time, &tw, &th);
7027+
int pad_x = SCALE1(6);
7028+
int pad_y = SCALE1(4);
7029+
int pill_h = SCALE1(PILL_SIZE);
7030+
int pill_w = tw + pad_x * 2;
7031+
if (pill_w < pill_h) pill_w = pill_h;
7032+
int tx = ox + SCALE1(6);
7033+
int ty = oy + hh - pill_h - SCALE1(6);
7034+
GFX_blitPillLight(ASSET_WHITE_PILL, screen, &(SDL_Rect){tx, ty, pill_w, pill_h});
7035+
SDL_Surface* time_text = TTF_RenderUTF8_Blended(font.tiny, save_time, uintToColour(THEME_COLOR1_255));
7036+
if (time_text) {
7037+
SDL_BlitSurface(time_text, NULL, screen, &(SDL_Rect){
7038+
tx + (pill_w - time_text->w) / 2,
7039+
ty + (pill_h - time_text->h) / 2
7040+
});
7041+
SDL_FreeSurface(time_text);
7042+
}
7043+
}
7044+
}
70027045
// pagination
70037046
ox += (pw-SCALE1(15*MENU_SLOT_COUNT))/2;
70047047
oy += hh+SCALE1(WINDOW_RADIUS);

0 commit comments

Comments
 (0)