Compare commits

...

2 Commits

Author SHA1 Message Date
d417b5bbd5 fixed unicode input on windows 2026-01-09 11:12:34 +05:00
1ce7090bd6 replaced confusing u64 joinded colors with struct TimStyle 2026-01-09 09:45:00 +05:00
14 changed files with 286 additions and 186 deletions

BIN
256colors.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

View File

@@ -3,12 +3,6 @@
#include "tim.h"
// colors
#define CTXT 0xf // text black, white
#define CFR 0x8 // frame black, gray
#define CYES 0xa000f // yes green, black, white
#define CNO 0x9000f // no red, black white
i32 main(i32 argc, char** argv) {
if (argc < 2 || strcmp(argv[1], "-h") == 0) {
printf("syntax: %s message\n", argv[0]);
@@ -18,6 +12,11 @@ i32 main(i32 argc, char** argv) {
// get text properties
TimText msg = tim_scan_str(argv[1]);
TimStyle style_frame = { .brd = TimColor16_DarkGray };
TimStyle style_text = { .fg = TimColor16_White };
TimStyle style_yes = { .brd = TimColor16_Green, .fg = style_text.fg };
TimStyle style_no = { .brd = TimColor16_Red, .fg = style_yes.fg };
while (tim_run(0)) {
// calculate size of message box
i32 w = MAX(msg.width + 4, 28);
@@ -25,18 +24,18 @@ i32 main(i32 argc, char** argv) {
tim_scope(A, A, w, h) {
// draw frame around entire scope
tim_frame(0, 0, ~0, ~0, CFR);
tim_frame(0, 0, ~0, ~0, style_frame);
// draw message
tim_label(argv[1], A, 1, msg.width, msg.lines, CTXT);
tim_label(argv[1], A, 1, msg.width, msg.lines, style_text);
// draw 'yes' button, return 0 when clicked
if (tim_button("[y] Yes", 2, ~1, A, A, CYES) || tim_is_key_press('y')) {
if (tim_button("[y] Yes", 2, ~1, A, A, style_yes) || tim_is_key_press('y')) {
exit(0);
}
// draw 'no' button, return 1 when clicked
if (tim_button("[n] No", ~2, ~1, A, A, CNO) || tim_is_key_press('n')) {
if (tim_button("[n] No", ~2, ~1, A, A, style_no) || tim_is_key_press('n')) {
exit(1);
}
@@ -46,4 +45,5 @@ i32 main(i32 argc, char** argv) {
}
}
}
return 0;
}

View File

@@ -1,15 +1,20 @@
#include "tim.h"
i32 main(void) {
while (tim_run(0)) { // init state and start event loop
tim_scope(A, A, 24, 8) { // centered 24x8 scope
u64 c = 0x0a060f; // three colors
tim_frame(0, 0, ~0, ~0, c); // draw frame for scope
while (tim_run(0)) { // init state and start event loop
tim_scope(A, A, 24, 8) { // centered 24x8 scope
TimStyle c = { // three colors
.brd= TimColor16_Green,
.bg = TimColor16_DarkCyan,
.fg = TimColor16_White
};
tim_frame(0, 0, ~0, ~0, c); // draw frame for scope
tim_label("Greetings!", A, 2, A, A, c); // label in top center
if (tim_button("OK", A, ~1, 8, A, c)) // button in bottom center
return 0; // exit on button click
if (tim_is_key_press('q')) // ctrl-c is masked
return 0; // exit on 'q' press
return 0; // exit on button click
if (tim_is_key_press('q')) // ctrl-c is masked
return 0; // exit on 'q' press
}
} // atexit() cleanup
} // atexit() cleanup
return 0;
}

View File

@@ -6,7 +6,6 @@
#define FG 0x10
#define BG 0xdd
#define BTN (FG << 16 | BG << 8 | FG)
#define NEW 0
#define RUN 1
@@ -81,7 +80,7 @@ static void game(void) {
}
// user input
if (tim->event.type == KEY_EVENT) {
if (tim->event.type == TimEvent_Key) {
i32 key = tim->event.key;
if ((key == TimKey_Right || key == 'd') && snek.look.x != -1) {
snek.look = (point){{1, 0}};
@@ -99,17 +98,18 @@ static void game(void) {
}
static void menu(void) {
TimStyle style_button = (TimStyle){ .brd= FG, .bg = BG, .fg = FG };
tim_scope(A, A, 20, 13) {
char* lbl = snek.state == OVER ? "GAME OVER" : "SNEK - THE GAME";
char* btn = snek.state == PAUSE ? "Resume" : "Play";
tim_label(lbl, A, 0, A, A, BTN);
if (tim_button(btn, A, 2, 20, 5, BTN) || tim_is_key_press(TimKey_Enter)) {
tim_label(lbl, A, 0, A, A, style_button);
if (tim_button(btn, A, 2, 20, 5, style_button) || tim_is_key_press(TimKey_Enter)) {
if (snek.state != PAUSE) {
start();
}
snek.state = RUN;
}
if (tim_button("Exit", A, 8, 20, 5, BTN) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
if (tim_button("Exit", A, 8, 20, 5, style_button) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
exit(0);
}
}
@@ -126,6 +126,6 @@ i32 main(void) {
} else {
menu();
}
}
return 0;
}

View File

@@ -16,10 +16,6 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// fix windows.h name clash, coincidentally they have the same values
#undef TimEvent_Key // 0x0001
#undef TimEvent_Mouse // 0x0002
#ifdef _MSC_VER
// disable integer conversion warnings
#pragma warning(disable:4244)
@@ -60,42 +56,42 @@ extern "C" {
#pragma region constants
#define TIM_ENABLE_DBUF 1 // double buffering
#define TIM_MAX_SCOPE 20 // max scope nesting
#define TIM_MAX_SCOPE 32 // max scope nesting
#define TIM_MAX_CELLS 0x20000 // size of screen buffer
#define TIM_MAX_BUF (TIM_MAX_CELLS * 4) // size of output buffer
#define A INT_MAX // auto center / width / height
typedef enum TimEventType {
TimEvent_Void, // an event was consumed
TimEvent_Draw, // draw screen
TimEvent_Key, // a key was pressed
TimEvent_Mouse, // mouse button, scroll or move
} TimEventType;
enum {
TimKey_MouseButtonLeft = 1,
TimKey_Backspace = 8,
TimKey_Tab = 9,
TimKey_Enter = 13,
TimKey_Escape = 27,
TimKey_Insert = -1,
TimKey_Delete = -2,
TimKey_Home = -3,
TimKey_End = -4,
TimKey_PageUp = -5,
TimKey_PageDown = -6,
TimKey_Up = -7,
TimKey_Down = -8,
TimKey_Left = -9,
TimKey_Right = -10,
};
// key code or 32-bit unicode char
typedef i32 TimKey;
#pragma endregion
#pragma region types
/* first 16 colors from xterm256 supported by any terminal emulator */
enum {
TimColor16_Black = 0x00,
TimColor16_DarkRed = 0x01,
TimColor16_DarkGreen = 0x02,
TimColor16_DarkYellow = 0x03,
TimColor16_DarkBlue = 0x04,
TimColor16_DarkMagenta = 0x05,
TimColor16_DarkCyan = 0x06,
TimColor16_Gray = 0x07,
TimColor16_DarkGray = 0x08,
TimColor16_Red = 0x09,
TimColor16_Green = 0x0a,
TimColor16_Yellow = 0x0b,
TimColor16_Blue = 0x0c,
TimColor16_Magenta = 0x0d,
TimColor16_Cyan = 0x0e,
TimColor16_White = 0x0f,
};
typedef struct TimStyle {
u8 brd; // border
u8 bg; // background
u8 fg; // foreground
} TimStyle;
typedef struct TimCell {
u8 fg; // foreground color
u8 bg; // background color
@@ -124,6 +120,33 @@ typedef struct TimLine {
i32 width; // line width in glyph
} TimLine;
typedef enum TimEventType {
TimEvent_Void, // an event was consumed
TimEvent_Draw, // draw screen
TimEvent_Key, // a key was pressed
TimEvent_Mouse, // mouse button, scroll or move
} TimEventType;
enum {
TimKey_MouseButtonLeft = 1,
TimKey_Backspace = 8,
TimKey_Tab = 9,
TimKey_Enter = 13,
TimKey_Escape = 27,
TimKey_Insert = -1,
TimKey_Delete = -2,
TimKey_Home = -3,
TimKey_End = -4,
TimKey_PageUp = -5,
TimKey_PageDown = -6,
TimKey_Up = -7,
TimKey_Down = -8,
TimKey_Left = -9,
TimKey_Right = -10,
};
// key code or 32-bit unicode char
typedef i32 TimKey;
typedef struct TimEvent {
TimEventType type;
TimKey key; // used by TimEvent_Key and TimEvent_Mouse
@@ -133,9 +156,9 @@ typedef struct TimEvent {
} TimEvent;
typedef struct TimEditState {
i32 cursor; // cursor position (utf8)
i32 length; // string length (utf8)
i32 capacity; // buffer size
i32 cursor; // cursor position (utf8)
i32 length; // string length (utf8)
i32 capacity; // buffer size in bytes
char* s; // zero terminated buffer
} TimEditState;
@@ -195,9 +218,17 @@ i64 tim_time_usec(void);
#pragma region scope
// for some stupid reason gcc requires more than 3 levels of macros to concat _i_ with line number
#define _tim_cat2(A, B) A##B
#define _tim_scope_line(L) _tim_cat2(_i_, L)
#define _tim_scope_i _tim_scope_line(__LINE__)
// enter layout scope
#define tim_scope(x, y, w, h) \
for (i32 _i = tim_enter_scope((x), (y), (w), (h)); _i; _i = tim_exit_scope())
#define tim_scope(x, y, w, h) for (\
i32 _tim_scope_i = tim_enter_scope((x), (y), (w), (h)); \
_tim_scope_i;\
_tim_scope_i = tim_exit_scope()\
) /* here goes your { code } */
// convert relative (scoped) to absolute (screen) coordinates
TimRect tim_scope_rect_to_absolute(i32 x, i32 y, i32 w, i32 h);
@@ -217,35 +248,35 @@ i32 tim_exit_scope(void);
// frame
// color: background, frame
void tim_frame(i32 x, i32 y, i32 w, i32 h, u64 color);
void tim_frame(i32 x, i32 y, i32 w, i32 h, TimStyle style);
// text label
// str : text - supports multiple lines
// color: background, text
void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color);
void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, TimStyle style);
// button - returns true on click
// color: frame, background, text
bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color);
bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, TimStyle style);
// check box - returns true when clicked
// txt : text label
// state: persistent state, 0 unchecked, -1 semi checked, !0: checked
// color: check, background, text
bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color);
bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, TimStyle style);
// radio button - return true when clicked
// txt : text label
// state: persistent state, selected if *state == v
// v : value
// color: radio, background, text
bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color);
bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle style);
/// text edit - value in state
/// @param e persistent edit state, use TimEditState_init() to create new state
/// @param color frame, background, text
/// @return key id or 0
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color);
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
void TimEditState_init(TimEditState* e, i32 capacity, cstr initial_content);

View File

@@ -38,17 +38,19 @@ STATIC_LIB_FILE="$PROJECT.a"
# example: "-I./include -I$DEPENDENCIES_DIR/libexample"
INCLUDE="-Iinclude -Isrc"
EXEC_FILE=""
EXEC_EXT=""
# OS-specific options
case "$OS" in
WINDOWS)
EXEC_FILE="snek.exe"
EXEC_EXT=".exe"
SHARED_LIB_FILE="$PROJECT.dll"
INCLUDE="$INCLUDE "
# example: "-lSDL2 -lSDL2_image"
LINKER_LIBS=""
;;
LINUX)
EXEC_FILE="snek"
SHARED_LIB_FILE="$PROJECT.so"
INCLUDE="$INCLUDE "
LINKER_LIBS=""
@@ -60,26 +62,72 @@ esac
# TASKS
case "$TASK" in
# creates executable using profiling info if it exists
build_exec)
SRC_C="$SRC_C example/snek.c"
# -flto applies more optimizations across object files
# -flto=auto is needed to multithreaded copilation
# -fuse-linker-plugin is required to use static libs with lto
# -fprofile-use enables compiler to use profiling info files to optimize executable
# -fprofile-prefix-path sets path where profiling info about objects are be saved
# -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
# ./ask question?
ask)
EXEC_FILE="ask$EXEC_EXT"
SRC_C="example/ask.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT="@cbuild/default_tasks/strip_exec.sh"
POST_TASK_SCRIPT=""
;;
# creates executable with debug info and no optimizations
build_exec_dbg)
SRC_C="$SRC_C example/snek.c"
C_ARGS="-O0 -g3"
# hello world
hello)
EXEC_FILE="hello$EXEC_EXT"
SRC_C="example/hello.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT=""
;;
# snake game
snek)
EXEC_FILE="snek$EXEC_EXT"
SRC_C="example/snek.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT=""
;;
# test
test)
EXEC_FILE="test$EXEC_EXT"
SRC_C="test/test.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT=""
;;
# color
color)
EXEC_FILE="color$EXEC_EXT"
SRC_C="test/color.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT=""
;;
# string
string)
EXEC_FILE="string$EXEC_EXT"
SRC_C="test/string.c"
LINKER_LIBS="bin/tim.a"
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=""

View File

@@ -5,6 +5,26 @@ Fork of https://codeberg.org/chuvok/tim.h
tim is a portable library to create simple terminal applications
Demo video: https://asciinema.org/a/zn3p0dsVCOQOzwY1S9gDfyaxQ
## build
1. Clone this repository.
```
git clone https://timerix.ddns.net/git/Timerix/tim.git
```
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild/releases).
Select latest version compatible with the one in `project.config`.
Example: For `2.3.2` download latest `2.3.x`.
3. Build static library
```
cd tim
cbuild build_static_lib
```
4. Build tests and examples
```
cbuild ask hello snek test color string
```
## quick start
See [example/hello.c](./example/hello.c)
@@ -44,18 +64,10 @@ take the full available space from parent.
The layout automatically adopts to terminal window resize events.
## colors
Most elements have a uint64 color argument which holds up to eight colors.
Typically byte 0 is the text color and byte 1 is the background color.
For example 0x08040f encodes three colors. When used with a button the text
is white (0f), the background is blue (04), and the frame is gray (08).
The terminal should support xterm-256 colors. The TERM variable is ignored.
The lower 16 colors vary across different terminals, so the upper 240 colors
should be used if consistency is important.
xterm-256 color chart
https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg
Colors are stored as 8-bit values.
Most terminals support 16 basic colors. You can see them in TimColor16 enum.
There is also support for xterm-256 colors.
![xterm-256 color chart](./256colors.jpg)
## events
tim_run blocks until it observes an event. Mouse and key events are always
@@ -66,20 +78,13 @@ event when focused in order to prevent other key handlers on acting on them.
The current event is stored in tim->event.
event | cause
-------------|-----------------------
DRAW_EVENT | input, timeout, resize
KEY_EVENT | key press
MOUSE_EVENT | mouse click
VOID_EVENT | consumed event
## elements
frame (x, y, w, h, color)
Draw an empty frame and fill area.
x/y/w/h see layout documentation
color background, frame
style background, frame
label (str, x, y, w, h, color)
@@ -88,7 +93,7 @@ label (str, x, y, w, h, color)
str zero terminated string
x/y/w/h see layout documentation
color background, text
style background, text
button (str, x, y, w, h, color) -> bool
@@ -97,7 +102,7 @@ button (str, x, y, w, h, color) -> bool
str zero terminated string
x/y/w/h see layout documentation
color frame, background, text
style frame, background, text
edit (state, x, y, w, color) -> int
@@ -108,7 +113,7 @@ edit (state, x, y, w, color) -> int
state pointer to persistent edit state struct
x/y/w see layout documentation
color f rame, background, text
style f rame, background, text
check (str, state, x, y, w, color) -> bool
@@ -119,7 +124,7 @@ check (str, state, x, y, w, color) -> bool
str zero terminated string
state pointer to persistent state variable
x/y/w see layout documentation
color check, background, text
style check, background, text
radio (str, state, v, x, y, w, color) -> bool
@@ -131,7 +136,7 @@ radio (str, state, v, x, y, w, color) -> bool
state pointer to persistent state variable
v unique state value
x/y/w see layout documentation
color radio, background, text
style radio, background, text
## functions
tim_run (fps) -> bool

View File

@@ -85,18 +85,18 @@ static TimKey edit_event(TimEditState* e, TimRect r) {
return tim->event.key;
}
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color) {
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style) {
TimRect r = tim_scope_rect_to_absolute(x, y, w, 3);
if (tim->event.type == TimEvent_Draw) {
tim_draw_box(r.x, r.y, r.w, r.h, color >> 16, color >> 8);
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
if (tim->focus == e) {
char* s = e->s + tim_utf8_pos(e->s, e->cursor - r.w + 4);
i32 cur = MIN(r.w - 4, e->cursor);
tim_draw_str(s, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
tim_draw_str(s, r.x + 2, r.y + 1, r.w - 3, style.fg, style.bg);
tim_draw_invert(r.x + cur + 2, r.y + 1, 1);
} else {
tim_draw_str(e->s, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
tim_draw_str(e->s, r.x + 2, r.y + 1, r.w - 3, style.fg, style.bg);
}
}

View File

@@ -1,19 +1,19 @@
#include "tim.h"
void tim_frame(i32 x, i32 y, i32 w, i32 h, u64 color) {
void tim_frame(i32 x, i32 y, i32 w, i32 h, TimStyle style) {
if (tim->event.type == TimEvent_Draw) {
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
tim_draw_box(r.x, r.y, r.w, r.h, color, color >> 8);
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
}
}
void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color) {
void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
if (tim->event.type == TimEvent_Draw) {
TimText t = tim_scan_str(s);
w = (w == A) ? t.width : w;
h = (h == A) ? t.lines : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
TimCell c = tim_cell(" ", color, color >> 8);
TimCell c = tim_cell(" ", style.fg, style.bg);
tim_draw_lot(c, r.x, r.y, r.w, r.h);
TimLine l = {.s = s, .line = ""};
for (i32 i = 0; tim_next_line(&l); i++) {
@@ -22,28 +22,28 @@ void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color) {
}
}
bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color) {
bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
i32 tw = tim_utf8_len(txt);
w = (w == A) ? (tw + 4) : w;
h = (h == A) ? 3 : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
if (tim->event.type == TimEvent_Draw) {
tim_draw_box(r.x, r.y, r.w, r.h, color >> 16, color >> 8);
tim_draw_str(txt, r.x + (w - tw) / 2, r.y + h / 2, w, color, color >> 8);
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
tim_draw_str(txt, r.x + (w - tw) / 2, r.y + h / 2, w, style.fg, style.bg);
}
return tim_is_mouse_click_over(r);
}
bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color) {
bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, TimStyle style) {
w = (w == A) ? tim_utf8_len(txt) + 4 : w;
TimRect r = tim_scope_rect_to_absolute(x, y, w, 1);
if (tim->event.type == TimEvent_Draw) {
cstr st = *state == -1 ? "-" : *state ? "x" : " ";
tim_draw_str("[ ] ", r.x, r.y, 4, color, color >> 8);
tim_draw_str(st, r.x + 1, r.y, 1, color >> 16, color >> 8);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, color, color >> 8);
tim_draw_str("[ ] ", r.x, r.y, 4, style.fg, style.bg);
tim_draw_str(st, r.x + 1, r.y, 1, style.brd, style.bg);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, style.fg, style.bg);
}
bool click = tim_is_mouse_click_over(r);
@@ -52,15 +52,15 @@ bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color) {
}
bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color) {
bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle style) {
w = (w == A) ? tim_utf8_len(txt) + 4 : w;
TimRect r = tim_scope_rect_to_absolute(x, y, w, 1);
if (tim->event.type == TimEvent_Draw) {
cstr st = *state == v ? "o" : " ";
tim_draw_str("( ) ", r.x, r.y, 4, color, color >> 8);
tim_draw_str(st, r.x + 1, r.y, 1, color >> 16, color >> 8);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, color, color >> 8);
tim_draw_str("( ) ", r.x, r.y, 4, style.fg, style.bg);
tim_draw_str(st, r.x + 1, r.y, 1, style.brd, style.bg);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, style.fg, style.bg);
}
bool click = tim_is_mouse_click_over(r);

View File

@@ -113,7 +113,7 @@ void tim_read_event(i32 timeout_ms) {
// received input
INPUT_RECORD rec = {0};
DWORD n = 0;
ReadConsoleInput(h, &rec, 1, &n);
ReadConsoleInputW(h, &rec, 1, &n);
switch (rec.EventType) {
case KEY_EVENT: {

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;
}