lipgloss 0.0.1 → 0.1.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,398 @@
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
+ static int is_adaptive_color(VALUE obj) {
107
+ return rb_respond_to(obj, rb_intern("light")) && rb_respond_to(obj, rb_intern("dark"));
108
+ }
109
+
110
+ // Color methods
111
+
112
+ static VALUE style_foreground(VALUE self, VALUE color) {
113
+ GET_STYLE(self, style);
114
+
115
+ if (is_adaptive_color(color)) {
116
+ VALUE light = rb_funcall(color, rb_intern("light"), 0);
117
+ VALUE dark = rb_funcall(color, rb_intern("dark"), 0);
118
+
119
+ if (is_complete_color(light) && is_complete_color(dark)) {
120
+ VALUE light_true = rb_funcall(light, rb_intern("true_color"), 0);
121
+ VALUE light_256 = rb_funcall(light, rb_intern("ansi256"), 0);
122
+ VALUE light_ansi = rb_funcall(light, rb_intern("ansi"), 0);
123
+ VALUE dark_true = rb_funcall(dark, rb_intern("true_color"), 0);
124
+ VALUE dark_256 = rb_funcall(dark, rb_intern("ansi256"), 0);
125
+ VALUE dark_ansi = rb_funcall(dark, rb_intern("ansi"), 0);
126
+
127
+ unsigned long long new_handle = lipgloss_style_foreground_complete_adaptive(
128
+ style->handle,
129
+ StringValueCStr(light_true),
130
+ StringValueCStr(light_256),
131
+ StringValueCStr(light_ansi),
132
+ StringValueCStr(dark_true),
133
+ StringValueCStr(dark_256),
134
+ StringValueCStr(dark_ansi)
135
+ );
136
+
137
+ return style_wrap(rb_class_of(self), new_handle);
138
+ }
139
+
140
+ unsigned long long new_handle = lipgloss_style_foreground_adaptive(
141
+ style->handle,
142
+ StringValueCStr(light),
143
+ StringValueCStr(dark)
144
+ );
145
+
146
+ return style_wrap(rb_class_of(self), new_handle);
147
+ }
148
+
149
+ if (is_complete_color(color)) {
150
+ VALUE true_color = rb_funcall(color, rb_intern("true_color"), 0);
151
+ VALUE ansi256 = rb_funcall(color, rb_intern("ansi256"), 0);
152
+ VALUE ansi = rb_funcall(color, rb_intern("ansi"), 0);
153
+
154
+ unsigned long long new_handle = lipgloss_style_foreground_complete(
155
+ style->handle,
156
+ StringValueCStr(true_color),
157
+ StringValueCStr(ansi256),
158
+ StringValueCStr(ansi)
159
+ );
160
+
161
+ return style_wrap(rb_class_of(self), new_handle);
162
+ }
163
+
164
+ Check_Type(color, T_STRING);
165
+ unsigned long long new_handle = lipgloss_style_foreground(style->handle, StringValueCStr(color));
166
+
167
+ return style_wrap(rb_class_of(self), new_handle);
168
+ }
169
+
170
+ static VALUE style_background(VALUE self, VALUE color) {
171
+ GET_STYLE(self, style);
172
+
173
+ if (is_adaptive_color(color)) {
174
+ VALUE light = rb_funcall(color, rb_intern("light"), 0);
175
+ VALUE dark = rb_funcall(color, rb_intern("dark"), 0);
176
+
177
+ if (is_complete_color(light) && is_complete_color(dark)) {
178
+ VALUE light_true = rb_funcall(light, rb_intern("true_color"), 0);
179
+ VALUE light_256 = rb_funcall(light, rb_intern("ansi256"), 0);
180
+ VALUE light_ansi = rb_funcall(light, rb_intern("ansi"), 0);
181
+ VALUE dark_true = rb_funcall(dark, rb_intern("true_color"), 0);
182
+ VALUE dark_256 = rb_funcall(dark, rb_intern("ansi256"), 0);
183
+ VALUE dark_ansi = rb_funcall(dark, rb_intern("ansi"), 0);
184
+
185
+ unsigned long long new_handle = lipgloss_style_background_complete_adaptive(
186
+ style->handle,
187
+ StringValueCStr(light_true),
188
+ StringValueCStr(light_256),
189
+ StringValueCStr(light_ansi),
190
+ StringValueCStr(dark_true),
191
+ StringValueCStr(dark_256),
192
+ StringValueCStr(dark_ansi)
193
+ );
194
+
195
+ return style_wrap(rb_class_of(self), new_handle);
196
+ }
197
+
198
+ unsigned long long new_handle = lipgloss_style_background_adaptive(
199
+ style->handle,
200
+ StringValueCStr(light),
201
+ StringValueCStr(dark)
202
+ );
203
+
204
+ return style_wrap(rb_class_of(self), new_handle);
205
+ }
206
+
207
+ if (is_complete_color(color)) {
208
+ VALUE true_color = rb_funcall(color, rb_intern("true_color"), 0);
209
+ VALUE ansi256 = rb_funcall(color, rb_intern("ansi256"), 0);
210
+ VALUE ansi = rb_funcall(color, rb_intern("ansi"), 0);
211
+
212
+ unsigned long long new_handle = lipgloss_style_background_complete(
213
+ style->handle,
214
+ StringValueCStr(true_color),
215
+ StringValueCStr(ansi256),
216
+ StringValueCStr(ansi)
217
+ );
218
+
219
+ return style_wrap(rb_class_of(self), new_handle);
220
+ }
221
+
222
+ Check_Type(color, T_STRING);
223
+ unsigned long long new_handle = lipgloss_style_background(style->handle, StringValueCStr(color));
224
+
225
+ return style_wrap(rb_class_of(self), new_handle);
226
+ }
227
+
228
+ static VALUE style_margin_background(VALUE self, VALUE color) {
229
+ GET_STYLE(self, style);
230
+ Check_Type(color, T_STRING);
231
+
232
+ unsigned long long new_handle = lipgloss_style_margin_background(style->handle, StringValueCStr(color));
233
+
234
+ return style_wrap(rb_class_of(self), new_handle);
235
+ }
236
+
237
+ // Size methods
238
+
239
+ static VALUE style_width(VALUE self, VALUE width) {
240
+ GET_STYLE(self, style);
241
+ unsigned long long new_handle = lipgloss_style_width(style->handle, NUM2INT(width));
242
+ return style_wrap(rb_class_of(self), new_handle);
243
+ }
244
+
245
+ static VALUE style_height(VALUE self, VALUE height) {
246
+ GET_STYLE(self, style);
247
+ unsigned long long new_handle = lipgloss_style_height(style->handle, NUM2INT(height));
248
+ return style_wrap(rb_class_of(self), new_handle);
249
+ }
250
+
251
+ static VALUE style_max_width(VALUE self, VALUE width) {
252
+ GET_STYLE(self, style);
253
+ unsigned long long new_handle = lipgloss_style_max_width(style->handle, NUM2INT(width));
254
+ return style_wrap(rb_class_of(self), new_handle);
255
+ }
256
+
257
+ static VALUE style_max_height(VALUE self, VALUE height) {
258
+ GET_STYLE(self, style);
259
+ unsigned long long new_handle = lipgloss_style_max_height(style->handle, NUM2INT(height));
260
+ return style_wrap(rb_class_of(self), new_handle);
261
+ }
262
+
263
+ // Alignment methods
264
+
265
+ static VALUE style_align(int argc, VALUE *argv, VALUE self) {
266
+ GET_STYLE(self, style);
267
+
268
+ if (argc == 0 || argc > 2) {
269
+ rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..2)", argc);
270
+ }
271
+
272
+ double positions[2];
273
+ for (int index = 0; index < argc; index++) {
274
+ positions[index] = NUM2DBL(argv[index]);
275
+ }
276
+
277
+ unsigned long long new_handle = lipgloss_style_align(style->handle, positions, argc);
278
+
279
+ return style_wrap(rb_class_of(self), new_handle);
280
+ }
281
+
282
+ static VALUE style_align_horizontal(VALUE self, VALUE position) {
283
+ GET_STYLE(self, style);
284
+ unsigned long long new_handle = lipgloss_style_align_horizontal(style->handle, NUM2DBL(position));
285
+ return style_wrap(rb_class_of(self), new_handle);
286
+ }
287
+
288
+ static VALUE style_align_vertical(VALUE self, VALUE position) {
289
+ GET_STYLE(self, style);
290
+ unsigned long long new_handle = lipgloss_style_align_vertical(style->handle, NUM2DBL(position));
291
+ return style_wrap(rb_class_of(self), new_handle);
292
+ }
293
+
294
+ // Other style methods
295
+
296
+ static VALUE style_inline(VALUE self, VALUE value) {
297
+ GET_STYLE(self, style);
298
+ unsigned long long new_handle = lipgloss_style_inline(style->handle, RTEST(value) ? 1 : 0);
299
+ return style_wrap(rb_class_of(self), new_handle);
300
+ }
301
+
302
+ static VALUE style_tab_width(VALUE self, VALUE width) {
303
+ GET_STYLE(self, style);
304
+ unsigned long long new_handle = lipgloss_style_tab_width(style->handle, NUM2INT(width));
305
+ return style_wrap(rb_class_of(self), new_handle);
306
+ }
307
+
308
+ static VALUE style_underline_spaces(VALUE self, VALUE value) {
309
+ GET_STYLE(self, style);
310
+ unsigned long long new_handle = lipgloss_style_underline_spaces(style->handle, RTEST(value) ? 1 : 0);
311
+ return style_wrap(rb_class_of(self), new_handle);
312
+ }
313
+
314
+ static VALUE style_strikethrough_spaces(VALUE self, VALUE value) {
315
+ GET_STYLE(self, style);
316
+ unsigned long long new_handle = lipgloss_style_strikethrough_spaces(style->handle, RTEST(value) ? 1 : 0);
317
+ return style_wrap(rb_class_of(self), new_handle);
318
+ }
319
+
320
+ // SetString, Inherit, to_s
321
+
322
+ static VALUE style_set_string(VALUE self, VALUE string) {
323
+ GET_STYLE(self, style);
324
+ Check_Type(string, T_STRING);
325
+
326
+ unsigned long long new_handle = lipgloss_style_set_string(style->handle, StringValueCStr(string));
327
+
328
+ return style_wrap(rb_class_of(self), new_handle);
329
+ }
330
+
331
+ static VALUE style_inherit(VALUE self, VALUE other) {
332
+ GET_STYLE(self, style);
333
+ lipgloss_style_t *other_style;
334
+
335
+ TypedData_Get_Struct(other, lipgloss_style_t, &style_type, other_style);
336
+ unsigned long long new_handle = lipgloss_style_inherit(style->handle, other_style->handle);
337
+
338
+ return style_wrap(rb_class_of(self), new_handle);
339
+ }
340
+
341
+ static VALUE style_to_s(VALUE self) {
342
+ GET_STYLE(self, style);
343
+ char *result = lipgloss_style_string(style->handle);
344
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
345
+
346
+ lipgloss_free(result);
347
+
348
+ return rb_result;
349
+ }
350
+
351
+ void Init_lipgloss_style(void) {
352
+ cStyle = rb_define_class_under(mLipgloss, "Style", rb_cObject);
353
+
354
+ rb_define_alloc_func(cStyle, style_alloc);
355
+
356
+ rb_define_method(cStyle, "initialize", style_initialize, 0);
357
+ rb_define_method(cStyle, "render", style_render, 1);
358
+
359
+ // Formatting
360
+ rb_define_method(cStyle, "bold", style_bold, 1);
361
+ rb_define_method(cStyle, "italic", style_italic, 1);
362
+ rb_define_method(cStyle, "underline", style_underline, 1);
363
+ rb_define_method(cStyle, "strikethrough", style_strikethrough, 1);
364
+ rb_define_method(cStyle, "reverse", style_reverse, 1);
365
+ rb_define_method(cStyle, "blink", style_blink, 1);
366
+ rb_define_method(cStyle, "faint", style_faint, 1);
367
+
368
+ // Colors
369
+ rb_define_method(cStyle, "foreground", style_foreground, 1);
370
+ rb_define_method(cStyle, "background", style_background, 1);
371
+ rb_define_method(cStyle, "margin_background", style_margin_background, 1);
372
+
373
+ // Size
374
+ rb_define_method(cStyle, "width", style_width, 1);
375
+ rb_define_method(cStyle, "height", style_height, 1);
376
+ rb_define_method(cStyle, "max_width", style_max_width, 1);
377
+ rb_define_method(cStyle, "max_height", style_max_height, 1);
378
+
379
+ // Alignment
380
+ rb_define_method(cStyle, "align", style_align, -1);
381
+ rb_define_method(cStyle, "align_horizontal", style_align_horizontal, 1);
382
+ rb_define_method(cStyle, "align_vertical", style_align_vertical, 1);
383
+
384
+ // Other
385
+ rb_define_method(cStyle, "inline", style_inline, 1);
386
+ rb_define_method(cStyle, "tab_width", style_tab_width, 1);
387
+ rb_define_method(cStyle, "underline_spaces", style_underline_spaces, 1);
388
+ rb_define_method(cStyle, "strikethrough_spaces", style_strikethrough_spaces, 1);
389
+
390
+ rb_define_method(cStyle, "set_string", style_set_string, 1);
391
+ rb_define_method(cStyle, "inherit", style_inherit, 1);
392
+ rb_define_method(cStyle, "to_s", style_to_s, 0);
393
+
394
+ // Register methods from sub-files
395
+ register_style_spacing_methods();
396
+ register_style_border_methods();
397
+ register_style_unset_methods();
398
+ }
@@ -0,0 +1,209 @@
1
+ #include "extension.h"
2
+
3
+ static int symbol_to_border_type(VALUE symbol) {
4
+ if (symbol == ID2SYM(rb_intern("normal"))) return BORDER_NORMAL;
5
+ if (symbol == ID2SYM(rb_intern("rounded"))) return BORDER_ROUNDED;
6
+ if (symbol == ID2SYM(rb_intern("thick"))) return BORDER_THICK;
7
+ if (symbol == ID2SYM(rb_intern("double"))) return BORDER_DOUBLE;
8
+ if (symbol == ID2SYM(rb_intern("hidden"))) return BORDER_HIDDEN;
9
+ if (symbol == ID2SYM(rb_intern("block"))) return BORDER_BLOCK;
10
+ if (symbol == ID2SYM(rb_intern("outer_half_block"))) return BORDER_OUTER_HALF_BLOCK;
11
+ if (symbol == ID2SYM(rb_intern("inner_half_block"))) return BORDER_INNER_HALF_BLOCK;
12
+ if (symbol == ID2SYM(rb_intern("ascii"))) return BORDER_ASCII;
13
+
14
+ return BORDER_NORMAL;
15
+ }
16
+
17
+ static VALUE style_border(int argc, VALUE *argv, VALUE self) {
18
+ GET_STYLE(self, style);
19
+
20
+ if (argc == 0) {
21
+ rb_raise(rb_eArgError, "wrong number of arguments (given 0, expected 1+)");
22
+ }
23
+
24
+ int border_type = symbol_to_border_type(argv[0]);
25
+
26
+ if (argc == 1) {
27
+ unsigned long long new_handle = lipgloss_style_border(style->handle, border_type, NULL, 0);
28
+ return style_wrap(rb_class_of(self), new_handle);
29
+ }
30
+
31
+ int sides[4];
32
+ int sides_count = argc - 1;
33
+ if (sides_count > 4) sides_count = 4;
34
+
35
+ for (int index = 0; index < sides_count; index++) {
36
+ sides[index] = RTEST(argv[index + 1]) ? 1 : 0;
37
+ }
38
+
39
+ unsigned long long new_handle = lipgloss_style_border(style->handle, border_type, sides, sides_count);
40
+
41
+ return style_wrap(rb_class_of(self), new_handle);
42
+ }
43
+
44
+ static VALUE style_border_style(VALUE self, VALUE border_sym) {
45
+ GET_STYLE(self, style);
46
+ int border_type = symbol_to_border_type(border_sym);
47
+ unsigned long long new_handle = lipgloss_style_border_style(style->handle, border_type);
48
+ return style_wrap(rb_class_of(self), new_handle);
49
+ }
50
+
51
+ static VALUE style_border_foreground(VALUE self, VALUE color) {
52
+ GET_STYLE(self, style);
53
+ Check_Type(color, T_STRING);
54
+ unsigned long long new_handle = lipgloss_style_border_foreground(style->handle, StringValueCStr(color));
55
+ return style_wrap(rb_class_of(self), new_handle);
56
+ }
57
+
58
+ static VALUE style_border_background(VALUE self, VALUE color) {
59
+ GET_STYLE(self, style);
60
+ Check_Type(color, T_STRING);
61
+ unsigned long long new_handle = lipgloss_style_border_background(style->handle, StringValueCStr(color));
62
+ return style_wrap(rb_class_of(self), new_handle);
63
+ }
64
+
65
+ static VALUE style_border_top(VALUE self, VALUE value) {
66
+ GET_STYLE(self, style);
67
+ unsigned long long new_handle = lipgloss_style_border_top(style->handle, RTEST(value) ? 1 : 0);
68
+ return style_wrap(rb_class_of(self), new_handle);
69
+ }
70
+
71
+ static VALUE style_border_right(VALUE self, VALUE value) {
72
+ GET_STYLE(self, style);
73
+ unsigned long long new_handle = lipgloss_style_border_right(style->handle, RTEST(value) ? 1 : 0);
74
+ return style_wrap(rb_class_of(self), new_handle);
75
+ }
76
+
77
+ static VALUE style_border_bottom(VALUE self, VALUE value) {
78
+ GET_STYLE(self, style);
79
+ unsigned long long new_handle = lipgloss_style_border_bottom(style->handle, RTEST(value) ? 1 : 0);
80
+ return style_wrap(rb_class_of(self), new_handle);
81
+ }
82
+
83
+ static VALUE style_border_left(VALUE self, VALUE value) {
84
+ GET_STYLE(self, style);
85
+ unsigned long long new_handle = lipgloss_style_border_left(style->handle, RTEST(value) ? 1 : 0);
86
+ return style_wrap(rb_class_of(self), new_handle);
87
+ }
88
+
89
+ static VALUE style_border_top_foreground(VALUE self, VALUE color) {
90
+ GET_STYLE(self, style);
91
+ Check_Type(color, T_STRING);
92
+ unsigned long long new_handle = lipgloss_style_border_top_foreground(style->handle, StringValueCStr(color));
93
+ return style_wrap(rb_class_of(self), new_handle);
94
+ }
95
+
96
+ static VALUE style_border_right_foreground(VALUE self, VALUE color) {
97
+ GET_STYLE(self, style);
98
+ Check_Type(color, T_STRING);
99
+ unsigned long long new_handle = lipgloss_style_border_right_foreground(style->handle, StringValueCStr(color));
100
+ return style_wrap(rb_class_of(self), new_handle);
101
+ }
102
+
103
+ static VALUE style_border_bottom_foreground(VALUE self, VALUE color) {
104
+ GET_STYLE(self, style);
105
+ Check_Type(color, T_STRING);
106
+ unsigned long long new_handle = lipgloss_style_border_bottom_foreground(style->handle, StringValueCStr(color));
107
+ return style_wrap(rb_class_of(self), new_handle);
108
+ }
109
+
110
+ static VALUE style_border_left_foreground(VALUE self, VALUE color) {
111
+ GET_STYLE(self, style);
112
+ Check_Type(color, T_STRING);
113
+ unsigned long long new_handle = lipgloss_style_border_left_foreground(style->handle, StringValueCStr(color));
114
+ return style_wrap(rb_class_of(self), new_handle);
115
+ }
116
+
117
+ static VALUE style_border_top_background(VALUE self, VALUE color) {
118
+ GET_STYLE(self, style);
119
+ Check_Type(color, T_STRING);
120
+ unsigned long long new_handle = lipgloss_style_border_top_background(style->handle, StringValueCStr(color));
121
+ return style_wrap(rb_class_of(self), new_handle);
122
+ }
123
+
124
+ static VALUE style_border_right_background(VALUE self, VALUE color) {
125
+ GET_STYLE(self, style);
126
+ Check_Type(color, T_STRING);
127
+ unsigned long long new_handle = lipgloss_style_border_right_background(style->handle, StringValueCStr(color));
128
+ return style_wrap(rb_class_of(self), new_handle);
129
+ }
130
+
131
+ static VALUE style_border_bottom_background(VALUE self, VALUE color) {
132
+ GET_STYLE(self, style);
133
+ Check_Type(color, T_STRING);
134
+ unsigned long long new_handle = lipgloss_style_border_bottom_background(style->handle, StringValueCStr(color));
135
+ return style_wrap(rb_class_of(self), new_handle);
136
+ }
137
+
138
+ static VALUE style_border_left_background(VALUE self, VALUE color) {
139
+ GET_STYLE(self, style);
140
+ Check_Type(color, T_STRING);
141
+ unsigned long long new_handle = lipgloss_style_border_left_background(style->handle, StringValueCStr(color));
142
+ return style_wrap(rb_class_of(self), new_handle);
143
+ }
144
+
145
+ static VALUE style_border_custom(int argc, VALUE *argv, VALUE self) {
146
+ GET_STYLE(self, style);
147
+
148
+ VALUE opts;
149
+ rb_scan_args(argc, argv, "0:", &opts);
150
+
151
+ if (NIL_P(opts)) {
152
+ rb_raise(rb_eArgError, "keyword arguments required");
153
+ }
154
+
155
+ VALUE top = rb_hash_aref(opts, ID2SYM(rb_intern("top")));
156
+ VALUE bottom = rb_hash_aref(opts, ID2SYM(rb_intern("bottom")));
157
+ VALUE left = rb_hash_aref(opts, ID2SYM(rb_intern("left")));
158
+ VALUE right = rb_hash_aref(opts, ID2SYM(rb_intern("right")));
159
+ VALUE top_left = rb_hash_aref(opts, ID2SYM(rb_intern("top_left")));
160
+ VALUE top_right = rb_hash_aref(opts, ID2SYM(rb_intern("top_right")));
161
+ VALUE bottom_left = rb_hash_aref(opts, ID2SYM(rb_intern("bottom_left")));
162
+ VALUE bottom_right = rb_hash_aref(opts, ID2SYM(rb_intern("bottom_right")));
163
+ VALUE middle_left = rb_hash_aref(opts, ID2SYM(rb_intern("middle_left")));
164
+ VALUE middle_right = rb_hash_aref(opts, ID2SYM(rb_intern("middle_right")));
165
+ VALUE middle = rb_hash_aref(opts, ID2SYM(rb_intern("middle")));
166
+ VALUE middle_top = rb_hash_aref(opts, ID2SYM(rb_intern("middle_top")));
167
+ VALUE middle_bottom = rb_hash_aref(opts, ID2SYM(rb_intern("middle_bottom")));
168
+
169
+ unsigned long long new_handle = lipgloss_style_border_custom(
170
+ style->handle,
171
+ NIL_P(top) ? "" : StringValueCStr(top),
172
+ NIL_P(bottom) ? "" : StringValueCStr(bottom),
173
+ NIL_P(left) ? "" : StringValueCStr(left),
174
+ NIL_P(right) ? "" : StringValueCStr(right),
175
+ NIL_P(top_left) ? "" : StringValueCStr(top_left),
176
+ NIL_P(top_right) ? "" : StringValueCStr(top_right),
177
+ NIL_P(bottom_left) ? "" : StringValueCStr(bottom_left),
178
+ NIL_P(bottom_right) ? "" : StringValueCStr(bottom_right),
179
+ NIL_P(middle_left) ? "" : StringValueCStr(middle_left),
180
+ NIL_P(middle_right) ? "" : StringValueCStr(middle_right),
181
+ NIL_P(middle) ? "" : StringValueCStr(middle),
182
+ NIL_P(middle_top) ? "" : StringValueCStr(middle_top),
183
+ NIL_P(middle_bottom) ? "" : StringValueCStr(middle_bottom)
184
+ );
185
+
186
+ return style_wrap(rb_class_of(self), new_handle);
187
+ }
188
+
189
+ void register_style_border_methods(void) {
190
+ rb_define_method(cStyle, "border", style_border, -1);
191
+ rb_define_method(cStyle, "border_style", style_border_style, 1);
192
+ rb_define_method(cStyle, "border_foreground", style_border_foreground, 1);
193
+ rb_define_method(cStyle, "border_background", style_border_background, 1);
194
+ rb_define_method(cStyle, "border_top", style_border_top, 1);
195
+ rb_define_method(cStyle, "border_right", style_border_right, 1);
196
+ rb_define_method(cStyle, "border_bottom", style_border_bottom, 1);
197
+ rb_define_method(cStyle, "border_left", style_border_left, 1);
198
+
199
+ rb_define_method(cStyle, "border_top_foreground", style_border_top_foreground, 1);
200
+ rb_define_method(cStyle, "border_right_foreground", style_border_right_foreground, 1);
201
+ rb_define_method(cStyle, "border_bottom_foreground", style_border_bottom_foreground, 1);
202
+ rb_define_method(cStyle, "border_left_foreground", style_border_left_foreground, 1);
203
+ rb_define_method(cStyle, "border_top_background", style_border_top_background, 1);
204
+ rb_define_method(cStyle, "border_right_background", style_border_right_background, 1);
205
+ rb_define_method(cStyle, "border_bottom_background", style_border_bottom_background, 1);
206
+ rb_define_method(cStyle, "border_left_background", style_border_left_background, 1);
207
+
208
+ rb_define_method(cStyle, "border_custom", style_border_custom, -1);
209
+ }
@@ -0,0 +1,97 @@
1
+ #include "extension.h"
2
+
3
+ static VALUE style_padding(int argc, VALUE *argv, VALUE self) {
4
+ GET_STYLE(self, style);
5
+
6
+ if (argc == 0 || argc > 4) {
7
+ rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..4)", argc);
8
+ }
9
+
10
+ int values[4];
11
+ for (int index = 0; index < argc; index++) {
12
+ values[index] = NUM2INT(argv[index]);
13
+ }
14
+
15
+ unsigned long long new_handle = lipgloss_style_padding(style->handle, values, argc);
16
+
17
+ return style_wrap(rb_class_of(self), new_handle);
18
+ }
19
+
20
+ static VALUE style_padding_top(VALUE self, VALUE value) {
21
+ GET_STYLE(self, style);
22
+ unsigned long long new_handle = lipgloss_style_padding_top(style->handle, NUM2INT(value));
23
+ return style_wrap(rb_class_of(self), new_handle);
24
+ }
25
+
26
+ static VALUE style_padding_right(VALUE self, VALUE value) {
27
+ GET_STYLE(self, style);
28
+ unsigned long long new_handle = lipgloss_style_padding_right(style->handle, NUM2INT(value));
29
+ return style_wrap(rb_class_of(self), new_handle);
30
+ }
31
+
32
+ static VALUE style_padding_bottom(VALUE self, VALUE value) {
33
+ GET_STYLE(self, style);
34
+ unsigned long long new_handle = lipgloss_style_padding_bottom(style->handle, NUM2INT(value));
35
+ return style_wrap(rb_class_of(self), new_handle);
36
+ }
37
+
38
+ static VALUE style_padding_left(VALUE self, VALUE value) {
39
+ GET_STYLE(self, style);
40
+ unsigned long long new_handle = lipgloss_style_padding_left(style->handle, NUM2INT(value));
41
+ return style_wrap(rb_class_of(self), new_handle);
42
+ }
43
+
44
+ static VALUE style_margin(int argc, VALUE *argv, VALUE self) {
45
+ GET_STYLE(self, style);
46
+
47
+ if (argc == 0 || argc > 4) {
48
+ rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..4)", argc);
49
+ }
50
+
51
+ int values[4];
52
+ for (int index = 0; index < argc; index++) {
53
+ values[index] = NUM2INT(argv[index]);
54
+ }
55
+
56
+ unsigned long long new_handle = lipgloss_style_margin(style->handle, values, argc);
57
+
58
+ return style_wrap(rb_class_of(self), new_handle);
59
+ }
60
+
61
+ static VALUE style_margin_top(VALUE self, VALUE value) {
62
+ GET_STYLE(self, style);
63
+ unsigned long long new_handle = lipgloss_style_margin_top(style->handle, NUM2INT(value));
64
+ return style_wrap(rb_class_of(self), new_handle);
65
+ }
66
+
67
+ static VALUE style_margin_right(VALUE self, VALUE value) {
68
+ GET_STYLE(self, style);
69
+ unsigned long long new_handle = lipgloss_style_margin_right(style->handle, NUM2INT(value));
70
+ return style_wrap(rb_class_of(self), new_handle);
71
+ }
72
+
73
+ static VALUE style_margin_bottom(VALUE self, VALUE value) {
74
+ GET_STYLE(self, style);
75
+ unsigned long long new_handle = lipgloss_style_margin_bottom(style->handle, NUM2INT(value));
76
+ return style_wrap(rb_class_of(self), new_handle);
77
+ }
78
+
79
+ static VALUE style_margin_left(VALUE self, VALUE value) {
80
+ GET_STYLE(self, style);
81
+ unsigned long long new_handle = lipgloss_style_margin_left(style->handle, NUM2INT(value));
82
+ return style_wrap(rb_class_of(self), new_handle);
83
+ }
84
+
85
+ void register_style_spacing_methods(void) {
86
+ rb_define_method(cStyle, "padding", style_padding, -1);
87
+ rb_define_method(cStyle, "padding_top", style_padding_top, 1);
88
+ rb_define_method(cStyle, "padding_right", style_padding_right, 1);
89
+ rb_define_method(cStyle, "padding_bottom", style_padding_bottom, 1);
90
+ rb_define_method(cStyle, "padding_left", style_padding_left, 1);
91
+
92
+ rb_define_method(cStyle, "margin", style_margin, -1);
93
+ rb_define_method(cStyle, "margin_top", style_margin_top, 1);
94
+ rb_define_method(cStyle, "margin_right", style_margin_right, 1);
95
+ rb_define_method(cStyle, "margin_bottom", style_margin_bottom, 1);
96
+ rb_define_method(cStyle, "margin_left", style_margin_left, 1);
97
+ }