lipgloss 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,474 @@
1
+ #include "extension.h"
2
+
3
+ static void style_free(void *pointer) {
4
+ lipgloss_style_t *style = (lipgloss_style_t *) pointer;
5
+
6
+ if (style->handle != 0) {
7
+ lipgloss_free_style(style->handle);
8
+ }
9
+
10
+ xfree(style);
11
+ }
12
+
13
+ static size_t style_memsize(const void *pointer) {
14
+ return sizeof(lipgloss_style_t);
15
+ }
16
+
17
+ const rb_data_type_t style_type = {
18
+ .wrap_struct_name = "Lipgloss::Style",
19
+ .function = {
20
+ .dmark = NULL,
21
+ .dfree = style_free,
22
+ .dsize = style_memsize,
23
+ },
24
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
25
+ };
26
+
27
+ static VALUE style_alloc(VALUE klass) {
28
+ lipgloss_style_t *style = ALLOC(lipgloss_style_t);
29
+ style->handle = lipgloss_new_style();
30
+ return TypedData_Wrap_Struct(klass, &style_type, style);
31
+ }
32
+
33
+ VALUE style_wrap(VALUE klass, unsigned long long handle) {
34
+ lipgloss_style_t *style = ALLOC(lipgloss_style_t);
35
+
36
+ style->handle = handle;
37
+
38
+ return TypedData_Wrap_Struct(klass, &style_type, style);
39
+ }
40
+
41
+ static VALUE style_initialize(VALUE self) {
42
+ return self;
43
+ }
44
+
45
+ static VALUE style_render(VALUE self, VALUE string) {
46
+ GET_STYLE(self, style);
47
+ Check_Type(string, T_STRING);
48
+
49
+ char *result = lipgloss_style_render(style->handle, StringValueCStr(string));
50
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
51
+ lipgloss_free(result);
52
+
53
+ return rb_result;
54
+ }
55
+
56
+ // Formatting methods
57
+
58
+ static VALUE style_bold(VALUE self, VALUE value) {
59
+ GET_STYLE(self, style);
60
+ unsigned long long new_handle = lipgloss_style_bold(style->handle, RTEST(value) ? 1 : 0);
61
+ return style_wrap(rb_class_of(self), new_handle);
62
+ }
63
+
64
+ static VALUE style_italic(VALUE self, VALUE value) {
65
+ GET_STYLE(self, style);
66
+ unsigned long long new_handle = lipgloss_style_italic(style->handle, RTEST(value) ? 1 : 0);
67
+ return style_wrap(rb_class_of(self), new_handle);
68
+ }
69
+
70
+ static VALUE style_underline(VALUE self, VALUE value) {
71
+ GET_STYLE(self, style);
72
+ unsigned long long new_handle = lipgloss_style_underline(style->handle, RTEST(value) ? 1 : 0);
73
+ return style_wrap(rb_class_of(self), new_handle);
74
+ }
75
+
76
+ static VALUE style_strikethrough(VALUE self, VALUE value) {
77
+ GET_STYLE(self, style);
78
+ unsigned long long new_handle = lipgloss_style_strikethrough(style->handle, RTEST(value) ? 1 : 0);
79
+ return style_wrap(rb_class_of(self), new_handle);
80
+ }
81
+
82
+ static VALUE style_reverse(VALUE self, VALUE value) {
83
+ GET_STYLE(self, style);
84
+ unsigned long long new_handle = lipgloss_style_reverse(style->handle, RTEST(value) ? 1 : 0);
85
+ return style_wrap(rb_class_of(self), new_handle);
86
+ }
87
+
88
+ static VALUE style_blink(VALUE self, VALUE value) {
89
+ GET_STYLE(self, style);
90
+ unsigned long long new_handle = lipgloss_style_blink(style->handle, RTEST(value) ? 1 : 0);
91
+ return style_wrap(rb_class_of(self), new_handle);
92
+ }
93
+
94
+ static VALUE style_faint(VALUE self, VALUE value) {
95
+ GET_STYLE(self, style);
96
+ unsigned long long new_handle = lipgloss_style_faint(style->handle, RTEST(value) ? 1 : 0);
97
+ return style_wrap(rb_class_of(self), new_handle);
98
+ }
99
+
100
+ // Color helper functions
101
+
102
+ static int is_complete_color(VALUE obj) {
103
+ return rb_respond_to(obj, rb_intern("true_color")) && rb_respond_to(obj, rb_intern("ansi256")) && rb_respond_to(obj, rb_intern("ansi"));
104
+ }
105
+
106
+ // Color methods
107
+
108
+ static VALUE style_foreground(VALUE self, VALUE color) {
109
+ GET_STYLE(self, style);
110
+
111
+ if (is_adaptive_color(color)) {
112
+ VALUE light = rb_funcall(color, rb_intern("light"), 0);
113
+ VALUE dark = rb_funcall(color, rb_intern("dark"), 0);
114
+
115
+ if (is_complete_color(light) && is_complete_color(dark)) {
116
+ VALUE light_true = rb_funcall(light, rb_intern("true_color"), 0);
117
+ VALUE light_256 = rb_funcall(light, rb_intern("ansi256"), 0);
118
+ VALUE light_ansi = rb_funcall(light, rb_intern("ansi"), 0);
119
+ VALUE dark_true = rb_funcall(dark, rb_intern("true_color"), 0);
120
+ VALUE dark_256 = rb_funcall(dark, rb_intern("ansi256"), 0);
121
+ VALUE dark_ansi = rb_funcall(dark, rb_intern("ansi"), 0);
122
+
123
+ unsigned long long new_handle = lipgloss_style_foreground_complete_adaptive(
124
+ style->handle,
125
+ StringValueCStr(light_true),
126
+ StringValueCStr(light_256),
127
+ StringValueCStr(light_ansi),
128
+ StringValueCStr(dark_true),
129
+ StringValueCStr(dark_256),
130
+ StringValueCStr(dark_ansi)
131
+ );
132
+
133
+ return style_wrap(rb_class_of(self), new_handle);
134
+ }
135
+
136
+ unsigned long long new_handle = lipgloss_style_foreground_adaptive(
137
+ style->handle,
138
+ StringValueCStr(light),
139
+ StringValueCStr(dark)
140
+ );
141
+
142
+ return style_wrap(rb_class_of(self), new_handle);
143
+ }
144
+
145
+ if (is_complete_color(color)) {
146
+ VALUE true_color = rb_funcall(color, rb_intern("true_color"), 0);
147
+ VALUE ansi256 = rb_funcall(color, rb_intern("ansi256"), 0);
148
+ VALUE ansi = rb_funcall(color, rb_intern("ansi"), 0);
149
+
150
+ unsigned long long new_handle = lipgloss_style_foreground_complete(
151
+ style->handle,
152
+ StringValueCStr(true_color),
153
+ StringValueCStr(ansi256),
154
+ StringValueCStr(ansi)
155
+ );
156
+
157
+ return style_wrap(rb_class_of(self), new_handle);
158
+ }
159
+
160
+ Check_Type(color, T_STRING);
161
+ unsigned long long new_handle = lipgloss_style_foreground(style->handle, StringValueCStr(color));
162
+
163
+ return style_wrap(rb_class_of(self), new_handle);
164
+ }
165
+
166
+ static VALUE style_background(VALUE self, VALUE color) {
167
+ GET_STYLE(self, style);
168
+
169
+ if (is_adaptive_color(color)) {
170
+ VALUE light = rb_funcall(color, rb_intern("light"), 0);
171
+ VALUE dark = rb_funcall(color, rb_intern("dark"), 0);
172
+
173
+ if (is_complete_color(light) && is_complete_color(dark)) {
174
+ VALUE light_true = rb_funcall(light, rb_intern("true_color"), 0);
175
+ VALUE light_256 = rb_funcall(light, rb_intern("ansi256"), 0);
176
+ VALUE light_ansi = rb_funcall(light, rb_intern("ansi"), 0);
177
+ VALUE dark_true = rb_funcall(dark, rb_intern("true_color"), 0);
178
+ VALUE dark_256 = rb_funcall(dark, rb_intern("ansi256"), 0);
179
+ VALUE dark_ansi = rb_funcall(dark, rb_intern("ansi"), 0);
180
+
181
+ unsigned long long new_handle = lipgloss_style_background_complete_adaptive(
182
+ style->handle,
183
+ StringValueCStr(light_true),
184
+ StringValueCStr(light_256),
185
+ StringValueCStr(light_ansi),
186
+ StringValueCStr(dark_true),
187
+ StringValueCStr(dark_256),
188
+ StringValueCStr(dark_ansi)
189
+ );
190
+
191
+ return style_wrap(rb_class_of(self), new_handle);
192
+ }
193
+
194
+ unsigned long long new_handle = lipgloss_style_background_adaptive(
195
+ style->handle,
196
+ StringValueCStr(light),
197
+ StringValueCStr(dark)
198
+ );
199
+
200
+ return style_wrap(rb_class_of(self), new_handle);
201
+ }
202
+
203
+ if (is_complete_color(color)) {
204
+ VALUE true_color = rb_funcall(color, rb_intern("true_color"), 0);
205
+ VALUE ansi256 = rb_funcall(color, rb_intern("ansi256"), 0);
206
+ VALUE ansi = rb_funcall(color, rb_intern("ansi"), 0);
207
+
208
+ unsigned long long new_handle = lipgloss_style_background_complete(
209
+ style->handle,
210
+ StringValueCStr(true_color),
211
+ StringValueCStr(ansi256),
212
+ StringValueCStr(ansi)
213
+ );
214
+
215
+ return style_wrap(rb_class_of(self), new_handle);
216
+ }
217
+
218
+ Check_Type(color, T_STRING);
219
+ unsigned long long new_handle = lipgloss_style_background(style->handle, StringValueCStr(color));
220
+
221
+ return style_wrap(rb_class_of(self), new_handle);
222
+ }
223
+
224
+ static VALUE style_margin_background(VALUE self, VALUE color) {
225
+ GET_STYLE(self, style);
226
+ Check_Type(color, T_STRING);
227
+
228
+ unsigned long long new_handle = lipgloss_style_margin_background(style->handle, StringValueCStr(color));
229
+
230
+ return style_wrap(rb_class_of(self), new_handle);
231
+ }
232
+
233
+ // Size methods
234
+
235
+ static VALUE style_width(VALUE self, VALUE width) {
236
+ GET_STYLE(self, style);
237
+ unsigned long long new_handle = lipgloss_style_width(style->handle, NUM2INT(width));
238
+ return style_wrap(rb_class_of(self), new_handle);
239
+ }
240
+
241
+ static VALUE style_height(VALUE self, VALUE height) {
242
+ GET_STYLE(self, style);
243
+ unsigned long long new_handle = lipgloss_style_height(style->handle, NUM2INT(height));
244
+ return style_wrap(rb_class_of(self), new_handle);
245
+ }
246
+
247
+ static VALUE style_max_width(VALUE self, VALUE width) {
248
+ GET_STYLE(self, style);
249
+ unsigned long long new_handle = lipgloss_style_max_width(style->handle, NUM2INT(width));
250
+ return style_wrap(rb_class_of(self), new_handle);
251
+ }
252
+
253
+ static VALUE style_max_height(VALUE self, VALUE height) {
254
+ GET_STYLE(self, style);
255
+ unsigned long long new_handle = lipgloss_style_max_height(style->handle, NUM2INT(height));
256
+ return style_wrap(rb_class_of(self), new_handle);
257
+ }
258
+
259
+ // Alignment methods
260
+
261
+ static VALUE style_align(int argc, VALUE *argv, VALUE self) {
262
+ GET_STYLE(self, style);
263
+
264
+ if (argc == 0 || argc > 2) {
265
+ rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..2)", argc);
266
+ }
267
+
268
+ double positions[2];
269
+ for (int index = 0; index < argc; index++) {
270
+ positions[index] = NUM2DBL(argv[index]);
271
+ }
272
+
273
+ unsigned long long new_handle = lipgloss_style_align(style->handle, positions, argc);
274
+
275
+ return style_wrap(rb_class_of(self), new_handle);
276
+ }
277
+
278
+ static VALUE style_align_horizontal(VALUE self, VALUE position) {
279
+ GET_STYLE(self, style);
280
+ unsigned long long new_handle = lipgloss_style_align_horizontal(style->handle, NUM2DBL(position));
281
+ return style_wrap(rb_class_of(self), new_handle);
282
+ }
283
+
284
+ static VALUE style_align_vertical(VALUE self, VALUE position) {
285
+ GET_STYLE(self, style);
286
+ unsigned long long new_handle = lipgloss_style_align_vertical(style->handle, NUM2DBL(position));
287
+ return style_wrap(rb_class_of(self), new_handle);
288
+ }
289
+
290
+ // Other style methods
291
+
292
+ static VALUE style_inline(VALUE self, VALUE value) {
293
+ GET_STYLE(self, style);
294
+ unsigned long long new_handle = lipgloss_style_inline(style->handle, RTEST(value) ? 1 : 0);
295
+ return style_wrap(rb_class_of(self), new_handle);
296
+ }
297
+
298
+ static VALUE style_tab_width(VALUE self, VALUE width) {
299
+ GET_STYLE(self, style);
300
+ unsigned long long new_handle = lipgloss_style_tab_width(style->handle, NUM2INT(width));
301
+ return style_wrap(rb_class_of(self), new_handle);
302
+ }
303
+
304
+ static VALUE style_underline_spaces(VALUE self, VALUE value) {
305
+ GET_STYLE(self, style);
306
+ unsigned long long new_handle = lipgloss_style_underline_spaces(style->handle, RTEST(value) ? 1 : 0);
307
+ return style_wrap(rb_class_of(self), new_handle);
308
+ }
309
+
310
+ static VALUE style_strikethrough_spaces(VALUE self, VALUE value) {
311
+ GET_STYLE(self, style);
312
+ unsigned long long new_handle = lipgloss_style_strikethrough_spaces(style->handle, RTEST(value) ? 1 : 0);
313
+ return style_wrap(rb_class_of(self), new_handle);
314
+ }
315
+
316
+ // SetString, Inherit, to_s
317
+
318
+ static VALUE style_set_string(VALUE self, VALUE string) {
319
+ GET_STYLE(self, style);
320
+ Check_Type(string, T_STRING);
321
+
322
+ unsigned long long new_handle = lipgloss_style_set_string(style->handle, StringValueCStr(string));
323
+
324
+ return style_wrap(rb_class_of(self), new_handle);
325
+ }
326
+
327
+ static VALUE style_inherit(VALUE self, VALUE other) {
328
+ GET_STYLE(self, style);
329
+ lipgloss_style_t *other_style;
330
+
331
+ TypedData_Get_Struct(other, lipgloss_style_t, &style_type, other_style);
332
+ unsigned long long new_handle = lipgloss_style_inherit(style->handle, other_style->handle);
333
+
334
+ return style_wrap(rb_class_of(self), new_handle);
335
+ }
336
+
337
+ static VALUE style_to_s(VALUE self) {
338
+ GET_STYLE(self, style);
339
+ char *result = lipgloss_style_string(style->handle);
340
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
341
+
342
+ lipgloss_free(result);
343
+
344
+ return rb_result;
345
+ }
346
+
347
+ // Getter methods
348
+
349
+ static VALUE style_get_bold(VALUE self) {
350
+ GET_STYLE(self, style);
351
+ return lipgloss_style_get_bold(style->handle) ? Qtrue : Qfalse;
352
+ }
353
+
354
+ static VALUE style_get_italic(VALUE self) {
355
+ GET_STYLE(self, style);
356
+ return lipgloss_style_get_italic(style->handle) ? Qtrue : Qfalse;
357
+ }
358
+
359
+ static VALUE style_get_underline(VALUE self) {
360
+ GET_STYLE(self, style);
361
+ return lipgloss_style_get_underline(style->handle) ? Qtrue : Qfalse;
362
+ }
363
+
364
+ static VALUE style_get_strikethrough(VALUE self) {
365
+ GET_STYLE(self, style);
366
+ return lipgloss_style_get_strikethrough(style->handle) ? Qtrue : Qfalse;
367
+ }
368
+
369
+ static VALUE style_get_reverse(VALUE self) {
370
+ GET_STYLE(self, style);
371
+ return lipgloss_style_get_reverse(style->handle) ? Qtrue : Qfalse;
372
+ }
373
+
374
+ static VALUE style_get_blink(VALUE self) {
375
+ GET_STYLE(self, style);
376
+ return lipgloss_style_get_blink(style->handle) ? Qtrue : Qfalse;
377
+ }
378
+
379
+ static VALUE style_get_faint(VALUE self) {
380
+ GET_STYLE(self, style);
381
+ return lipgloss_style_get_faint(style->handle) ? Qtrue : Qfalse;
382
+ }
383
+
384
+ static VALUE style_get_foreground(VALUE self) {
385
+ GET_STYLE(self, style);
386
+ char *result = lipgloss_style_get_foreground(style->handle);
387
+ if (result == NULL || result[0] == '\0') {
388
+ if (result) lipgloss_free(result);
389
+ return Qnil;
390
+ }
391
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
392
+ lipgloss_free(result);
393
+ return rb_result;
394
+ }
395
+
396
+ static VALUE style_get_background(VALUE self) {
397
+ GET_STYLE(self, style);
398
+ char *result = lipgloss_style_get_background(style->handle);
399
+
400
+ if (result == NULL || result[0] == '\0') {
401
+ if (result) lipgloss_free(result);
402
+ return Qnil;
403
+ }
404
+
405
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
406
+ lipgloss_free(result);
407
+
408
+ return rb_result;
409
+ }
410
+
411
+ static VALUE style_get_width(VALUE self) {
412
+ GET_STYLE(self, style);
413
+ return INT2NUM(lipgloss_style_get_width(style->handle));
414
+ }
415
+
416
+ static VALUE style_get_height(VALUE self) {
417
+ GET_STYLE(self, style);
418
+ return INT2NUM(lipgloss_style_get_height(style->handle));
419
+ }
420
+
421
+ void Init_lipgloss_style(void) {
422
+ cStyle = rb_define_class_under(mLipgloss, "Style", rb_cObject);
423
+
424
+ rb_define_alloc_func(cStyle, style_alloc);
425
+
426
+ rb_define_method(cStyle, "initialize", style_initialize, 0);
427
+ rb_define_method(cStyle, "render", style_render, 1);
428
+
429
+ rb_define_method(cStyle, "bold", style_bold, 1);
430
+ rb_define_method(cStyle, "italic", style_italic, 1);
431
+ rb_define_method(cStyle, "underline", style_underline, 1);
432
+ rb_define_method(cStyle, "strikethrough", style_strikethrough, 1);
433
+ rb_define_method(cStyle, "reverse", style_reverse, 1);
434
+ rb_define_method(cStyle, "blink", style_blink, 1);
435
+ rb_define_method(cStyle, "faint", style_faint, 1);
436
+
437
+ rb_define_method(cStyle, "foreground", style_foreground, 1);
438
+ rb_define_method(cStyle, "background", style_background, 1);
439
+ rb_define_method(cStyle, "margin_background", style_margin_background, 1);
440
+
441
+ rb_define_method(cStyle, "width", style_width, 1);
442
+ rb_define_method(cStyle, "height", style_height, 1);
443
+ rb_define_method(cStyle, "max_width", style_max_width, 1);
444
+ rb_define_method(cStyle, "max_height", style_max_height, 1);
445
+
446
+ rb_define_method(cStyle, "_align", style_align, -1);
447
+ rb_define_method(cStyle, "_align_horizontal", style_align_horizontal, 1);
448
+ rb_define_method(cStyle, "_align_vertical", style_align_vertical, 1);
449
+
450
+ rb_define_method(cStyle, "inline", style_inline, 1);
451
+ rb_define_method(cStyle, "tab_width", style_tab_width, 1);
452
+ rb_define_method(cStyle, "underline_spaces", style_underline_spaces, 1);
453
+ rb_define_method(cStyle, "strikethrough_spaces", style_strikethrough_spaces, 1);
454
+
455
+ rb_define_method(cStyle, "set_string", style_set_string, 1);
456
+ rb_define_method(cStyle, "inherit", style_inherit, 1);
457
+ rb_define_method(cStyle, "to_s", style_to_s, 0);
458
+
459
+ rb_define_method(cStyle, "bold?", style_get_bold, 0);
460
+ rb_define_method(cStyle, "italic?", style_get_italic, 0);
461
+ rb_define_method(cStyle, "underline?", style_get_underline, 0);
462
+ rb_define_method(cStyle, "strikethrough?", style_get_strikethrough, 0);
463
+ rb_define_method(cStyle, "reverse?", style_get_reverse, 0);
464
+ rb_define_method(cStyle, "blink?", style_get_blink, 0);
465
+ rb_define_method(cStyle, "faint?", style_get_faint, 0);
466
+ rb_define_method(cStyle, "get_foreground", style_get_foreground, 0);
467
+ rb_define_method(cStyle, "get_background", style_get_background, 0);
468
+ rb_define_method(cStyle, "get_width", style_get_width, 0);
469
+ rb_define_method(cStyle, "get_height", style_get_height, 0);
470
+
471
+ register_style_spacing_methods();
472
+ register_style_border_methods();
473
+ register_style_unset_methods();
474
+ }