-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.c
180 lines (148 loc) · 3.99 KB
/
frame.c
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
/* Plan:
*
* global rect structs - one for the main screen, the other for
* the text box to type in.
*
* array of rect structs
* function that draws the rects, according to the screensize (no refresh)
* open a thread that runs the function every 1/60th of a second
*/
#define _XOPEN_SOURCE_EXTENDED
#include "frame.h"
#include <locale.h>
#include <ncurses.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
#define NUM_THREADS 1
#define VERTICAL L"┃"
#define HORIZONTAL L"━"
// constants
// const wchar_t HORIZONTAL = 0x2501;
const wchar_t CORNERS[] = { 0x250f, 0x2513, 0x251b, 0x2517 };
void *update_bounds();
// globals
int max_y, max_x;
pthread_t threads[NUM_THREADS];
/**
* Write text to the given rectangle. If the text is too long, it will return.
*
* @param text The text to write
* @param r The rectangle in which to write the text. Must have height >= 3
* to work properly.
* @return void
*/
void write_text(char *text, rect_t *r) {
int c;
char *w = text;
int y, x;
for (y = r->y0 + 1; y < r->y1; y++)
for (x = r->x0 + 1; x < r->x1; x++)
mvaddch(y, x, ' ');
y = r->y0 + 1, x = r->x0 + 1;
move(y, x);
while ((c = *w++)) {
if (c == '\n')
move(y + 1, r->x0 + 1);
else
addch(c);
getyx(stdscr, y, x);
if (x == r->x1) {
x = r->x0 + 1;
y++;
move(y, x);
if (y == r->y1)
return;
}
}
}
int write_strlist(strlist *bank, rect_t *r, int start_index, int end_index) {
for (int y0 = r->y0 + 1; y0 < r->y1; y0++) {
move(y0, r->x0 + 1);
for (int i = r->x0 + 1; i < r->x1 - 1; i++)
addch(' ');
}
int counter = start_index;
// Write characters
move(r->y0 + 1, r->x0 + 1);
int wc = 0, y, x; // word count at first line
string *curr;
for (int i = 0; i < (int)bank->len; i++) {
curr = strlist_get(bank, i);
// fprintf(stderr, "%s\n", curr->val);
getyx(stdscr, y, x);
// Move to next line if word goes past end of screen
if (curr->len + 1 >= r->x1 - x) {
x = r->x0 + 1;
y++;
move(y, x);
if (y == r->y1)
break;
}
if (y - r->y0 - 1 == 0)
wc++;
attron(curr->style);
addstr(curr->val);
attroff(curr->style);
addch(' ');
if (counter++ == end_index)
break;
}
return wc;
}
void set_color(int y, int x, int code) {
chtype curr = mvinch(y, x);
mvaddch(y, x, curr | COLOR_PAIR(code));
}
/**
* Draw a rectangle to the screen.
*
* @param r The rect_t struct to draw
*/
void draw_rect(rect_t *r) {
int width = r->x1 - r->x0 + 1;
wchar_t hor_line[width + 1];
// make horizontal lines for top and bottom
hor_line[0] = CORNERS[0];
for (int i = 1; i < width - 1; i++)
hor_line[i] = HORIZONTAL[0];
hor_line[width - 1] = CORNERS[1];
hor_line[width] = L'\0';
mvaddwstr(r->y0, r->x0, hor_line);
hor_line[0] = CORNERS[3];
hor_line[width - 1] = CORNERS[2];
mvaddwstr(r->y1, r->x0, hor_line);
cchar_t vertical_bar;
setcchar(&vertical_bar, L"┃", 0, 0, NULL);
for (int y = r->y0 + 1; y < r->y1; y++) {
mvadd_wch(y, r->x0, &vertical_bar);
mvadd_wch(y, r->x1, &vertical_bar);
}
}
void *update_bounds() {
for (;;) {
getmaxyx(stdscr, max_y, max_x);
usleep(100000);
}
}
void init_frame() {
setlocale(LC_ALL, "");
initscr();
noecho();
start_color();
use_default_colors();
curs_set(0);
// create colors
init_pair(1, COLOR_GREEN, -1);
init_pair(2, COLOR_WHITE, COLOR_RED);
// have the bounds update in the background
getmaxyx(stdscr, max_y, max_x);
/* pthread_create(&threads[0], NULL, update_bounds, NULL); */
}
void del_frame() {
endwin();
for (int i = 0; i < NUM_THREADS; i++)
pthread_cancel(threads[i]);
}