Update 0.1

Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:06:53 -05:00
parent aa4872c215
commit 88435ebe15
Signed by: 0xf8
GPG Key ID: 446580D758689584
3 changed files with 114 additions and 63 deletions

View File

@ -1,8 +1,8 @@
OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c))
HEADERS = $(wildcard src/*.h)
CC = clang
FLAGS = -std=c17 -O2
LIB = -lncurses
FLAGS = -std=c17 -O2 -pthread
LIB = -lncurses -lnotcurses -lnotcurses-core
%.o: src/%.c $(HEADERS)
$(CC) $(FLAGS) -c $< -o $@

View File

@ -13,15 +13,17 @@ You should have received a copy of the GNU General Public License along with thi
#include "./main.h"
#include <curses.h>
#include <stdlib.h>
#include <notcurses/nckeys.h>
#include <notcurses/notcurses.h>
#define true 1
#define false 0
enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 40000000};
struct notcurses *_nc;
void cleanup() {
endwin();
notcurses_stop(_nc);
}
uint mimicks[10][5][3] = {
@ -96,72 +98,107 @@ uint mimicks[10][5][3] = {
{ 1, 1, 1 } }
};
int rows;
int cols;
int midr;
int midc;
uint stop = false;
void *notcurses_thread(void *_) {
uint32_t input = 0;
ncinput ni;
while ((input = notcurses_get(_nc, 0, &ni)) != (uint32_t)-1) {
if (ni.evtype == NCTYPE_RELEASE) continue;
if (input == 'q') { stop = true; break; }
if (ni.id == NCKEY_RESIZE) {
unsigned dimx, dimy, oldx, oldy;
notcurses_term_dim_yx(_nc, &dimy, &dimx);
ncplane_dim_yx(notcurses_stdplane(_nc), &oldy, &oldx);
notcurses_refresh(_nc, &dimy, &dimx);
rows = dimy - 1;
cols = dimx - 1;
midr = rows / 2;
midc = cols / 2;
}
}
return NULL;
}
int main(int argc, char **argv) {
// tests
struct timespec remaining, request = {SECS_TO_SLEEP, NSEC_TO_SLEEP};
initscr();
int rows = LINES - 1;
int cols = COLS - 1;
int midr = rows/2;
int midc = cols/2;
setlocale(LC_ALL, "C");
struct notcurses* nc = notcurses_init(NULL, stdout);
_nc = nc;
notcurses_enter_alternate_screen(nc);
atexit(cleanup);
start_color();
init_pair(1,8,0);
init_pair(2,7,0);
rows = LINES - 1;
cols = COLS - 1;
midr = rows / 2;
midc = cols / 2;
cbreak();
noecho();
clear();
pthread_t thread_id;
pthread_create(&thread_id, NULL, notcurses_thread, NULL);
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;
while (!stop) {
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 min = lc->tm_min;
int minl = min % 10;
int minu = (min - minl) / 10;
int sec = lc->tm_sec;
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);
int sec = lc->tm_sec;
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();
ncplane_erase(notcurses_stdplane(nc));
attron(A_DIM);
mvaddch(midr + 0, midc, '|');
mvaddch(midr + 1, midc, '|');
mvaddch(midr - 1, midc, '|');
attroff(A_DIM);
ncplane_set_fg_rgb(notcurses_stdplane(nc), 0x777777);
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 + 1, midc, '|');
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);
drawbignumber(bn_houru, sec, midc - 19, midr - 2, notcurses_stdplane(nc));
drawbignumber(bn_hourl, sec, midc - 9, midr - 2, notcurses_stdplane(nc));
drawbignumber(bn_minu, sec, midc + 2, midr - 2, notcurses_stdplane(nc));
drawbignumber(bn_minl, sec, midc + 12, midr - 2, notcurses_stdplane(nc));
free(bn_houru);
free(bn_hourl);
free(bn_minu);
free(bn_minl);
refresh();
refresh();
notcurses_render(nc);
free(t);
free(t);
nanosleep(&request, &remaining);
nanosleep(&request, &remaining);
}
pthread_kill(thread_id, SIGKILL);
return 0;
}
@ -179,26 +216,37 @@ struct bignumber_t *bignumber(int start, uint index, uint mimick) {
return bn;
}
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty) {
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) {
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));
ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT);
ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE);
if (currentSec) attron(A_REVERSE),attron(COLOR_PAIR(2));
uint32_t bg = ncplane_bg_rgb(plane);
uint32_t fg = ncplane_fg_rgb(plane);
if (partOfMimick) ncplane_set_fg_rgb(plane, 0xCCCCCC), ncplane_set_styles(plane, 0x0002u);
else ncplane_set_fg_rgb(plane, 0x555555);
addstr(padded_num->s);
if (currentSec) ncplane_set_bg_rgb(plane, 0xFFFFFF), ncplane_set_fg_rgb(plane, 0x333333), ncplane_set_bg_alpha(plane, NCALPHA_OPAQUE);
attroff(A_BOLD);
attroff(A_REVERSE);
attroff(COLOR_PAIR(1));
attroff(COLOR_PAIR(2));
ncplane_set_styles(plane, ncplane_styles(plane));
ncplane_putstr_yx(plane, starty + y, startx + (x * 3), padded_num->s);
ncplane_set_styles(plane, 0x0000);
ncplane_set_bg_rgb(plane, bg);
ncplane_set_fg_rgb(plane, fg);
ncplane_set_bg_alpha(plane, NCALPHA_TRANSPARENT);
ncplane_set_fg_alpha(plane, NCALPHA_OPAQUE);
// attroff(A_BOLD);
// attroff(A_REVERSE);
// attroff(COLOR_PAIR(1));
// attroff(COLOR_PAIR(2));
free_string(padded_num);
}

View File

@ -14,14 +14,17 @@ You should have received a copy of the GNU General Public License along with thi
#ifndef __MAIN_H__
#define __MAIN_H__
#include <locale.h>
#include <math.h>
#include <memory.h>
#include <ncurses.h>
#include <time.h>
// #include <ncurses.h>
#include <notcurses/notcurses.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
typedef unsigned long size;
@ -49,6 +52,6 @@ struct string *pad_int(int n, uint pad, uint maxsize);
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);
void drawbignumber(struct bignumber_t *, int sec, int startx, int starty, struct ncplane *place);
#endif // __MAIN_H__