just moved some code between files, behaviour unchanged

This commit is contained in:
Lexi Quinn 2023-03-07 20:41:12 +11:00
parent 16310e7c17
commit 577e88551d
4 changed files with 305 additions and 292 deletions

View File

@ -6,6 +6,17 @@ int maxcols = INT_MAX;
int colwidth = 10;
int in;
//UI
struct color bg = { 47, 53, 66};
struct color fg = {247, 248, 242};
struct color fade = {210, 210, 210};
struct color gold = {249, 255, 79};
struct color good = { 79, 255, 85};
struct color bad = {255, 79, 79};
int h, w;
bool compact = false;
bool dirty = false;
struct termios base;
void setBGColor(struct color c)
@ -135,3 +146,273 @@ void setMaxCols(int cols)
{
maxcols = cols;
}
void drawSegmentNames()
{
char *names[segCount];
for(int i = 0; i < segCount; i++) {
names[i] = segments[i].name;
}
drawColumn(names, segCount, 0, segCount);
}
//TODO: Fix up all this commented garbage
//Really the entire display system needs rethinking first but yea
void drawDeltaColumn(int column)
{
char *times[segCount];
for (int i = 0; i < segCount; i++) {
times[i] = calloc(1, COLSTRLEN);
int time = 0;
if (i == currSeg)
time = currentMS - pbrun[i].ms;
else if (i < currSeg)
time = segments[i].ms - pbrun[i].ms;
ftime(times[i], time, 1, true);
struct color col = {0};
if (time <= 0)
col = good;
else
col = bad;
if (i < currSeg)
drawCell(times[i], column, i + 6, col);
if (i == currSeg && time >= -5000)
drawCell(times[i], column, i + 6, col);
}
//drawColumn(times, segCount, column, currSeg);
//Use drawCell because we're doing colors.
//for (int i = 0; i < segCount; i++) {
// if (i <= currSeg)
// drawCell(times[i], column, i + 6, good);
//}
setFGColor(fg);
for (int i = 0; i < segCount; i++) {
free(times[i]);
}
}
//TODO: try to clean the branching up
void drawTimeColumn(int timeoption, int column)
{
char *times[segCount];
int drawEnd = currSeg;
for (int i = 0; i < segCount; i++) {
times[i] = calloc(1, COLSTRLEN);
int time = 0;
switch (timeoption) {
case 0:
time = pbrun[i].ms;
drawEnd = segCount;
break;
case 2:
if (i > 0 && i < currSeg)
time = segments[i].ms - segments[i - 1].ms;
else if (i > 0 && i == currSeg)
time = currentMS - segments[i - 1].ms;
else if (i == 0 && i == currSeg)
time = currentMS;
else
time = segments[i].ms;
break;
case 3:
if (i == currSeg)
time = currentMS;
else
time = segments[i].ms;
}
ftime(times[i], time, 1, false);
}
drawColumn(times, segCount, column, drawEnd);
for (int i = 0; i < segCount; i++) {
free(times[i]);
}
}
void drawNotif(char* text)
{
clock_gettime(CLOCK_REALTIME, &notif);
clearNotif();
leftPrint(maxrows, w, text);
}
void clearNotif()
{
leftPrint(maxrows, w, "\033[2K");
}
void toggleCompact()
{
compact = !compact;
//Clears the screen rather than dirtying it so the notif doesnt clear
clrScreen();
if (compact)
drawNotif("Compact mode enabled");
else
drawNotif("Compact mode disabled");
}
void drawDisplay()
{
if (dirty) {
clrScreen();
dirty = false;
}
rghtPrint(1, w, "Attempts");
char atmpt[10];
sprintf(atmpt, "%9d", attempts);
rghtPrint(2, w, atmpt);
cntrPrint(1, w / 2, w, gameTitle);
cntrPrint(2, w / 2, w, categoryTitle);
setFGColor(fade);
drawHLine(5, w);
printf("\033[5;3H");
if (hotkeys_enabled || compact)
printf("[");
if (hotkeys_enabled)
printf("h");
if (compact)
printf("c");
if (hotkeys_enabled || compact)
printf("]");
setFGColor(fg);
drawSegmentNames();
//TODO: The column names stuff has to be more dynamic, part of the
//drawColumn function probably
if (!compact) {
char cols[41];
sprintf(cols, "%10s%10s%10s%10s", "Delta", "Sgmt", "Time", "PB");
setFGColor(fade);
rghtPrint(4, w, cols);
setFGColor(fg);
drawTimeColumn(0, 1);
drawTimeColumn(3, 2);
drawTimeColumn(2, 3);
drawDeltaColumn(4);
} else {
char cols[21];
sprintf(cols, "%10s%10s", "Delta", "Time/PB");
setFGColor(fade);
rghtPrint(4, w, cols);
setFGColor(fg);
drawTimeColumn(0, 1);
drawTimeColumn(3, 1);
drawDeltaColumn(2);
}
setFGColor(fade);
drawHLine(segCount + 6, w);
setFGColor(fg);
ftime(currentTime, currentMS, 2, false);
rghtPrint(segCount + 7, w, currentTime);
fflush(stdout);
}
void resize(int i)
{
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
w = ws.ws_col;
h = ws.ws_row;
setMaxCols(w);
setMaxRows(h);
dirty = true;
}
void ftime(char *timestr, int rms, int decimals, bool sign)
{
if (decimals > 3 || decimals < 0)
decimals = 0;
int seconds = rms / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
//A few better formatted variables for displaying these numbers
int thr = rms % 1000;
int two = thr / 10;
int one = two / 10;
int s = seconds % 60;
int m = minutes % 60;
int h = hours;
int d = 0;
switch (decimals) {
case 1:
d = one;
break;
case 2:
d = two;
break;
case 3:
d = thr;
break;
}
char tformat[22];
int i = 0;
int decimalspace = decimals + (decimals != 0);
if (hours) {
tformat[i++] = '%';
if (sign)
tformat[i++] = '+';
tformat[i++] = (colwidth - 6 - decimalspace) + 48;
tformat[i++] = 'd';
tformat[i++] = ':';
}
if (minutes) {
tformat[i++] = '%';
if (sign && !hours)
tformat[i++] = '+';
if (hours) {
tformat[i++] = '0';
tformat[i++] = '2';
} else {
tformat[i++] = (colwidth - 3 - decimalspace) + 48;
}
tformat[i++] = 'd';
tformat[i++] = ':';
}
tformat[i++] = '%';
if (s != 0 && sign && !hours && !minutes)
tformat[i++] = '+';
if (minutes) {
tformat[i++] = '0';
tformat[i++] = '2';
} else {
//This value can push the resulting char out of the numbers
//section of the ascii table so we gotta clamp it
int n = colwidth - decimalspace + 48;
if (n >= 58)
n = 57;
tformat[i++] = n;
}
tformat[i++] = 'd';
if (decimals) {
tformat[i++] = '.';
tformat[i++] = '%';
tformat[i++] = '0';
tformat[i++] = decimals + 48;
tformat[i++] = 'd';
}
tformat[i] = 0;
if (hours) {
if (!decimals)
sprintf(timestr, tformat, h, abs(m), abs(s));
else
sprintf(timestr, tformat, h, abs(m), abs(s), abs(d));
} else if (minutes) {
if (!decimals)
sprintf(timestr, tformat, m, abs(s));
else
sprintf(timestr, tformat, m, abs(s), abs(d));
} else {
if (!decimals) {
sprintf(timestr, tformat, s);
} else {
sprintf(timestr, tformat, s, abs(d));
if (sign && s == 0 && d < 0)
timestr[COLSTRLEN - 4 - decimals] = '-';
if (sign && s == 0 && d >= 0)
timestr[COLSTRLEN - 4 - decimals] = '+';
}
}
}

View File

@ -7,11 +7,18 @@
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <time.h>
#include "timer.h"
#define COLSTRLEN 11
extern int maxrows;
extern int maxcols;
extern int colwidth;
extern int in;
extern bool dirty;
struct color {
int r;
@ -19,6 +26,9 @@ struct color {
int b;
};
extern struct color bg;
extern struct color fg;
void setBGColor(struct color);
void setFGColor(struct color);
void clrScreen();
@ -37,6 +47,14 @@ void drawRow(char **data, int count, int row);
void drawCell(char *data, int column, int row, struct color col);
void setMaxRows(int rows);
void setMaxCols(int cols);
void drawNotif();
void clearNotif();
void drawSegmentNames();
void drawTimeColumn();
void toggleCompact();
void drawDisplay();
void resize(int i);
void ftime(char *timestr, int rms, int decimals, bool sign);
#endif

View File

@ -1,24 +1,11 @@
#include "timer.h"
#define COLSTRLEN 11
//Timekeeping
struct timespec timestart, finish, notif;
int currentMS = 0;
int timeSave = 0;
bool timerActive = false;
//UI
struct color bg = { 47, 53, 66};
struct color fg = {247, 248, 242};
struct color fade = {210, 210, 210};
struct color gold = {249, 255, 79};
struct color good = { 79, 255, 85};
struct color bad = {255, 79, 79};
int h, w;
bool compact = false;
bool dirty = false;
//Run data
char *filepath;
char *configpath;
@ -58,7 +45,7 @@ void start()
clock_gettime(CLOCK_REALTIME, &timestart);
timerActive = true;
//Reset state of timer
dirty = true;
dirty = true; //find a way to put this in src/display.c
for(int i = 0; i < segCount; i++) {
segments[i].ms = 0;
segments[i].isSkipped = false;
@ -126,281 +113,11 @@ void skip()
stop();
}
void ftime(char *timestr, int rms, int decimals, bool sign)
{
if (decimals > 3 || decimals < 0)
decimals = 0;
int seconds = rms / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
//A few better formatted variables for displaying these numbers
int thr = rms % 1000;
int two = thr / 10;
int one = two / 10;
int s = seconds % 60;
int m = minutes % 60;
int h = hours;
int d = 0;
switch (decimals) {
case 1:
d = one;
break;
case 2:
d = two;
break;
case 3:
d = thr;
break;
}
char tformat[22];
int i = 0;
int decimalspace = decimals + (decimals != 0);
if (hours) {
tformat[i++] = '%';
if (sign)
tformat[i++] = '+';
tformat[i++] = (colwidth - 6 - decimalspace) + 48;
tformat[i++] = 'd';
tformat[i++] = ':';
}
if (minutes) {
tformat[i++] = '%';
if (sign && !hours)
tformat[i++] = '+';
if (hours) {
tformat[i++] = '0';
tformat[i++] = '2';
} else {
tformat[i++] = (colwidth - 3 - decimalspace) + 48;
}
tformat[i++] = 'd';
tformat[i++] = ':';
}
tformat[i++] = '%';
if (s != 0 && sign && !hours && !minutes)
tformat[i++] = '+';
if (minutes) {
tformat[i++] = '0';
tformat[i++] = '2';
} else {
//This value can push the resulting char out of the numbers
//section of the ascii table so we gotta clamp it
int n = colwidth - decimalspace + 48;
if (n >= 58)
n = 57;
tformat[i++] = n;
}
tformat[i++] = 'd';
if (decimals) {
tformat[i++] = '.';
tformat[i++] = '%';
tformat[i++] = '0';
tformat[i++] = decimals + 48;
tformat[i++] = 'd';
}
tformat[i] = 0;
if (hours) {
if (!decimals)
sprintf(timestr, tformat, h, abs(m), abs(s));
else
sprintf(timestr, tformat, h, abs(m), abs(s), abs(d));
} else if (minutes) {
if (!decimals)
sprintf(timestr, tformat, m, abs(s));
else
sprintf(timestr, tformat, m, abs(s), abs(d));
} else {
if (!decimals) {
sprintf(timestr, tformat, s);
} else {
sprintf(timestr, tformat, s, abs(d));
if (sign && s == 0 && d < 0)
timestr[COLSTRLEN - 4 - decimals] = '-';
if (sign && s == 0 && d >= 0)
timestr[COLSTRLEN - 4 - decimals] = '+';
}
}
}
int timespecToMS(struct timespec t)
{
return (t.tv_nsec / 1000000) + (t.tv_sec * 1000);
}
void drawSegmentNames()
{
char *names[segCount];
for(int i = 0; i < segCount; i++) {
names[i] = segments[i].name;
}
drawColumn(names, segCount, 0, segCount);
}
//TODO: Fix up all this commented garbage
//Really the entire display system needs rethinking first but yea
void drawDeltaColumn(int column)
{
char *times[segCount];
for (int i = 0; i < segCount; i++) {
times[i] = calloc(1, COLSTRLEN);
int time = 0;
if (i == currSeg)
time = currentMS - pbrun[i].ms;
else if (i < currSeg)
time = segments[i].ms - pbrun[i].ms;
ftime(times[i], time, 1, true);
struct color col = {0};
if (time <= 0)
col = good;
else
col = bad;
if (i < currSeg)
drawCell(times[i], column, i + 6, col);
if (i == currSeg && time >= -5000)
drawCell(times[i], column, i + 6, col);
}
//drawColumn(times, segCount, column, currSeg);
//Use drawCell because we're doing colors.
//for (int i = 0; i < segCount; i++) {
// if (i <= currSeg)
// drawCell(times[i], column, i + 6, good);
//}
setFGColor(fg);
for (int i = 0; i < segCount; i++) {
free(times[i]);
}
}
//TODO: try to clean the branching up
void drawTimeColumn(int timeoption, int column)
{
char *times[segCount];
int drawEnd = currSeg;
for (int i = 0; i < segCount; i++) {
times[i] = calloc(1, COLSTRLEN);
int time = 0;
switch (timeoption) {
case 0:
time = pbrun[i].ms;
drawEnd = segCount;
break;
case 2:
if (i > 0 && i < currSeg)
time = segments[i].ms - segments[i - 1].ms;
else if (i > 0 && i == currSeg)
time = currentMS - segments[i - 1].ms;
else if (i == 0 && i == currSeg)
time = currentMS;
else
time = segments[i].ms;
break;
case 3:
if (i == currSeg)
time = currentMS;
else
time = segments[i].ms;
}
ftime(times[i], time, 1, false);
}
drawColumn(times, segCount, column, drawEnd);
for (int i = 0; i < segCount; i++) {
free(times[i]);
}
}
void drawNotif(char* text)
{
clock_gettime(CLOCK_REALTIME, &notif);
clearNotif();
leftPrint(maxrows, w, text);
}
void clearNotif()
{
leftPrint(maxrows, w, "\033[2K");
}
void toggleCompact()
{
compact = !compact;
//Clears the screen rather than dirtying it so the notif doesnt clear
clrScreen();
if (compact)
drawNotif("Compact mode enabled");
else
drawNotif("Compact mode disabled");
}
void drawDisplay()
{
if (dirty) {
clrScreen();
dirty = false;
}
rghtPrint(1, w, "Attempts");
char atmpt[10];
sprintf(atmpt, "%9d", attempts);
rghtPrint(2, w, atmpt);
cntrPrint(1, w / 2, w, gameTitle);
cntrPrint(2, w / 2, w, categoryTitle);
setFGColor(fade);
drawHLine(5, w);
printf("\033[5;3H");
if (hotkeys_enabled || compact)
printf("[");
if (hotkeys_enabled)
printf("h");
if (compact)
printf("c");
if (hotkeys_enabled || compact)
printf("]");
setFGColor(fg);
drawSegmentNames();
//TODO: The column names stuff has to be more dynamic, part of the
//drawColumn function probably
if (!compact) {
char cols[41];
sprintf(cols, "%10s%10s%10s%10s", "Delta", "Sgmt", "Time", "PB");
setFGColor(fade);
rghtPrint(4, w, cols);
setFGColor(fg);
drawTimeColumn(0, 1);
drawTimeColumn(3, 2);
drawTimeColumn(2, 3);
drawDeltaColumn(4);
} else {
char cols[21];
sprintf(cols, "%10s%10s", "Delta", "Time/PB");
setFGColor(fade);
rghtPrint(4, w, cols);
setFGColor(fg);
drawTimeColumn(0, 1);
drawTimeColumn(3, 1);
drawDeltaColumn(2);
}
setFGColor(fade);
drawHLine(segCount + 6, w);
setFGColor(fg);
ftime(currentTime, currentMS, 2, false);
rghtPrint(segCount + 7, w, currentTime);
fflush(stdout);
}
void resize(int i)
{
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
w = ws.ws_col;
h = ws.ws_row;
setMaxCols(w);
setMaxRows(h);
dirty = true;
}
void calculatePB()
{
bool valid = false;

View File

@ -39,11 +39,16 @@ struct pastseg
extern char *gameTitle;
extern char *categoryTitle;
extern int currentMS;
extern int currSeg;
extern int segCount;
extern int attempts;
extern char currentTime[10];
extern struct segment *pbrun;
extern struct segment *bestsegs;
extern struct segment *wrrun;
extern struct segment *segments;
extern struct timespec notif;
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td);
void add_timespec(struct timespec t1, struct timespec t2, struct timespec* td);
@ -53,15 +58,7 @@ void split();
void tpause();
void unsplit();
void skip();
void ftime(char *timestr, int rms, int decimals, bool sign);
int timespecToMS(struct timespec t);
void drawNotif();
void clearNotif();
void drawSegmentNames();
void drawTimeColumn();
void toggleCompact();
void drawDisplay();
void resize(int i);
void calculatePB();
void loadConfig();
void saveConfig(cJSON *config);