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,150 @@
1
+ #include "extension.h"
2
+
3
+ VALUE mLipgloss;
4
+ VALUE cStyle;
5
+ VALUE cTable;
6
+ VALUE cList;
7
+ VALUE cTree;
8
+
9
+ static VALUE lipgloss_join_horizontal_rb(VALUE self, VALUE position, VALUE strings) {
10
+ Check_Type(strings, T_ARRAY);
11
+
12
+ VALUE json_str = rb_funcall(strings, rb_intern("to_json"), 0);
13
+ char *result = lipgloss_join_horizontal(NUM2DBL(position), StringValueCStr(json_str));
14
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
15
+
16
+ lipgloss_free(result);
17
+
18
+ return rb_result;
19
+ }
20
+
21
+ static VALUE lipgloss_join_vertical_rb(VALUE self, VALUE position, VALUE strings) {
22
+ Check_Type(strings, T_ARRAY);
23
+
24
+ VALUE json_str = rb_funcall(strings, rb_intern("to_json"), 0);
25
+ char *result = lipgloss_join_vertical(NUM2DBL(position), StringValueCStr(json_str));
26
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
27
+
28
+ lipgloss_free(result);
29
+
30
+ return rb_result;
31
+ }
32
+
33
+ static VALUE lipgloss_width_rb(VALUE self, VALUE string) {
34
+ Check_Type(string, T_STRING);
35
+
36
+ return INT2NUM(lipgloss_width(StringValueCStr(string)));
37
+ }
38
+
39
+ static VALUE lipgloss_height_rb(VALUE self, VALUE string) {
40
+ Check_Type(string, T_STRING);
41
+
42
+ return INT2NUM(lipgloss_height(StringValueCStr(string)));
43
+ }
44
+
45
+ static VALUE lipgloss_size_rb(VALUE self, VALUE string) {
46
+ Check_Type(string, T_STRING);
47
+ char *string_cstr = StringValueCStr(string);
48
+
49
+ VALUE width = INT2NUM(lipgloss_width(string_cstr));
50
+ VALUE height = INT2NUM(lipgloss_height(string_cstr));
51
+
52
+ return rb_ary_new_from_args(2, width, height);
53
+ }
54
+
55
+ static VALUE lipgloss_place_rb(int argc, VALUE *argv, VALUE self) {
56
+ VALUE width, height, horizontal_position, vertical_position, string;
57
+ rb_scan_args(argc, argv, "5", &width, &height, &horizontal_position, &vertical_position, &string);
58
+
59
+ Check_Type(string, T_STRING);
60
+
61
+ char *result = lipgloss_place(
62
+ NUM2INT(width),
63
+ NUM2INT(height),
64
+ NUM2DBL(horizontal_position),
65
+ NUM2DBL(vertical_position),
66
+ StringValueCStr(string)
67
+ );
68
+
69
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
70
+
71
+ lipgloss_free(result);
72
+
73
+ return rb_result;
74
+ }
75
+
76
+ static VALUE lipgloss_place_horizontal_rb(VALUE self, VALUE width, VALUE position, VALUE string) {
77
+ Check_Type(string, T_STRING);
78
+
79
+ char *result = lipgloss_place_horizontal(
80
+ NUM2INT(width),
81
+ NUM2DBL(position),
82
+ StringValueCStr(string)
83
+ );
84
+
85
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
86
+
87
+ lipgloss_free(result);
88
+
89
+ return rb_result;
90
+ }
91
+
92
+ static VALUE lipgloss_place_vertical_rb(VALUE self, VALUE height, VALUE position, VALUE string) {
93
+ Check_Type(string, T_STRING);
94
+
95
+ char *result = lipgloss_place_vertical(
96
+ NUM2INT(height),
97
+ NUM2DBL(position),
98
+ StringValueCStr(string)
99
+ );
100
+
101
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
102
+
103
+ lipgloss_free(result);
104
+
105
+ return rb_result;
106
+ }
107
+
108
+ static VALUE lipgloss_has_dark_background_rb(VALUE self) {
109
+ return lipgloss_has_dark_background() ? Qtrue : Qfalse;
110
+ }
111
+
112
+ static VALUE lipgloss_upstream_version_rb(VALUE self) {
113
+ char *version = lipgloss_upstream_version();
114
+ VALUE rb_version = rb_utf8_str_new_cstr(version);
115
+
116
+ lipgloss_free(version);
117
+
118
+ return rb_version;
119
+ }
120
+
121
+ static VALUE lipgloss_version_rb(VALUE self) {
122
+ VALUE gem_version = rb_const_get(self, rb_intern("VERSION"));
123
+ VALUE upstream_version = lipgloss_upstream_version_rb(self);
124
+ VALUE format_string = rb_utf8_str_new_cstr("lipgloss v%s (upstream v%s) [Go native extension]");
125
+
126
+ return rb_funcall(rb_mKernel, rb_intern("sprintf"), 3, format_string, gem_version, upstream_version);
127
+ }
128
+
129
+ __attribute__((__visibility__("default"))) void Init_lipgloss(void) {
130
+ rb_require("json");
131
+
132
+ mLipgloss = rb_define_module("Lipgloss");
133
+
134
+ Init_lipgloss_style();
135
+ Init_lipgloss_table();
136
+ Init_lipgloss_list();
137
+ Init_lipgloss_tree();
138
+
139
+ rb_define_singleton_method(mLipgloss, "join_horizontal", lipgloss_join_horizontal_rb, 2);
140
+ rb_define_singleton_method(mLipgloss, "join_vertical", lipgloss_join_vertical_rb, 2);
141
+ rb_define_singleton_method(mLipgloss, "width", lipgloss_width_rb, 1);
142
+ rb_define_singleton_method(mLipgloss, "height", lipgloss_height_rb, 1);
143
+ rb_define_singleton_method(mLipgloss, "size", lipgloss_size_rb, 1);
144
+ rb_define_singleton_method(mLipgloss, "place", lipgloss_place_rb, -1);
145
+ rb_define_singleton_method(mLipgloss, "place_horizontal", lipgloss_place_horizontal_rb, 3);
146
+ rb_define_singleton_method(mLipgloss, "place_vertical", lipgloss_place_vertical_rb, 3);
147
+ rb_define_singleton_method(mLipgloss, "has_dark_background?", lipgloss_has_dark_background_rb, 0);
148
+ rb_define_singleton_method(mLipgloss, "upstream_version", lipgloss_upstream_version_rb, 0);
149
+ rb_define_singleton_method(mLipgloss, "version", lipgloss_version_rb, 0);
150
+ }
@@ -0,0 +1,76 @@
1
+ #ifndef LIPGLOSS_EXTENSION_H
2
+ #define LIPGLOSS_EXTENSION_H
3
+
4
+ #include <ruby.h>
5
+ #include "liblipgloss.h"
6
+
7
+ extern VALUE mLipgloss;
8
+ extern VALUE cStyle;
9
+ extern VALUE cTable;
10
+ extern VALUE cList;
11
+ extern VALUE cTree;
12
+
13
+ extern const rb_data_type_t style_type;
14
+ extern const rb_data_type_t table_type;
15
+ extern const rb_data_type_t list_type;
16
+ extern const rb_data_type_t tree_type;
17
+
18
+ typedef struct {
19
+ unsigned long long handle;
20
+ } lipgloss_style_t;
21
+
22
+ typedef struct {
23
+ unsigned long long handle;
24
+ } lipgloss_table_t;
25
+
26
+ typedef struct {
27
+ unsigned long long handle;
28
+ } lipgloss_list_t;
29
+
30
+ typedef struct {
31
+ unsigned long long handle;
32
+ } lipgloss_tree_t;
33
+
34
+
35
+ #define GET_STYLE(self, style) \
36
+ lipgloss_style_t *style; \
37
+ TypedData_Get_Struct(self, lipgloss_style_t, &style_type, style)
38
+
39
+ #define GET_TABLE(self, table) \
40
+ lipgloss_table_t *table; \
41
+ TypedData_Get_Struct(self, lipgloss_table_t, &table_type, table)
42
+
43
+ #define GET_LIST(self, list) \
44
+ lipgloss_list_t *list; \
45
+ TypedData_Get_Struct(self, lipgloss_list_t, &list_type, list)
46
+
47
+ #define GET_TREE(self, tree) \
48
+ lipgloss_tree_t *tree; \
49
+ TypedData_Get_Struct(self, lipgloss_tree_t, &tree_type, tree)
50
+
51
+ #define BORDER_NORMAL 0
52
+ #define BORDER_ROUNDED 1
53
+ #define BORDER_THICK 2
54
+ #define BORDER_DOUBLE 3
55
+ #define BORDER_HIDDEN 4
56
+ #define BORDER_BLOCK 5
57
+ #define BORDER_OUTER_HALF_BLOCK 6
58
+ #define BORDER_INNER_HALF_BLOCK 7
59
+ #define BORDER_ASCII 8
60
+ #define BORDER_MARKDOWN 9
61
+
62
+ VALUE style_wrap(VALUE klass, unsigned long long handle);
63
+ VALUE table_wrap(VALUE klass, unsigned long long handle);
64
+ VALUE list_wrap_handle(VALUE klass, unsigned long long handle);
65
+ VALUE tree_wrap_handle(VALUE klass, unsigned long long handle);
66
+
67
+ void Init_lipgloss_style(void);
68
+ void Init_lipgloss_table(void);
69
+ void Init_lipgloss_list(void);
70
+ void Init_lipgloss_tree(void);
71
+
72
+ void register_style_spacing_methods(void);
73
+ void register_style_border_methods(void);
74
+ void register_style_unset_methods(void);
75
+
76
+ #endif
@@ -0,0 +1,147 @@
1
+ #include "extension.h"
2
+
3
+ static void list_free(void *pointer) {
4
+ lipgloss_list_t *list = (lipgloss_list_t *) pointer;
5
+
6
+ if (list->handle != 0) {
7
+ lipgloss_list_free(list->handle);
8
+ }
9
+
10
+ xfree(list);
11
+ }
12
+
13
+ static size_t list_memsize(const void *pointer) {
14
+ return sizeof(lipgloss_list_t);
15
+ }
16
+
17
+ const rb_data_type_t list_type = {
18
+ .wrap_struct_name = "Lipgloss::List",
19
+ .function = {
20
+ .dmark = NULL,
21
+ .dfree = list_free,
22
+ .dsize = list_memsize,
23
+ },
24
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
25
+ };
26
+
27
+ static VALUE list_alloc(VALUE klass) {
28
+ lipgloss_list_t *list = ALLOC(lipgloss_list_t);
29
+ list->handle = lipgloss_list_new();
30
+
31
+ return TypedData_Wrap_Struct(klass, &list_type, list);
32
+ }
33
+
34
+ VALUE list_wrap_handle(VALUE klass, unsigned long long handle) {
35
+ lipgloss_list_t *list = ALLOC(lipgloss_list_t);
36
+ list->handle = handle;
37
+
38
+ return TypedData_Wrap_Struct(klass, &list_type, list);
39
+ }
40
+
41
+ static VALUE list_initialize(int argc, VALUE *argv, VALUE self) {
42
+ if (argc > 0) {
43
+ GET_LIST(self, list);
44
+ VALUE json_str = rb_funcall(rb_ary_new_from_values(argc, argv), rb_intern("to_json"), 0);
45
+ list->handle = lipgloss_list_items(list->handle, StringValueCStr(json_str));
46
+ }
47
+
48
+ return self;
49
+ }
50
+
51
+ static VALUE list_item(VALUE self, VALUE item) {
52
+ GET_LIST(self, list);
53
+
54
+ if (rb_obj_is_kind_of(item, cList)) {
55
+ lipgloss_list_t *sublist;
56
+ TypedData_Get_Struct(item, lipgloss_list_t, &list_type, sublist);
57
+ unsigned long long new_handle = lipgloss_list_item_list(list->handle, sublist->handle);
58
+
59
+ return list_wrap_handle(rb_class_of(self), new_handle);
60
+ }
61
+
62
+ Check_Type(item, T_STRING);
63
+ unsigned long long new_handle = lipgloss_list_item(list->handle, StringValueCStr(item));
64
+
65
+ return list_wrap_handle(rb_class_of(self), new_handle);
66
+ }
67
+
68
+ static VALUE list_items(VALUE self, VALUE items) {
69
+ GET_LIST(self, list);
70
+ Check_Type(items, T_ARRAY);
71
+
72
+ VALUE json_str = rb_funcall(items, rb_intern("to_json"), 0);
73
+ unsigned long long new_handle = lipgloss_list_items(list->handle, StringValueCStr(json_str));
74
+
75
+ return list_wrap_handle(rb_class_of(self), new_handle);
76
+ }
77
+
78
+ #define LIST_ENUMERATOR_BULLET 0
79
+ #define LIST_ENUMERATOR_ARABIC 1
80
+ #define LIST_ENUMERATOR_ALPHABET 2
81
+ #define LIST_ENUMERATOR_ROMAN 3
82
+ #define LIST_ENUMERATOR_DASH 4
83
+ #define LIST_ENUMERATOR_ASTERISK 5
84
+
85
+ static int symbol_to_list_enumerator(VALUE symbol) {
86
+ if (symbol == ID2SYM(rb_intern("bullet"))) return LIST_ENUMERATOR_BULLET;
87
+ if (symbol == ID2SYM(rb_intern("arabic"))) return LIST_ENUMERATOR_ARABIC;
88
+ if (symbol == ID2SYM(rb_intern("alphabet"))) return LIST_ENUMERATOR_ALPHABET;
89
+ if (symbol == ID2SYM(rb_intern("roman"))) return LIST_ENUMERATOR_ROMAN;
90
+ if (symbol == ID2SYM(rb_intern("dash"))) return LIST_ENUMERATOR_DASH;
91
+ if (symbol == ID2SYM(rb_intern("asterisk"))) return LIST_ENUMERATOR_ASTERISK;
92
+
93
+ return LIST_ENUMERATOR_BULLET;
94
+ }
95
+
96
+ static VALUE list_enumerator(VALUE self, VALUE enum_symbol) {
97
+ GET_LIST(self, list);
98
+ int enum_type = symbol_to_list_enumerator(enum_symbol);
99
+ unsigned long long new_handle = lipgloss_list_enumerator(list->handle, enum_type);
100
+
101
+ return list_wrap_handle(rb_class_of(self), new_handle);
102
+ }
103
+
104
+ static VALUE list_enumerator_style(VALUE self, VALUE style_object) {
105
+ GET_LIST(self, list);
106
+ lipgloss_style_t *style;
107
+
108
+ TypedData_Get_Struct(style_object, lipgloss_style_t, &style_type, style);
109
+ unsigned long long new_handle = lipgloss_list_enumerator_style(list->handle, style->handle);
110
+
111
+ return list_wrap_handle(rb_class_of(self), new_handle);
112
+ }
113
+
114
+ static VALUE list_item_style(VALUE self, VALUE style_object) {
115
+ GET_LIST(self, list);
116
+ lipgloss_style_t *style;
117
+ TypedData_Get_Struct(style_object, lipgloss_style_t, &style_type, style);
118
+ unsigned long long new_handle = lipgloss_list_item_style(list->handle, style->handle);
119
+ return list_wrap_handle(rb_class_of(self), new_handle);
120
+ }
121
+
122
+ static VALUE list_render(VALUE self) {
123
+ GET_LIST(self, list);
124
+ char *result = lipgloss_list_render(list->handle);
125
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
126
+ lipgloss_free(result);
127
+ return rb_result;
128
+ }
129
+
130
+ static VALUE list_to_s(VALUE self) {
131
+ return list_render(self);
132
+ }
133
+
134
+ void Init_lipgloss_list(void) {
135
+ cList = rb_define_class_under(mLipgloss, "List", rb_cObject);
136
+
137
+ rb_define_alloc_func(cList, list_alloc);
138
+
139
+ rb_define_method(cList, "initialize", list_initialize, -1);
140
+ rb_define_method(cList, "item", list_item, 1);
141
+ rb_define_method(cList, "items", list_items, 1);
142
+ rb_define_method(cList, "enumerator", list_enumerator, 1);
143
+ rb_define_method(cList, "enumerator_style", list_enumerator_style, 1);
144
+ rb_define_method(cList, "item_style", list_item_style, 1);
145
+ rb_define_method(cList, "render", list_render, 0);
146
+ rb_define_method(cList, "to_s", list_to_s, 0);
147
+ }