-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
117 lines (103 loc) · 3.37 KB
/
display.c
File metadata and controls
117 lines (103 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "display.h"
DisplayColumn *makeDisplayColumn(
List(DisplayRow) *u, DisplayRow *a, List(DisplayRow) *d
) {
DisplayColumn *column = malloc(sizeof(DisplayColumn));
column->up = u;
column->active = a;
column->down = d;
return column;
}
DisplayRow *makeDisplayRow(List(Pane) *l, Pane *a, List(Pane) *r) {
DisplayRow *row = malloc(sizeof(DisplayRow));
row->left = l;
row->active = a;
row->right = r;
return row;
}
Pane *activePane(Display *d) {
return d->panes->active->active;
}
int activeHeight(Display *d) {
return d->height / displayColumnSize(d->panes);
}
int activeWidth(Display *d) {
return d->width / displayRowSize(d->panes->active);
}
ScreenCursor activeCursor(Display *d) {
int columnUnitSize = d->height / displayColumnSize(d->panes);
int columnOffset = columnUnitSize * ListF(DisplayRow).length(d->panes->up);
int rowUnitSize = d->width / displayRowSize(d->panes->active);
int rowOffset = rowUnitSize * ListF(Pane).length(d->panes->active->left);
return (ScreenCursor){
rowOffset + activePane(d)->cursorX + 1,
columnOffset + activePane(d)->cursorY + 1
};
}
List(List(List(PaneRow))) *drawDisplayColumn(DisplayColumn *column, int height, int width) {
int n = displayColumnSize(column);
int *eachHeight = malloc(sizeof(int));
*eachHeight = height / n;
int *firstHeight = malloc(sizeof(int));
*firstHeight = height - ((n - 1) * *eachHeight);
List(int) *heights =
ListF(int).cons(firstHeight, ListF(int).cons(eachHeight, NULL));
heights->tail->tail = heights->tail;
int *eachWidth = malloc(sizeof(int));
*eachWidth = width;
List(int) *widths = ListF(int).cons(eachWidth, NULL);
widths->tail = widths;
List(DisplayRow) *top = ListF(DisplayRow).reverse(column->up);
List(DisplayRow) *rows = ListF(DisplayRow).concat(
top,
ListF(DisplayRow).cons(column->active, column->down)
);
List(List(List(PaneRow))) *result = ListF4(DisplayRow, int, int, List(List(PaneRow)))
.zipWith(drawDisplayRow, rows, heights, widths);
free(heights->tail);
free(heights);
free(widths);
free(eachHeight);
free(firstHeight);
free(eachWidth);
ListF(DisplayRow).free(top);
ListF(DisplayRow).freeUntil(rows, column->down);
return result;
}
List(List(PaneRow)) *drawDisplayRow(DisplayRow *row, int *height, int *width) {
List(Pane) *above = ListF(Pane).reverse(row->left);
List(Pane) *panes =
ListF(Pane).concat(above, ListF(Pane).cons(row->active, row->right));
int n = ListF(Pane).length(panes);
int *eachWidth = malloc(sizeof(int));
*eachWidth = *width / n;
List(int) *widths = ListF(int).cons(eachWidth, NULL);
widths->tail = widths;
List(int) *heights = ListF(int).cons(height, NULL);
heights->tail = heights;
List(List(PaneRow)) *result =
ListF4(Pane, int, int, List(PaneRow))
.zipWith(paneDraw, panes, heights, widths);
free(heights);
free(widths);
free(eachWidth);
ListF(Pane).free(above);
ListF(Pane).freeUntil(panes, row->right);
return result;
}
int displayColumnSize(DisplayColumn *column) {
if (column == NULL) {
return 0;
} else {
return 1 + ListF(DisplayRow).length(column->up)
+ ListF(DisplayRow).length(column->down);
}
}
int displayRowSize(DisplayRow *row) {
if (row == NULL) {
return 0;
} else {
return 1 + ListF(Pane).length(row->left) +
ListF(Pane).length(row->right);
}
}