all: at this point just read readme 😭

spaghetti code
equivalent spaghetti readme
spaghetti commit (wait no, this is actually descriptive)
This commit is contained in:
2024-07-13 01:30:47 +02:00
parent 08debefeb7
commit 6b2d76c9b8
16 changed files with 954 additions and 39 deletions

64
src/efield.c Normal file
View File

@@ -0,0 +1,64 @@
#include <string.h>
#include <efield.h>
#include <ui.h>
struct editable_field field_new(char* content) {
struct editable_field __efield;
if(content != NULL) {
__efield.length = __efield.pos = strlen(content);
memcpy(__efield.content, content, __efield.length);
} else {
field_trim(&__efield, 0);
}
return __efield;
}
void field_trim(struct editable_field *field, u_char pos) {
field->length = field->pos = pos;
}
void field_update(struct editable_field *field, char *update) {
u_char insert_len = strlen(update);
if (insert_len == 0)
return;
if (field->pos > field->length)
field->pos = field->length; // WTF
if (insert_len == 1) {
// backspace
if (*update == 127) {
if (field->pos < field->length) {
memcpy(&field->content[field->pos - 1], &field->content[field->pos],
field->length - field->pos);
}
(field->pos)--;
(field->length)--;
return;
}
}
// append
if (field->length + field->pos >= 255) {
print_err("field too long");
}
if (field->pos < field->length) {
// move with immediate buffer
memmove(&field->content[field->pos + insert_len], &field->content[field->pos],
field->length - field->pos);
}
memcpy(&field->content[field->pos], update, insert_len);
field->pos += insert_len;
field->length += insert_len;
}
// returns bool depending if it was able to "use" the seek
bool field_seek(struct editable_field *field, char seek) {
if(field->length == 0) return false;
if(seek < 0 && -seek > field->pos) field->pos = 0;
else if(seek > 0 && 255 - field->pos < seek) field->pos = 255;
else field->pos += seek;
return true;
}