-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeWindow.c
More file actions
221 lines (165 loc) · 6.4 KB
/
codeWindow.c
File metadata and controls
221 lines (165 loc) · 6.4 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdlib.h>
#include "codeWindow.h"
#include "color.h"
#include "interpreter.h" // For the defines
static uint16_t codeHeight = 0;
static WINDOW* codeWin;
static char* brainfuckCode;
// codeLines is an array that stores the first index of every line in the code. Lines include newlines, but also width overflows.
static uint16_t* codeLines;
static uint16_t codeLinesAmount;
static uint16_t codeLinesArraySize;
static uint16_t currentCodeLine = 0;
static void StoreCodeLine(uint16_t index);
static int GetCharAttr(char c);
static void ReprintCode(void);
static void FindAllCodeLines(void);
// Print the first window of code.
void InitCodeWindow(WINDOW* window, char* code) {
codeWin = window;
codeHeight = getmaxy(window);
brainfuckCode = code;
// Init the codeLines array with like idk 2 times the window height.
codeLinesArraySize = 2 * codeHeight;
codeLines = (uint16_t*) malloc(codeLinesArraySize * sizeof(uint16_t));
FindAllCodeLines();
ReprintCode();
wrefresh(codeWin);
}
// Reprints a character at a certain index, and selects it as the new cursor if `isCursor` != 0.
void UpdateCode(uint16_t codeIndex, uint8_t isCursor) {
static uint16_t prevIndex = -1;
static uint16_t prevLine = -1;
static uint16_t prevCol = -1;
//* Finding the line //
uint16_t line = 0;
// Find the line of the character that has to be reprinted through codeLines.
do line++;
while(codeLines[line] <= codeIndex && line < codeLinesAmount);
line--;
// Make the next code more readable.
int16_t row = line - currentCodeLine;
const int16_t prevRow = prevLine - currentCodeLine;
//* Scrolling //
// If the line is out of bounds, but we've printed it before:
// Scroll the screen down.
if((row < CODE_TOP_MARGIN && line > CODE_TOP_MARGIN) || row < 0) {
// Make sure that adding the margin does not put the first line lower than the top of the window.
if(line <= CODE_TOP_MARGIN) currentCodeLine = 0;
else currentCodeLine = line - CODE_TOP_MARGIN;
ReprintCode();
}
// Scroll the screen up.
else if(row > codeHeight - CODE_BOTTOM_MARGIN) {
currentCodeLine = line - codeHeight + CODE_BOTTOM_MARGIN;
ReprintCode();
}
// If we're reprinting a new cursor, we want to remove the old one.
// Doesn't need to happen if the window was reprinted by scrolling.
else if(isCursor) {
// Print the previously selected character in the normal color.
wattrset(codeWin, GetCharAttr(brainfuckCode[prevIndex]));
mvwaddch(codeWin, prevRow, prevCol, NO_BREAKPOINT_bm & brainfuckCode[prevIndex]);
}
// currentCodeLine could have changed.
row = line - currentCodeLine;
// Calculate the column of the character.
int16_t col = codeIndex - codeLines[line];
//* Printing //
// Set the character's color & style.
if(isCursor) {
// Select the character if we're reprinting a new cursor.
wattrset(codeWin, COLOR_PAIR(RUNNING_PAIR));
// Add an underline if it's a breakpoint.
if(brainfuckCode[codeIndex] & BREAKPOINT_bm) wattron(codeWin, A_UNDERLINE);
// Keep track of the previous cursor position.
prevIndex = codeIndex;
prevLine = line;
prevCol = col;
}
// Color the character normally we're reprinting a new breakpoint.
else wattrset(codeWin, GetCharAttr(brainfuckCode[codeIndex]));
// Actually print the character.
mvwaddch(codeWin, row, col, NO_BREAKPOINT_bm & brainfuckCode[codeIndex]);
wstandend(codeWin);
wrefresh(codeWin);
}
void FindAllCodeLines(void) {
uint16_t codeIndex = 0;
scrollok(codeWin, 0);
while(1) {
// Move to the start of the final line so we get an error from waddch
// when we try to print a character on the next line.
wmove(codeWin, codeHeight - 1, 0);
// Do this first so we store 0 as well (very nice to have).
StoreCodeLine(codeIndex);
int endOfLine = 0; // This is an int (cringe) because waddch returns an int.
while(!endOfLine) {
// Quit when we reach the end of the file.
if(brainfuckCode[codeIndex] == '\0') {
wmove(codeWin, 0, 0);
wclear(codeWin);
return;
}
// Print the line onto the window. There might be better ways to detect the end of the line,
// but this is the easiest and most accessable one for me right now.
endOfLine = waddch(codeWin, NO_BREAKPOINT_bm & brainfuckCode[codeIndex]);
codeIndex++;
}
}
StoreCodeLine(codeIndex);
}
void StoreCodeLine(uint16_t index) {
codeLines[codeLinesAmount++] = index;
// Reallocate codeLines if it too big.
if(codeLinesAmount >= codeLinesArraySize) {
codeLinesArraySize += codeHeight;
codeLines = (uint16_t*) realloc(codeLines, codeLinesArraySize * sizeof(uint16_t));
}
}
// Prints lines from `from` to `to` in lines (not rows).
void ReprintCode(void) {
wclear(codeWin);
wmove(codeWin, 0, 0);
uint16_t codeIndex = codeLines[currentCodeLine];
int endOfLine = 0;
while(!endOfLine && brainfuckCode[codeIndex] != '\0') {
wattrset(codeWin, GetCharAttr(brainfuckCode[codeIndex]));
endOfLine = waddch(codeWin, NO_BREAKPOINT_bm & brainfuckCode[codeIndex]);
codeIndex++;
}
wstandend(codeWin);
}
int16_t InterfaceGetCodeIndex(uint16_t mouseY, uint16_t mouseX) {
// Return the index of the clicked character.
mouseY -= getbegy(codeWin);
mouseX -= getbegx(codeWin);
const uint16_t line = mouseY + currentCodeLine;
const uint16_t index = codeLines[line] + mouseX;
// If it's not on a character, return an error.
if(index >= codeLines[line + 1]) return ERR;
return index;
}
int GetCharAttr(char c) {
int underline = 0;
// Put an underline if it's a breakpoint character
if(c & BREAKPOINT_bm) underline = A_UNDERLINE;
switch(c & NO_BREAKPOINT_bm) {
case '[':
case ']':
return underline | COLOR_PAIR(BRACKET_PAIR);
case '>':
case '<':
return underline | COLOR_PAIR(MOVER_PAIR);
case '+':
case '-':
return underline | COLOR_PAIR(CHANGER_PAIR);
case '.':
case ',':
return underline | COLOR_PAIR(INOUT_PAIR);
case '#':
return underline | COLOR_PAIR(BREAKPOINT_PAIR);
default:
return COLOR_PAIR(COMMENT_PAIR);
}
}