replaced confusing u64 joinded colors with struct TimStyle

This commit is contained in:
2026-01-09 09:45:00 +05:00
parent 161f655492
commit 1ce7090bd6
13 changed files with 285 additions and 181 deletions

View File

@@ -26,4 +26,5 @@ i32 main(void) {
exit(1);
}
}
return 0;
}

View File

@@ -67,4 +67,5 @@ i32 main(void) {
TEST(tim_utf8_is_wide_perhaps(U("")) == false);
TEST(tim_utf8_is_wide_perhaps(U("")) == true);
TEST(tim_utf8_is_wide_perhaps(U("𐍈")) == true);
return 0;
}

View File

@@ -1,120 +1,128 @@
#include "tim.h"
static inline void test_screen(TimEvent* e) {
static TimEvent me;
static TimEvent ke;
static i32 render_us;
char buf[64];
static TimEditState ed1;
static TimEditState ed2;
ke = (e->type == KEY_EVENT) ? *e : ke;
me = (e->type == MOUSE_EVENT) ? *e : me;
static inline void test_screen(TimEvent* e) {
static TimEvent me = {0};
static TimEvent ke = {0};
static i32 render_us = 0;
char buf[64] = {0};
if(e->type == TimEvent_Key)
ke = *e;
if(e->type == TimEvent_Mouse)
me = *e;
// positioning
tim_label("+", 0, 0, A, A, 0xf);
tim_label("+", ~0, 0, A, A, 0xf);
tim_label("+", 0, ~0, A, A, 0xf);
tim_label("+", ~0, ~0, A, A, 0xf);
tim_label("+", A, A, A, A, 0xf);
tim_label("-", 0, A, A, A, 0xf);
tim_label("-", ~0, A, A, A, 0xf);
tim_label("|", A, 0, A, A, 0xf);
tim_label("|", A, ~0, A, A, 0xf);
static TimStyle style_default = { .fg = TimColor16_White };
tim_label("+", 0, 0, A, A, style_default);
tim_label("+", ~0, 0, A, A, style_default);
tim_label("+", 0, ~0, A, A, style_default);
tim_label("+", ~0, ~0, A, A, style_default);
tim_label("+", A, A, A, A, style_default);
tim_label("-", 0, A, A, A, style_default);
tim_label("-", ~0, A, A, A, style_default);
tim_label("|", A, 0, A, A, style_default);
tim_label("|", A, ~0, A, A, style_default);
// some information
sprintf(buf, "screen: %dx%d", tim->w, tim->h);
tim_label(buf, 2, 0, A, A, 0xf);
tim_label(buf, 2, 0, A, A, style_default);
sprintf(buf, "frame : [%c] %d", ": "[tim->frame & 1], tim->frame);
tim_label(buf, 2, 1, A, A, 0xf);
tim_label(buf, 2, 1, A, A, style_default);
sprintf(buf, "key : [%d] %s", ke.key, ke.s + (ke.key < 32));
tim_label(buf, 2, 2, A, A, 0xf);
tim_label(buf, 2, 2, A, A, style_default);
sprintf(buf, "mouse : [%d] %d:%d", me.key, me.x, me.y);
tim_label(buf, 2, 3, A, A, 0xf);
tim_label(buf, 2, 3, A, A, style_default);
sprintf(buf, "input : %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
e->s[0], e->s[1], e->s[2], e->s[3], e->s[4], e->s[5], e->s[6], e->s[7]);
tim_label(buf, 2, 4, A, A, 0xf);
tim_label(buf, 2, 4, A, A, style_default);
// lower right
render_us += tim->render_us;
sprintf(buf, "%d µs (Ø %d µs)", tim->render_us, render_us / MAX(tim->frame, 1));
tim_label(buf, ~2, ~2, A, A, 0xf);
tim_label(buf, ~2, ~2, A, A, style_default);
sprintf(buf, "%d cells (%.0f %%)", tim->w * tim->h, 100.0 * tim->w * tim->h / TIM_MAX_CELLS);
tim_label(buf, ~2, ~1, A, A, 0xf);
tim_label(buf, ~2, ~1, A, A, style_default);
sprintf(buf, "%d bytes (%.0f %%)", tim->buf_size, 100.0 * tim->buf_size / TIM_MAX_BUF);
tim_label(buf, ~2, ~0, A, A, 0xf);
tim_label(buf, ~2, ~0, A, A, style_default);
// multi line label
tim_label("multi\nliñe\nlabël", 24, 1, A, A, 0xf);
tim_label("multi\nliñe\nlabël", 24, 1, A, A, style_default);
// colors
tim_scope(1, 5, 16, 5) {
tim_frame(0, 0, ~0, ~0, 0xf);
tim_label(" Red ", 1, 1, 7, A, 0x0900);
tim_label(" ", 8, 1, 7, A, 0xc400);
tim_label(" Green ", 1, 2, 7, A, 0x0a00);
tim_label(" ", 8, 2, 7, A, 0x2e00);
tim_label(" Blue ", 1, 3, 7, A, 0x0c00);
tim_label(" ", 8, 3, 7, A, 0x1500);
tim_frame(0, 0, ~0, ~0, style_default);
tim_label(" Red ", 1, 1, 7, A, (TimStyle){ .bg = 0x09 });
tim_label(" ", 8, 1, 7, A, (TimStyle){ .bg = 0xc4 });
tim_label(" Green ", 1, 2, 7, A, (TimStyle){ .bg = 0x0a });
tim_label(" ", 8, 2, 7, A, (TimStyle){ .bg = 0x2e });
tim_label(" Blue ", 1, 3, 7, A, (TimStyle){ .bg = 0x0c });
tim_label(" ", 8, 3, 7, A, (TimStyle){ .bg = 0x15 });
}
// button
static u64 bc = 0x100;
if (tim_button("Click Me", 17, 5, 16, 5, bc)) {
bc = (bc + 0x100) & 0xff00;
static TimStyle style_button = { .bg = 0x01 };
if (tim_button("Click Me", 17, 5, 16, 5, style_button)) {
style_button.bg = (style_button.bg + 1) & 0xff;
}
// edit
static TimEditState ed1;
static TimEditState ed2;
TimEditState_init(&ed1, 32, "Edit 1");
TimEditState_init(&ed2, 32, "");
tim_edit(&ed1, 1, 10, 32, 0xff00ff);
static TimStyle style_edit = { .brd = 0xff, .fg = 0xff };
tim_edit(&ed1, 1, 10, 32, style_edit);
sprintf(buf, "cursor: %d length: %d", ed1.cursor, ed1.length);
tim_label(buf, 2, 13, A, A, 0xf);
tim_edit(&ed2, 1, 14, 32, 0xff00ff);
tim_label(ed2.s, 2, 17, A, A, 0xf);
tim_label(buf, 2, 13, A, A, style_default);
tim_edit(&ed2, 1, 14, 32, style_edit);
tim_label(ed2.s, 2, 17, A, A, style_default);
// checkbox
static TimStyle style_checkbox = { .brd = TimColor16_Cyan, .fg = TimColor16_White };
static i32 chk[2] = {-1, 1};
tim_checkbox("Check 1", &chk[0], 1, 18, A, 0xa000f);
tim_checkbox("Check 2", &chk[1], 14, 18, A, 0xa000f);
tim_checkbox("Check 1", &chk[0], 1, 18, A, style_checkbox);
tim_checkbox("Check 2", &chk[1], 14, 18, A, style_checkbox);
// radiobox
static i32 rad = 0;
tim_radiobutton("Radio 1", &rad, 1, 1, 19, A, 0xa000f);
tim_radiobutton("Radio 2", &rad, 2, 14, 19, A, 0xa000f);
tim_radiobutton("Radio 3", &rad, 3, 1, 20, A, 0xa000f);
tim_radiobutton("Radio 4", &rad, 4, 14, 20, A, 0xa000f);
tim_radiobutton("Radio 1", &rad, 1, 1, 19, A, style_checkbox);
tim_radiobutton("Radio 2", &rad, 2, 14, 19, A, style_checkbox);
tim_radiobutton("Radio 3", &rad, 3, 1, 20, A, style_checkbox);
tim_radiobutton("Radio 4", &rad, 4, 14, 20, A, style_checkbox);
// scope nesting
tim_scope(~1, 1, 20, 10) {
tim_scope(0, 0, 10, 5) {
tim_frame(0, 0, ~0, ~0, 0x9);
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Red });
}
tim_scope(~0, 0, 10, 5) {
tim_frame(0, 0, ~0, ~0, 0xa);
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Green });
}
tim_scope(~0, ~0, 10, 5) {
tim_frame(0, 0, ~0, ~0, 0xb);
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Yellow });
}
tim_scope(0, ~0, 10, 5) {
tim_frame(0, 0, ~0, ~0, 0xc);
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Blue });
}
}
// funny characters
static TimStyle style_funny = { .bg = TimColor16_White, .fg = TimColor16_Magenta };
tim_scope(~1, ~3, 11, 5) {
tim_frame(0, 0, ~0, ~0, 0xf);
tim_label("123456789", 1, 1, 9, A, 0x0f05);
tim_label("$£ह€𐍈6789", 1, 2, A, A, 0x0f05);
tim_label("圍棋56789", 1, 3, A, A, 0x0f05);
tim_frame(0, 0, ~0, ~0, style_default);
tim_label("123456789", 1, 1, 9, A, style_funny);
tim_label("$£ह€𐍈6789", 1, 2, A, A, style_funny);
tim_label("圍棋56789", 1, 3, A, A, style_funny);
}
}
i32 main(void) {
TimEditState_init(&ed1, 32, "Edit 1");
TimEditState_init(&ed2, 32, "");
while (tim_run(1.5)) {
test_screen(&tim->event);
if (tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
break;
}
}
return 0;
}

View File

@@ -66,4 +66,5 @@ i32 main(i32 argc, char** argv) {
tim_reset_terminal();
fclose(f);
return 0;
}