diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-12-07 19:17:33 +0100 |
---|---|---|
committer | Raymaekers Luca <luca@spacehb.net> | 2025-01-07 18:51:21 +0100 |
commit | 0574f5a7c5159a2ae1d7d2182cec982509947db9 (patch) | |
tree | 2a9678fd39bcf8ead03c7ee979caf668f2f17892 /archived | |
parent | 4aec2e1331650cabb966bbf24ed83f76cbbe9912 (diff) |
checkpoint
Diffstat (limited to 'archived')
-rw-r--r-- | archived/input_box.c | 70 | ||||
-rw-r--r-- | archived/network_compression.c | 154 | ||||
-rw-r--r-- | archived/ui_checkmark.c | 87 | ||||
-rw-r--r-- | archived/ui_meter.c | 108 | ||||
-rw-r--r-- | archived/ui_selection.c | 71 | ||||
-rw-r--r-- | archived/wrap.c | 2 |
6 files changed, 491 insertions, 1 deletions
diff --git a/archived/input_box.c b/archived/input_box.c new file mode 100644 index 0000000..466c03d --- /dev/null +++ b/archived/input_box.c @@ -0,0 +1,70 @@ +#define Assert(expr) if (!(expr)) { \ + tb_shutdown(); \ + raise(SIGTRAP); \ +} + +#define TB_IMPL +#include "termbox2.h" +#undef TB_IMPL + +#define TEXTBOX_MAX_INPUT 255 + +#define UI_IMPL +#include "ui.h" + +#define ARENA_IMPL +#include "arena.h" + +#include <locale.h> + +int +main(void) +{ + struct tb_event ev = {0}; + + u32 InputLen = 0; + wchar_t Input[TEXTBOX_MAX_INPUT] = {0}; + rect TextBox = {0, 0, 32, 5}; + rect TextR = { + 2, 1, + TextBox.W - 2*TEXTBOX_PADDING_X - 2*TEXTBOX_BORDER_WIDTH, + TextBox.H - 2*TEXTBOX_BORDER_WIDTH + }; + // Used for scrolling the text. Text before TextOffset will not be printed. + u32 TextOffset = 0; + // Position in input based on cursor position. + u32 InputPos = 0; + + Assert(setlocale(LC_ALL, "")); + tb_init(); + bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]); + global.cursor_x = TextR.X; + global.cursor_y = TextR.Y; + + DrawBox(TextBox, 0); + + while (ev.key != TB_KEY_CTRL_C) + { + DrawTextBoxWrapped(TextR, Input + TextOffset, InputLen - TextOffset); + + InputPos = TextOffset + (global.cursor_x - TextR.X) + (global.cursor_y - TextR.Y) * TextR.W; + Assert(InputPos <= InputLen); + + tb_present(); + tb_poll_event(&ev); + + u32 Ret = TextBoxKeypress(ev, TextR, Input, &InputLen, InputPos, &TextOffset); + + u32 ShouldInsert = (!Ret) && (ev.ch && InputLen < TEXTBOX_MAX_INPUT); + // Insert new character in Input at InputPos + if (ShouldInsert) + { + TextBoxInsert(Input, InputPos, InputLen++, ev.ch); + TextBoxScrollRight(TextR, &TextOffset); + } + } + + + tb_shutdown(); + return 0; +} diff --git a/archived/network_compression.c b/archived/network_compression.c new file mode 100644 index 0000000..eef9e3c --- /dev/null +++ b/archived/network_compression.c @@ -0,0 +1,154 @@ +#include <strings.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <wchar.h> +#include <fcntl.h> +#include <assert.h> +#include <stddef.h> +#include <stdint.h> + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +size_t +GetMaximumCompressedOutputSize(size_t FileSize) +{ + // TODO: figure out equation + return FileSize*2 + 256; +} + +u32 +RLECompress(size_t InSize, u8* In, size_t MaxOutSize, u8* OutBase) +{ + u8* Out = OutBase; + +#define MAX_LITERAL_COUNT 255 +#define MAX_RUN_COUNT 255 + u32 LiteralCount = 0; + u8 Literals[MAX_LITERAL_COUNT] = {0}; + + u8 *InEnd = In + InSize; + while(In < InEnd) + { + u8 StartingValue = In[0]; + size_t Run = 1; // first one is the character itself + while((Run < (InEnd - In)) && + (Run < MAX_RUN_COUNT) && + (In[Run] == StartingValue)) + { + ++Run; + } + + if ((Run > 1) || + (LiteralCount == MAX_LITERAL_COUNT)) // stop doing runs when there is no + // space left in the buffer. + { + // Encode a literal/run pair + u8 LiteralCount8 = (u8)LiteralCount; + assert(LiteralCount8 == LiteralCount); + *Out++ = LiteralCount8; + + for(u32 LiteralIndex = 0; + LiteralIndex < LiteralCount; + ++LiteralIndex) + { + *Out++ = Literals[LiteralIndex]; + } + LiteralCount = 0; + + u8 Run8 = (u8)Run; + assert(Run8 == Run); + *Out++ = Run8; + + *Out++ = StartingValue; + + In += Run; + } + else + { + // Buffer literals, you have to because we are encoding them in pairs + Literals[LiteralCount++] = StartingValue; + ++In; + } + + } +#undef MAX_LITERAL_COUNT +#undef MAX_RUN_COUNT + + assert(In == InEnd); + + size_t OutSize = Out - OutBase; + assert(OutSize <= MaxOutSize); + + return OutSize; +} + +void +RLEDecompress(size_t InSize, u8* In, size_t OutSize, u8* Out) +{ + u8 *InEnd = In + InSize; + while(In < InEnd) + { + // TODO: I think Casey made a mistake and this should be an u8 + u8 LiteralCount = *In++; + while(LiteralCount--) + { + *Out++ = *In++; + } + + // Alternate to Run + u8 RepCount = *In++; + u8 RepValue = *In++; + while(RepCount--) + { + *Out++ = RepValue; + } + } + + assert(In == InEnd); +} + +int main(int Argc, char* Argv[]) { + + if (Argc < 2) + { + fprintf(stderr, "Usage: %s [compress|decompress]\n", Argv[0]); + return 1; + } + + char* Command = Argv[1]; + + if (!strcmp(Command, "compress")) + { + u32* Message = (u32*)L"abcabcbbbbbbb"; + size_t MessageSize = wcslen((const wchar_t*)Message) * 4; + + size_t OutBufferSize = GetMaximumCompressedOutputSize(MessageSize); + u8 OutBuffer[OutBufferSize + 8]; + bzero(OutBuffer, OutBufferSize + 8); + *(u32*)OutBuffer = MessageSize; + + size_t CompressedSize = RLECompress(MessageSize, (u8*)Message, OutBufferSize, OutBuffer + 8); + + s32 OutFile = open("test.compressed", O_WRONLY | O_CREAT, 0600); + + write(OutFile, OutBuffer, CompressedSize); + + fprintf(stdout, "%lu -> %lu bytes\n", MessageSize, CompressedSize); + } + else if (!strcmp(Command, "decompress")) + { + fprintf(stderr, "Not implemented yet.\n"); + } + else + fprintf(stderr, "Unknown command: '%s'\n", Command); + + return 0; +} diff --git a/archived/ui_checkmark.c b/archived/ui_checkmark.c new file mode 100644 index 0000000..df7e507 --- /dev/null +++ b/archived/ui_checkmark.c @@ -0,0 +1,87 @@ +#define TB_IMPL +#include "../chatty/external/termbox2.h" + +int +main(void) +{ + struct tb_event ev = {0}; + + typedef struct { + int X, Y; + int Checked; + } Checkmark; + +#define NUM_CHECKMARKS 4 + int Y = 0; + Checkmark Marks[NUM_CHECKMARKS] = { + {0, Y++, 0}, + {0, Y++, 0}, + {0, Y++, 1}, + {0, Y++, 0} + }; + Y++; + + int Selected = 0; + + tb_init(); + + int Quit = 0; + while (!Quit) + { + tb_clear(); + + for (int CheckmarkIndex = 0; + CheckmarkIndex < NUM_CHECKMARKS; + CheckmarkIndex++) + { + Checkmark Mark = Marks[CheckmarkIndex]; + if (Mark.Checked) + { + tb_printf(Mark.X, Mark.Y, 0, 0, "[x]"); + } + else + { + tb_printf(Mark.X, Mark.Y, 0, 0, "[ ]"); + } + } + Checkmark Mark = Marks[Selected]; + if (Mark.Checked) + { + tb_set_cell(Mark.X + 1, Mark.Y, L'x', TB_UNDERLINE, 0); + } + else + { + tb_set_cell(Mark.X + 1, Mark.Y, L' ', TB_UNDERLINE, 0); + } + + int BaseY = Y; + tb_printf(0, BaseY, TB_BOLD, 0, "j"); tb_printf(2, BaseY++, 0, 0, "next"); + tb_printf(0, BaseY, TB_BOLD, 0, "k"); tb_printf(2, BaseY++, 0, 0, "previous"); + tb_printf(0, BaseY, TB_BOLD, 0, "c"); tb_printf(2, BaseY++, 0, 0, "toggle"); + tb_printf(0, BaseY, TB_BOLD, 0, "q"); tb_printf(2, BaseY++, 0, 0, "quit"); + + tb_present(); + + tb_poll_event(&ev); + if (ev.ch == 'q') + { + Quit = 1; + } + else if (ev.ch == 'j') + { + if (Selected == NUM_CHECKMARKS - 1) Selected = 0; + else Selected++; + } + else if (ev.ch == 'k') + { + if (Selected) Selected--; + else Selected = NUM_CHECKMARKS - 1; + } + else if (ev.ch == 'c') + { + Marks[Selected].Checked = !Marks[Selected].Checked; + } + } + + tb_shutdown(); +} diff --git a/archived/ui_meter.c b/archived/ui_meter.c new file mode 100644 index 0000000..8636ba4 --- /dev/null +++ b/archived/ui_meter.c @@ -0,0 +1,108 @@ +#define TB_IMPL +#include "../chatty/external/termbox2.h" + +#include <locale.h> +#include <signal.h> +#include <unistd.h> +#include <sys/wait.h> + +#define Assert(expr) \ + if (!(expr)) \ + { \ + tb_shutdown(); \ + raise(SIGTRAP); \ + } + +#define METER_WIDTH 4 + +static char ***EnvironmentPath = 0; + +void +draw_meter(int X, int Y, int Height, int MaxLevel, int Level) +{ + int FillColor = 0; + + Height -= 2; // substract top and bottom border + + tb_printf(X, Y, 0, 0, "%ls", L"┌──┐"); + tb_printf(X, Y + Height + 1, 0, 0, "%ls", L"└ ┘"); + + if (Level == MaxLevel) + { + FillColor = TB_GREEN; + tb_printf(X + 1, Y + Height + 1, 0, 0, "00"); + } + else + { + FillColor = TB_CYAN; + tb_printf(X + 1, Y + Height + 1, 0, 0, "%02d", Level); + } + + int FilledLevel = (Level * Height)/MaxLevel; + + for (int LevelIndex = 0; LevelIndex < FilledLevel; LevelIndex++) + { + int YLevel = Y + (Height - LevelIndex); + tb_set_cell(X + 1, YLevel, L'▒', FillColor, FillColor); + tb_set_cell(X + 2, YLevel, L'▒', FillColor, FillColor); + } + + for (int YOffset = 0; YOffset < Height; YOffset++) + { + tb_set_cell(X + 0, Y + YOffset + 1, L'│', 0, 0); + tb_set_cell(X + 3, Y + YOffset + 1, L'│', 0, 0); + } + +} + +int +main(int Argc, char *Args[], char *Envp[]) +{ + EnvironmentPath = &Envp; + + struct tb_event ev = {0}; + Assert(setlocale(LC_ALL, "")); + int Quit = 0; + int Level = 0; + int LevelStep = 10; + int MeterMaxLevel = 100; + int MeterHeight = 10 + 2; + + tb_init(); + tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); + + while (!Quit) + { + tb_clear(); + + int Y = 0; + draw_meter(1, Y, MeterHeight, MeterMaxLevel, Level); + Y += MeterHeight + 1;; + + tb_print(0, Y, TB_BOLD, 0, "j"); tb_print(2, Y++, 0, 0, "decrease"); + tb_print(0, Y, TB_BOLD, 0, "k"); tb_print(2, Y++, 0, 0, "increase"); + tb_print(0, Y, TB_BOLD, 0, "q"); tb_print(2, Y++, 0, 0, "to quit"); + + tb_present(); + + tb_poll_event(&ev); + + if (ev.ch == 'q' || ev.key == TB_KEY_CTRL_C) Quit = 1; + else if ((ev.ch == 'j' || ev.key == TB_KEY_MOUSE_WHEEL_DOWN) && + Level > 0) + { + if (LevelStep > Level) Level = 0; + else Level -= LevelStep; + } + else if ((ev.ch == 'k' || ev.key == TB_KEY_MOUSE_WHEEL_UP) && + Level < MeterMaxLevel) + { + if (Level + LevelStep > MeterMaxLevel) Level = MeterMaxLevel; + else Level += LevelStep; + } + + } + + tb_shutdown(); + return 0; +} diff --git a/archived/ui_selection.c b/archived/ui_selection.c new file mode 100644 index 0000000..3e45ee2 --- /dev/null +++ b/archived/ui_selection.c @@ -0,0 +1,71 @@ +#define TB_IMPL +#include "../chatty/external/termbox2.h" + +#include <signal.h> + +#define Assert(expr) if (!(expr)) raise(SIGTRAP) + +typedef struct { + int X, Y; +} Position; + +int +main(void) +{ + int Selected = 0; + int NumChoices = 3; + int Y = 0; + + Position Positions[NumChoices]; + Positions[0] = (Position){1, Y}; + Positions[1] = (Position){5, Y}; + Positions[2] = (Position){9, Y}; + Y += 2; + + struct tb_event ev = {0}; + int Color = TB_GREEN; + + tb_init(); + + int Quit = 0; + while (!Quit) + { + tb_clear(); + + int BaseY = Y; + tb_printf(0, BaseY, TB_BOLD, 0, "j"); tb_printf(2, BaseY++, 0, 0, "select next"); + tb_printf(0, BaseY, TB_BOLD, 0, "k"); tb_printf(2, BaseY++, 0, 0, "select previous"); + tb_printf(0, BaseY, TB_BOLD, 0, "s"); tb_printf(2, BaseY++, 0, 0, "change color"); + tb_printf(0, BaseY, TB_BOLD, 0, "q"); tb_printf(2, BaseY++, 0, 0, "quit"); + + // Draw a box at position + for (int PositionsIndex = 0; PositionsIndex < NumChoices; PositionsIndex++) + { + Assert(Positions[PositionsIndex].X > 0); + tb_printf(Positions[PositionsIndex].X - 1, Positions[PositionsIndex].Y, Color, 0, "[ ]"); + } + // Draw mark in selected box + tb_printf(Positions[Selected].X, Positions[Selected].Y, Color, 0, "x"); + + tb_present(); + tb_poll_event(&ev); + + if (ev.ch == 'q') Quit = 1; + else if (ev.ch == 'j' && Selected < NumChoices - 1) Selected++; + else if (ev.ch == 'k' && Selected) Selected--; + else if (ev.ch == 's') + { + switch (Selected) + { + case 0: Color = TB_GREEN; break; + case 1: Color = TB_BLUE; break; + case 2: Color = TB_RED; break; + default: Assert(0); break; + } + } + } + + tb_shutdown(); + + return 0; +} diff --git a/archived/wrap.c b/archived/wrap.c index 57506b8..2317c6e 100644 --- a/archived/wrap.c +++ b/archived/wrap.c @@ -33,7 +33,7 @@ wrap(u8* Text, u32 Len, u32 XLimit, u32 YLimit) // break t = Text[X]; - *(Text + X) = '\0'; + Text[X] = '\0'; tb_printf(0, Y, 0, 0, "%s", Text + PrevX); Text[X] = t; Y++; |