pane

Termbox2 based terminal UI library
git clone git://git.dimitrijedobrota.com/pane.git
Log | Files | Refs

commit bc29ff001086358d5dbc2c6a8429f5aaf7410ee3
parent f03c95750611845c993f4fe486b743636e6a67e7
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 31 Aug 2022 16:47:31 +0200

Condense pane directional pointers into array

Use continence macros for easy interface

Diffstat:
Msrc/pane.c | 56+++++++++++++++++++++++++-------------------------------
1 file changed, 25 insertions(+), 31 deletions(-)

diff --git a/src/pane.c b/src/pane.c @@ -6,7 +6,6 @@ #include "utils.h" #define T Pane_T - struct T { int children_num; T *children; @@ -22,16 +21,18 @@ struct T { int y; int active; - T left; - T right; - T up; - T down; + T direction[4]; int index; int child_no; T parent; }; +#define UP(pane) pane->direction[0] +#define RIGHT(pane) pane->direction[1] +#define DOWN(pane) pane->direction[2] +#define LEFT(pane) pane->direction[3] + Pane_T MAIN; Pane_T ACTIVE; @@ -168,10 +169,6 @@ int pane_start(void) MAIN->width = tb_width(); MAIN->active = 1; MAIN->parent = MAIN; - /* MAIN->up = MAIN; */ - /* MAIN->down = MAIN; */ - /* MAIN->left = MAIN; */ - /* MAIN->right = MAIN; */ pane_handle_resize(); ACTIVE = MAIN; @@ -216,10 +213,9 @@ void split(T self, size_t count, va_list ap) pane_resize(self); } -#define wrap_or_slap(direction, index) \ - child->direction = (self->direction && self->direction != self) ? \ - (self->direction) : \ - self->children[index]; +#define wrap_or_slap(dir, index) \ + dir(child) = (dir(self) && dir(self) != self) ? dir(self) : \ + self->children[index]; T *pane_split(T self, size_t count, ...) { @@ -234,19 +230,19 @@ T *pane_split(T self, size_t count, ...) T child; for (i = 0; i < self->children_num; i++) { child = self->children[i]; - wrap_or_slap(up, i); - wrap_or_slap(down, i); + wrap_or_slap(UP, i); + wrap_or_slap(DOWN, i); } for (i = 1; i < self->children_num; i++) - self->children[i]->left = self->children[i - 1]; + LEFT(self->children[i]) = self->children[i - 1]; child = self->children[0]; - wrap_or_slap(left, self->children_num - 1); + wrap_or_slap(LEFT, self->children_num - 1); for (i = 0; i < self->children_num - 1; i++) - self->children[i]->right = self->children[i + 1]; + RIGHT(self->children[i]) = self->children[i + 1]; child = self->children[self->children_num - 1]; - wrap_or_slap(right, 0); + wrap_or_slap(RIGHT, 0); return self->children; } @@ -264,19 +260,19 @@ T *pane_vsplit(T self, size_t count, ...) T child; for (i = 0; i < self->children_num; i++) { child = self->children[i]; - wrap_or_slap(left, i); - wrap_or_slap(right, i); + wrap_or_slap(LEFT, i); + wrap_or_slap(RIGHT, i); } for (i = 1; i < self->children_num; i++) - self->children[i]->up = self->children[i - 1]; + UP(self->children[i]) = self->children[i - 1]; child = self->children[0]; - wrap_or_slap(up, self->children_num - 1); + wrap_or_slap(UP, self->children_num - 1); for (i = 0; i < self->children_num - 1; i++) - self->children[i]->down = self->children[i + 1]; + DOWN(self->children[i]) = self->children[i + 1]; child = self->children[self->children_num - 1]; - wrap_or_slap(down, 0); + wrap_or_slap(DOWN, 0); return self->children; } @@ -421,9 +417,7 @@ redraw:; void setActive(int dir) { T old = ACTIVE, tmp; - - T arr[] = { ACTIVE->up, ACTIVE->right, ACTIVE->down, ACTIVE->left }; - ACTIVE = arr[dir]; + ACTIVE = ACTIVE->direction[dir]; while (ACTIVE->children_num) { int last = ACTIVE->children_num - 1; @@ -460,9 +454,9 @@ void printDiagram(T self) if (self != MAIN) printf("%p:%d: parent:%d up:%d;down:%d;left:%d;right:%d;\n", - self, self->index, self->parent->index, self->up->index, - self->down->index, self->left->index, - self->right->index); + self, self->index, self->parent->index, UP(self)->index, + DOWN(self)->index, LEFT(self)->index, + RIGHT(self)->index); for (i = 0; i < self->children_num; i++) printDiagram(self->children[i]);