Seperated chart from box; fixed kkr segfault.

This commit is contained in:
2025-10-18 19:21:22 +02:00
parent c7f218da30
commit 65a1c9f159
8 changed files with 80 additions and 43 deletions

35
box.c
View File

@@ -73,7 +73,6 @@ draw_rectangle(double fx, double fy, double fwidth, double fheight)
static void static void
draw_chart(struct box *chart, double x, double y, double width, double height) draw_chart(struct box *chart, double x, double y, double width, double height)
{ {
char c = '@';
long int actual_diff; long int actual_diff;
double diff, price; double diff, price;
enum ui_color color; enum ui_color color;
@@ -89,23 +88,11 @@ draw_chart(struct box *chart, double x, double y, double width, double height)
actual_diff = 0; actual_diff = 0;
for (size_t i = 0; i < width; i++) { for (size_t i = 0; i < width; i++) {
diff = (double)rand() / RAND_MAX - 0.6; price = chart->chart->prices[i];
actual_diff = (size_t)(height + y - price)
- (size_t)(height + y - (price + diff));
if (actual_diff > 0) { ui_color_on(UI_BLUE);
color = UI_GREEN; ui_char(x + i, height + y - price, '@');
} else if (actual_diff == 0) { ui_color_off(UI_BLUE);
color = UI_NONE;
} else if (actual_diff < 0) {
color = UI_RED;
}
ui_color_on(color);
ui_char(x + i, height + y - price, c);
ui_color_off(color);
price += diff;
} }
} }
@@ -163,16 +150,16 @@ draw_container(struct box *b, double x, double y, double width, double height)
struct box * struct box *
new_chart_box(void) new_chart_box(struct chart *chart)
{ {
struct box *chart = malloc(sizeof(struct box)); struct box *box = malloc(sizeof(struct box));
chart->type = BOX_CHART; box->type = BOX_CHART;
chart->prices = emalloc(sizeof(double) * 100); box->chart = chart;
chart->fills = 1; box->fills = 1;
chart->color = UI_NONE; box->color = UI_NONE;
return chart; return box;
} }

20
box.h
View File

@@ -8,6 +8,7 @@
#pragma once #pragma once
#include "chart.h"
#include "ui.h" #include "ui.h"
@@ -27,18 +28,13 @@ struct box {
/* button */ /* button */
char *name; char *name;
/* chart and vertical/horizontal box */
struct {
/* chart and vertical/horizontal box */
size_t length;
union {
/* vertical/horizontal box */
struct box **children;
/* chart */ /* chart */
double *prices; struct chart *chart;
};
/* vertical/horizontal */
struct {
size_t length;
struct box **children;
}; };
}; };
}; };
@@ -47,7 +43,7 @@ struct box {
struct box *new_button_box(char *name); struct box *new_button_box(char *name);
struct box *new_vertical_box(void); struct box *new_vertical_box(void);
struct box *new_horizontal_box(void); struct box *new_horizontal_box(void);
struct box *new_chart_box(void); struct box *new_chart_box(struct chart *);
void append_box(struct box *parent, struct box *child); void append_box(struct box *parent, struct box *child);

32
chart.c Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) Artsiom D.
* Copyright (C) shit-co.de
*/
#include <stdlib.h>
#include "chart.h"
#include "util.h"
struct chart new_chart(unsigned int length)
{
unsigned int i;
double price;
struct chart chart;
chart.prices = emalloc(sizeof(double) * length);
chart.length = length;
/* populate with random prices */
price = 10;
for (i = 0; i < length; i++) {
chart.prices[i] = price;
price += (double)rand() / RAND_MAX - 0.4;
}
return chart;
}

17
chart.h Normal file
View File

@@ -0,0 +1,17 @@
/*
* Copyright (C) Artsiom D.
* Copyright (C) shit-co.de
*/
#pragma once
struct chart {
double *prices;
unsigned int length;
};
struct chart new_chart(unsigned int length);

9
main.c
View File

@@ -16,7 +16,9 @@
int int
main(void) main(void)
{ {
struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, *title, *desc; struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam,
*title, *desc;
struct chart chart;
ui_init(); ui_init();
@@ -27,7 +29,8 @@ main(void)
append_box(top, title); append_box(top, title);
append_box(top, desc); append_box(top, desc);
left = new_chart_box(); chart = new_chart(400);
left = new_chart_box(&chart);
left->fills = 5; left->fills = 5;
buy = new_button_box("buy"); buy = new_button_box("buy");
@@ -51,7 +54,7 @@ main(void)
append_box(ui, top); append_box(ui, top);
append_box(ui, bottom); append_box(ui, bottom);
draw_box(ui, 0, 0, ui_width()-1, ui_height()-1); draw_box(ui, 0, 0, ui_width(), ui_height());
ui_refresh(); ui_refresh();

View File

@@ -1,11 +1,12 @@
CFLAGS = -std=c23 -Wextra -pedantic -Wall -pedantic CFLAGS = -std=c23 -Wextra -pedantic -Wall -O0 -g3 -fsanitize=address
LDFLAGS = -lncurses LDFLAGS = -lncurses -fsanitize=address
main: box.o main.o ui.o util.o main: box.o main.o ui.o util.o chart.o
.PHONY: clean .PHONY: clean
box.o: box.c box.o: box.c
chart.o: chart.c
main.o: main.c main.o: main.c
ui.o: ui.c ui.o: ui.c
util.o: util.c util.o: util.c

2
util.c
View File

@@ -23,7 +23,7 @@ emalloc(size_t n)
} }
void noreturn void
die(char *fmt, ...) die(char *fmt, ...)
{ {
va_list va; va_list va;

3
util.h
View File

@@ -8,7 +8,8 @@
#pragma once #pragma once
#include <stddef.h> #include <stddef.h>
#include <stdnoreturn.h>
void *emalloc(size_t n); void *emalloc(size_t n);
void die(char *fmt, ...); noreturn void die(char *fmt, ...);