Initial commit

Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:06:52 -05:00
commit db95c6f0d0
Signed by: 0xf8
GPG Key ID: 446580D758689584
4 changed files with 323 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
main.o
tempus

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c))
HEADERS = $(wildcard src/*.h)
CC = clang
FLAGS = -g -std=c17 -fsanitize=address
LIB = -lncurses
%.o: src/%.c $(HEADERS)
$(CC) $(FLAGS) -c $< -o $@
tempus: $(OBJ)
$(CC) $(LIB) $(FLAGS) $^ -o $@
.PHONY: clean
clean:
rm $(OBJ)

264
src/main.c Normal file
View File

@ -0,0 +1,264 @@
#include "./main.h"
#include <curses.h>
#include <stdlib.h>
#define true 1
#define false 0
enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 40000000};
void cleanup() {
endwin();
}
uint mimicks[10][5][3] = {
// 0
{ { 1, 1, 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 },
{ 1, 1, 1 } },
// 2
{ { 1, 1, 1 },
{ 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 },
{ 1, 1, 1 } },
// 3
{ { 1, 1, 1 },
{ 0, 0, 1 },
{ 1, 1, 1 },
{ 0, 0, 1 },
{ 1, 1, 1 } },
// 4
{ { 1, 0, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 },
{ 0, 0, 1 },
{ 0, 0, 1 } },
// 5
{ { 1, 1, 1 },
{ 1, 0, 0 },
{ 1, 1, 1 },
{ 0, 0, 1 },
{ 1, 1, 1 } },
// 6
{ { 1, 1, 1 },
{ 1, 0, 0 },
{ 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 } },
// 7
{ { 1, 1, 1 },
{ 0, 0, 1 },
{ 0, 1, 1 },
{ 0, 1, 0 },
{ 0, 1, 0 } },
// 8
{ { 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 } },
// 9
{ { 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 },
{ 0, 0, 1 },
{ 1, 1, 1 } }
};
int main(int argc, char **argv) {
// tests
struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP};
WINDOW *w = initscr();
int rows = LINES - 1;
int cols = COLS - 1;
int midr = rows/2;
int midc = cols/2;
atexit(cleanup);
start_color();
init_pair(1,8,0);
init_pair(2,7,0);
cbreak();
noecho();
clear();
// TODO: add framelimiting
while (true) {
time_t *t = (time_t *)malloc(sizeof(time_t) + 1);
time(t);
struct tm *lc = localtime(t);
// hourul:minul:sec
int hour = lc->tm_hour;
int hourl = hour % 10;
int houru = (hour - hourl) / 10;
int min = lc->tm_min;
int minl = min % 10;
int minu = (min - minl) / 10;
int sec = lc->tm_sec;
// print_number
// #hour
int sy = 0, sx = 0;
struct bignumber_t *bn_houru = bignumber(00, 0, houru);
struct bignumber_t *bn_hourl = bignumber(15, 1, hourl);
struct bignumber_t *bn_minu = bignumber(30, 2, minu);
struct bignumber_t *bn_minl = bignumber(45, 3, minl);
clear();
attron(A_DIM);
mvaddch(midr + 0, midc, '|');
mvaddch(midr + 1, midc, '|');
mvaddch(midr - 1, midc, '|');
attroff(A_DIM);
drawbignumber(bn_houru, sec, midc - 19, midr - 2);
drawbignumber(bn_hourl, sec, midc - 9, midr - 2);
drawbignumber(bn_minu, sec, midc + 2, midr - 2);
drawbignumber(bn_minl, sec, midc + 12, midr - 2);
free(bn_houru);
free(bn_hourl);
free(bn_minu);
free(bn_minl);
refresh();
free(t);
nanosleep(&request, &remaining);
}
return 0;
}
struct bignumber_t *bignumber(int start, uint index, uint mimick) {
struct bignumber_t *bn = (struct bignumber_t *)malloc(sizeof(struct bignumber_t) + 1);
uint i = start;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 5; y++)
bn->matrix[y][x] = i, i++;
bn->index = index;
bn->mimick = mimick;
return bn;
}
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 5; y++) {
move(starty + y, startx + (x * 3));
struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2);
uint currentSec = (uint)sec == bn->matrix[y][x];
uint partOfMimick = mimicks[bn->mimick][y][x];
if (partOfMimick) attron(A_BOLD);
else attron(COLOR_PAIR(1));
if (currentSec) attron(A_REVERSE),attron(A_BOLD),attron(COLOR_PAIR(2));
addstr(padded_num->s);
attroff(A_BOLD);
attroff(A_REVERSE);
attroff(COLOR_PAIR(1));
attroff(COLOR_PAIR(2));
free_string(padded_num);
}
}
}
// pad_uint
struct string *pad_int(int n, uint pad, uint maxsize) {
struct string *str = (struct string *)malloc(sizeof (struct string) + 1);
maxsize++;
str->s = (char *)malloc((sizeof(char) * maxsize) + 1);
str->length = 0;
memset(str->s, 0, maxsize);
// Figure out how many place values are in n
int _n = abs(n);
uint places = 1;
for (uint i = 0; i < pad; i++) {
if (_n >= 10) {
_n /= 10;
places++;
}
else break;
}
char *padding = (char *)malloc((sizeof(char) * pad) + 2);
for (uint i = 0; i < pad + 1; i++)
padding[i] = '0';
uint required_padding = pad - places;
if (required_padding > pad) required_padding = 0;
// Negative value
if (n < 0) {
required_padding++;
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;
}
}
return str;
}
// alloc_string
struct string *alloc_string(uint l) {
struct string *s = (struct string *)malloc(sizeof(struct string) + 1);
s->s = (char *)malloc(l + 1);
s->length = l;
memset(s->s, 0, l);
return s;
}
// free_string
void free_string(struct string *s) {
free(s->s);
free(s);
}

41
src/main.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef __MAIN_H__
#define __MAIN_H__
#include <math.h>
#include <memory.h>
#include <ncurses.h>
#include <time.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef unsigned long size;
typedef unsigned int uint;
struct string {
char *s;
size length;
};
struct bignumber_t {
uint matrix[5][3];
uint mimick;
uint index;
};
struct string *alloc_string(uint l);
void free_string(struct string *s);
// pads a number with 0's
struct string *pad_int(int n, uint pad, uint maxsize);
// this creates a bignumber_t
struct bignumber_t *bignumber(int start, uint index, uint mimick);
// this draws a big number
void drawbignumber(struct bignumber_t *, int sec, int startx, int starty);
#endif // __MAIN_H__