Update 0.4

Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:06:54 -05:00
parent d9526039eb
commit a57b0371b1
Signed by: 0xf8
GPG Key ID: 446580D758689584
2 changed files with 192 additions and 195 deletions

View File

@ -16,93 +16,90 @@ You should have received a copy of the GNU General Public License along with thi
#include <notcurses/nckeys.h> #include <notcurses/nckeys.h>
#include <notcurses/notcurses.h> #include <notcurses/notcurses.h>
#define true 1
#define false 0
enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000}; enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000};
struct notcurses *_nc; struct notcurses *_nc;
void cleanup() { void cleanup() {
notcurses_stop(_nc); notcurses_stop(_nc);
} }
uint mimicks[10][5][3] = { uint mimicks[10][5][3] = {
// 0 // 0
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 1 // 1
{ { 1, 1, 0 }, { { 1, 1, 0 },
{ 0, 1, 0 }, { 0, 1, 0 },
{ 0, 1, 0 }, { 0, 1, 0 },
{ 0, 1, 0 }, { 0, 1, 0 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 2 // 2
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 1, 0, 0 }, { 1, 0, 0 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 3 // 3
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 4 // 4
{ { 1, 0, 1 }, { { 1, 0, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 0, 0, 1 } }, { 0, 0, 1 } },
// 5 // 5
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 1, 0, 0 }, { 1, 0, 0 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 6 // 6
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 1, 0, 0 }, { 1, 0, 0 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 7 // 7
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 0, 1, 1 }, { 0, 1, 1 },
{ 0, 1, 0 }, { 0, 1, 0 },
{ 0, 1, 0 } }, { 0, 1, 0 } },
// 8 // 8
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 } }, { 1, 1, 1 } },
// 9 // 9
{ { 1, 1, 1 }, { { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },
{ 0, 0, 1 }, { 0, 0, 1 },
{ 1, 1, 1 } } { 1, 1, 1 } }
}; };
int rows; int rows;
int cols; int cols;
int midr; int midr;
int midc; int midc;
uint stop = false; uint stop = 0;
void *notcurses_thread(void *_) { void *notcurses_thread(void *_) {
uint32_t input = 0; uint32_t input = 0;
@ -110,7 +107,7 @@ void *notcurses_thread(void *_) {
while ((input = notcurses_get(_nc, 0, &ni)) != (uint32_t)-1) { while ((input = notcurses_get(_nc, 0, &ni)) != (uint32_t)-1) {
if (ni.evtype == NCTYPE_RELEASE) continue; if (ni.evtype == NCTYPE_RELEASE) continue;
if (input == 'q') { stop = true; break; } if (input == 'q') { stop = 1; break; }
if (ni.id == NCKEY_RESIZE) { if (ni.id == NCKEY_RESIZE) {
unsigned dimx, dimy, oldx, oldy; unsigned dimx, dimy, oldx, oldy;
@ -129,186 +126,186 @@ void *notcurses_thread(void *_) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP}; struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP};
setlocale(LC_ALL, "C"); setlocale(LC_ALL, "C");
struct notcurses* nc = notcurses_init(NULL, stdout); struct notcurses* nc = notcurses_init(NULL, stdout);
_nc = nc; _nc = nc;
notcurses_enter_alternate_screen(nc); notcurses_enter_alternate_screen(nc);
atexit(cleanup); atexit(cleanup);
rows = LINES - 1; rows = LINES - 1;
cols = COLS - 1; cols = COLS - 1;
midr = rows / 2; midr = rows / 2;
midc = cols / 2; midc = cols / 2;
cbreak(); cbreak();
noecho(); noecho();
pthread_t thread_id; pthread_t thread_id;
pthread_create(&thread_id, NULL, notcurses_thread, NULL); pthread_create(&thread_id, NULL, notcurses_thread, NULL);
while (!stop) { while (!stop) {
time_t *t = (time_t *)malloc(sizeof(time_t) + 1); time_t *t = (time_t *)malloc(sizeof(time_t) + 1);
time(t); time(t);
struct tm *lc = localtime(t); struct tm *lc = localtime(t);
// hourul:minul:sec // hourul:minul:sec
int hour = lc->tm_hour; int hour = lc->tm_hour;
int hourl = hour % 10; int hourl = hour % 10;
int houru = (hour - hourl) / 10; int houru = (hour - hourl) / 10;
int min = lc->tm_min; int min = lc->tm_min;
int minl = min % 10; int minl = min % 10;
int minu = (min - minl) / 10; int minu = (min - minl) / 10;
int sec = lc->tm_sec; int sec = lc->tm_sec;
ncplane_erase(notcurses_stdplane(nc)); ncplane_erase(notcurses_stdplane(nc));
ncplane_set_fg_rgb(notcurses_stdplane(nc), 0x777777); ncplane_set_fg_rgb(notcurses_stdplane(nc), 0x777777);
ncplane_putchar_yx(notcurses_stdplane(nc), midr - 1, midc, '|'); ncplane_putchar_yx(notcurses_stdplane(nc), midr - 1, midc, '|');
ncplane_putchar_yx(notcurses_stdplane(nc), midr + 0, midc, '|'); ncplane_putchar_yx(notcurses_stdplane(nc), midr + 0, midc, '|');
ncplane_putchar_yx(notcurses_stdplane(nc), midr + 1, midc, '|'); ncplane_putchar_yx(notcurses_stdplane(nc), midr + 1, midc, '|');
struct bignumber_t *bn_houru = bignumber(00, houru); struct bignumber_t *bn_houru = bignumber(00, houru);
drawbignumber(bn_houru, sec, midc - 20, midr - 2, notcurses_stdplane(nc)); drawbignumber(bn_houru, sec, midc - 20, midr - 2, notcurses_stdplane(nc));
free(bn_houru); free(bn_houru);
struct bignumber_t *bn_hourl = bignumber(15, hourl); struct bignumber_t *bn_hourl = bignumber(15, hourl);
drawbignumber(bn_hourl, sec, midc - 10, midr - 2, notcurses_stdplane(nc)); drawbignumber(bn_hourl, sec, midc - 10, midr - 2, notcurses_stdplane(nc));
free(bn_hourl); free(bn_hourl);
struct bignumber_t *bn_minu = bignumber(30, minu); struct bignumber_t *bn_minu = bignumber(30, minu);
drawbignumber(bn_minu, sec, midc + 3, midr - 2, notcurses_stdplane(nc)); drawbignumber(bn_minu, sec, midc + 3, midr - 2, notcurses_stdplane(nc));
free(bn_minu); free(bn_minu);
struct bignumber_t *bn_minl = bignumber(45, minl); struct bignumber_t *bn_minl = bignumber(45, minl);
drawbignumber(bn_minl, sec, midc + 13, midr - 2, notcurses_stdplane(nc)); drawbignumber(bn_minl, sec, midc + 13, midr - 2, notcurses_stdplane(nc));
free(bn_minl); free(bn_minl);
refresh(); refresh();
notcurses_render(nc); notcurses_render(nc);
free(t); free(t);
nanosleep(&request, &remaining); nanosleep(&request, &remaining);
} }
pthread_kill(thread_id, SIGKILL); pthread_kill(thread_id, SIGKILL);
return 0; return 0;
} }
struct bignumber_t *bignumber(int start, uint mimick) { struct bignumber_t *bignumber(int start, uint mimick) {
struct bignumber_t *bn = (struct bignumber_t *)malloc(sizeof(struct bignumber_t) + 1); struct bignumber_t *bn = (struct bignumber_t *)malloc(sizeof(struct bignumber_t) + 1);
uint i = start; uint i = start;
for (int x = 0; x < 3; x++) for (int x = 0; x < 3; x++)
for (int y = 0; y < 5; y++) for (int y = 0; y < 5; y++)
bn->matrix[y][x] = i, i++; bn->matrix[y][x] = i, i++;
bn->mimick = mimick;
return bn; bn->mimick = mimick;
return bn;
} }
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) { void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) {
for (int x = 0; x < 3; x++) { for (int x = 0; x < 3; x++) {
for (int y = 0; y < 5; y++) { for (int y = 0; y < 5; y++) {
struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2); struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2);
uint currentSec = (uint)sec == bn->matrix[y][x]; uint currentSec = (uint)sec == bn->matrix[y][x];
uint partOfMimick = mimicks[bn->mimick][y][x]; uint partOfMimick = mimicks[bn->mimick][y][x];
ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT);
ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE);
uint32_t bg = ncplane_bg_rgb(plane); uint32_t bg = ncplane_bg_rgb(plane);
uint32_t fg = ncplane_fg_rgb(plane); uint32_t fg = ncplane_fg_rgb(plane);
if (partOfMimick) ncplane_set_fg_rgb(plane, 0xCCCCCC), ncplane_set_styles(plane, 0x0002u); if (partOfMimick) ncplane_set_fg_rgb(plane, 0xCCCCCC), ncplane_set_styles(plane, 0x0002u);
else ncplane_set_fg_rgb(plane, 0x555555); else ncplane_set_fg_rgb(plane, 0x555555);
if (currentSec) ncplane_set_bg_rgb(plane, 0xFFFFFF), ncplane_set_fg_rgb(plane, 0x333333), ncplane_set_bg_alpha(plane, NCALPHA_OPAQUE); if (currentSec) ncplane_set_bg_rgb(plane, 0xFFFFFF), ncplane_set_fg_rgb(plane, 0x333333), ncplane_set_bg_alpha(plane, NCALPHA_OPAQUE);
ncplane_putstr_yx(plane, starty + y, startx + (x * 3), padded_num->s); ncplane_putstr_yx(plane, starty + y, startx + (x * 3), padded_num->s);
ncplane_set_styles(plane, 0x0000); ncplane_set_styles(plane, 0x0000);
ncplane_set_bg_rgb(plane, bg); ncplane_set_bg_rgb(plane, bg);
ncplane_set_fg_rgb(plane, fg); ncplane_set_fg_rgb(plane, fg);
ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT); ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT);
ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE); ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE);
free_string(padded_num); free_string(padded_num);
}
} }
}
} }
// pad_uint // pad_uint
struct string *pad_int(int n, uint pad, uint maxsize) { struct string *pad_int(int n, uint pad, uint maxsize) {
struct string *str = (struct string *)malloc(sizeof (struct string) + 1); struct string *str = (struct string *)malloc(sizeof (struct string) + 1);
maxsize++; maxsize++;
str->s = (char *)malloc((sizeof(char) * maxsize) + 1); str->s = (char *)malloc((sizeof(char) * maxsize) + 1);
str->length = 0; str->length = 0;
memset(str->s, 0, maxsize); memset(str->s, 0, maxsize);
// Figure out how many place values are in n // Figure out how many place values are in n
int _n = abs(n); int _n = abs(n);
uint places = 1; uint places = 1;
for (uint i = 0; i < pad; i++) { for (uint i = 0; i < pad; i++) {
if (_n >= 10) { if (_n >= 10) {
_n /= 10; _n /= 10;
places++; places++;
}
else break;
} }
else break;
}
char *padding = (char *)malloc((sizeof(char) * pad) + 2); char *padding = (char *)malloc((sizeof(char) * pad) + 2);
for (uint i = 0; i < pad + 1; i++) for (uint i = 0; i < pad + 1; i++)
padding[i] = '0'; padding[i] = '0';
uint required_padding = pad - places; uint required_padding = pad - places;
if (required_padding > pad) required_padding = 0; if (required_padding > pad) required_padding = 0;
// Negative value // Negative value
if (n < 0) { if (n < 0) {
required_padding++; required_padding++;
padding[0] = '-'; padding[0] = '-';
}
snprintf(str->s, maxsize, "%*.*s%i%c", 0, required_padding, padding, abs(n), 0);
free(padding);
for (uint i = 0; i < maxsize + 1; i++) {
if (str->s[i] == 0) {
str->length = i;
break;
} }
}
snprintf(str->s, maxsize, "%*.*s%i%c", 0, required_padding, padding, abs(n), 0); return str;
free(padding);
for (uint i = 0; i < maxsize + 1; i++) {
if (str->s[i] == 0) {
str->length = i;
break;
}
}
return str;
} }
// alloc_string // alloc_string
struct string *alloc_string(uint l) { struct string *alloc_string(uint l) {
struct string *s = (struct string *)malloc(sizeof(struct string) + 1); struct string *s = (struct string *)malloc(sizeof(struct string) + 1);
s->s = (char *)malloc(l + 1); s->s = (char *)malloc(l + 1);
s->length = l; s->length = l;
memset(s->s, 0, l); memset(s->s, 0, l);
return s; return s;
} }
// free_string // free_string
void free_string(struct string *s) { void free_string(struct string *s) {
free(s->s); free(s->s);
free(s); free(s);
} }

View File

@ -31,13 +31,13 @@ typedef unsigned long size;
typedef unsigned int uint; typedef unsigned int uint;
struct string { struct string {
char *s; char *s;
size length; size length;
}; };
struct bignumber_t { struct bignumber_t {
uint matrix[5][3]; uint matrix[5][3];
uint mimick; uint mimick;
}; };