lipgloss 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be9e2e8c665f23da1ba5aa8d5b934d65df1ee974ba3d97e5e294d3211b1afa16
4
- data.tar.gz: efe00d6790c979a2fadf2f1e8eacd3289662ed2f05a1700132b76dafb4e8657b
3
+ metadata.gz: 64298cd49ffbe321b89fd15467e5a2f8a004df9766a3c496503deda02570d5a1
4
+ data.tar.gz: d5cc36929140d8447811478accbf01bf1ca34579e3121ba019c577b80e321221
5
5
  SHA512:
6
- metadata.gz: f188d487f00874ebf6c572a7640b98d03ec3403c4fe6b187aed2581b88952f18f6d9481e325ba5b0851ac2133c2a75647b8513c8ddbc2e060a62adfdb451bda8
7
- data.tar.gz: a4f376335a343fa269e32ed14ebc178206370962e3c102e26305d94218e3bba6713732432eb327b2a88cc0c63a189618c9d2dffa97d4425c28e966d6e15d92eb
6
+ metadata.gz: e1dfd94aaf1ab37927e9da42f64726a59cd78cbf15f8a559ecd333f81f589e7f06b3ac056639970970147dca46bf429868d993003bd2cbe073d1bbb06bf35cba
7
+ data.tar.gz: 576fa3a0014de56d0f0f82159b79ef35b7e2a22e52fd9ce9c24790be9e3d1da999242cd47b8087f35e5f3882ef04a4258a359a99b6c1c73bd543a2672d289f62
data/README.md CHANGED
@@ -176,6 +176,17 @@ color = Lipgloss::CompleteAdaptiveColor.new(light: light, dark: dark)
176
176
  style = Lipgloss::Style.new.foreground(color)
177
177
  ```
178
178
 
179
+ **Color profile:**
180
+
181
+ Lip Gloss detects how many colors the terminal supports when it first renders. Set the profile to force colors on, for example in CI or golden tests, or off:
182
+
183
+ ```ruby
184
+ Lipgloss.color_profile # => :true_color, :ansi256, :ansi, or :ascii
185
+ Lipgloss.color_profile = :true_color # always render colors
186
+ Lipgloss.color_profile = :ascii # never render colors
187
+ Lipgloss.has_dark_background = true # pick the dark variant of adaptive colors
188
+ ```
189
+
179
190
  ### Style Inheritance
180
191
 
181
192
  **Inherit styles from a parent:**
@@ -415,6 +426,9 @@ puts tree.render
415
426
  | `Lipgloss.place_horizontal(width, position, string)` | Place horizontally |
416
427
  | `Lipgloss.place_vertical(height, position, string)` | Place vertically |
417
428
  | `Lipgloss.has_dark_background?` | Check terminal background |
429
+ | `Lipgloss.has_dark_background = bool` | Set terminal background |
430
+ | `Lipgloss.color_profile` | Get the color profile |
431
+ | `Lipgloss.color_profile = profile` | Set the color profile |
418
432
 
419
433
  ### Color Blending
420
434
 
@@ -150,6 +150,20 @@ static VALUE lipgloss_has_dark_background_rb(VALUE self) {
150
150
  return lipgloss_has_dark_background() ? Qtrue : Qfalse;
151
151
  }
152
152
 
153
+ static VALUE lipgloss_color_profile_rb(VALUE self) {
154
+ return INT2NUM(lipgloss_color_profile());
155
+ }
156
+
157
+ static VALUE lipgloss_set_color_profile_rb(VALUE self, VALUE profile) {
158
+ lipgloss_set_color_profile(NUM2INT(profile));
159
+ return profile;
160
+ }
161
+
162
+ static VALUE lipgloss_set_has_dark_background_rb(VALUE self, VALUE dark) {
163
+ lipgloss_set_has_dark_background(RTEST(dark) ? 1 : 0);
164
+ return dark;
165
+ }
166
+
153
167
  static VALUE lipgloss_upstream_version_rb(VALUE self) {
154
168
  char *version = lipgloss_upstream_version();
155
169
  VALUE rb_version = rb_utf8_str_new_cstr(version);
@@ -187,6 +201,9 @@ __attribute__((__visibility__("default"))) void Init_lipgloss(void) {
187
201
  rb_define_singleton_method(mLipgloss, "_place_horizontal", lipgloss_place_horizontal_rb, 3);
188
202
  rb_define_singleton_method(mLipgloss, "_place_vertical", lipgloss_place_vertical_rb, 3);
189
203
  rb_define_singleton_method(mLipgloss, "has_dark_background?", lipgloss_has_dark_background_rb, 0);
204
+ rb_define_singleton_method(mLipgloss, "has_dark_background=", lipgloss_set_has_dark_background_rb, 1);
205
+ rb_define_singleton_method(mLipgloss, "_color_profile", lipgloss_color_profile_rb, 0);
206
+ rb_define_singleton_method(mLipgloss, "_set_color_profile", lipgloss_set_color_profile_rb, 1);
190
207
  rb_define_singleton_method(mLipgloss, "upstream_version", lipgloss_upstream_version_rb, 0);
191
208
  rb_define_singleton_method(mLipgloss, "version", lipgloss_version_rb, 0);
192
209
  }
data/ext/lipgloss/style.c CHANGED
@@ -344,6 +344,16 @@ static VALUE style_to_s(VALUE self) {
344
344
  return rb_result;
345
345
  }
346
346
 
347
+ static VALUE style_to_json(VALUE self) {
348
+ GET_STYLE(self, style);
349
+ char *result = lipgloss_style_to_json(style->handle);
350
+ VALUE rb_result = rb_utf8_str_new_cstr(result);
351
+
352
+ lipgloss_free(result);
353
+
354
+ return rb_result;
355
+ }
356
+
347
357
  // Getter methods
348
358
 
349
359
  static VALUE style_get_bold(VALUE self) {
@@ -455,6 +465,7 @@ void Init_lipgloss_style(void) {
455
465
  rb_define_method(cStyle, "set_string", style_set_string, 1);
456
466
  rb_define_method(cStyle, "inherit", style_inherit, 1);
457
467
  rb_define_method(cStyle, "to_s", style_to_s, 0);
468
+ rb_define_method(cStyle, "_to_json", style_to_json, 0);
458
469
 
459
470
  rb_define_method(cStyle, "bold?", style_get_bold, 0);
460
471
  rb_define_method(cStyle, "italic?", style_get_italic, 0);
data/ext/lipgloss/table.c CHANGED
@@ -40,29 +40,49 @@ static VALUE table_initialize(VALUE self) {
40
40
  return self;
41
41
  }
42
42
 
43
+ static VALUE table_cells_to_s(VALUE cells) {
44
+ Check_Type(cells, T_ARRAY);
45
+
46
+ long length = RARRAY_LEN(cells);
47
+ VALUE result = rb_ary_new_capa(length);
48
+
49
+ for (long index = 0; index < length; index++) {
50
+ rb_ary_push(result, rb_obj_as_string(rb_ary_entry(cells, index)));
51
+ }
52
+
53
+ return result;
54
+ }
55
+
56
+ static VALUE table_rows_to_s(VALUE rows) {
57
+ Check_Type(rows, T_ARRAY);
58
+
59
+ long length = RARRAY_LEN(rows);
60
+ VALUE result = rb_ary_new_capa(length);
61
+
62
+ for (long index = 0; index < length; index++) {
63
+ rb_ary_push(result, table_cells_to_s(rb_ary_entry(rows, index)));
64
+ }
65
+
66
+ return result;
67
+ }
68
+
43
69
  static VALUE table_headers(VALUE self, VALUE headers) {
44
70
  GET_TABLE(self, table);
45
- Check_Type(headers, T_ARRAY);
46
-
47
- VALUE json_str = rb_funcall(headers, rb_intern("to_json"), 0);
71
+ VALUE json_str = rb_funcall(table_cells_to_s(headers), rb_intern("to_json"), 0);
48
72
  unsigned long long new_handle = lipgloss_table_headers(table->handle, StringValueCStr(json_str));
49
73
  return table_wrap(rb_class_of(self), new_handle);
50
74
  }
51
75
 
52
76
  static VALUE table_row(VALUE self, VALUE row) {
53
77
  GET_TABLE(self, table);
54
- Check_Type(row, T_ARRAY);
55
-
56
- VALUE json_str = rb_funcall(row, rb_intern("to_json"), 0);
78
+ VALUE json_str = rb_funcall(table_cells_to_s(row), rb_intern("to_json"), 0);
57
79
  unsigned long long new_handle = lipgloss_table_row(table->handle, StringValueCStr(json_str));
58
80
  return table_wrap(rb_class_of(self), new_handle);
59
81
  }
60
82
 
61
83
  static VALUE table_rows(VALUE self, VALUE rows) {
62
84
  GET_TABLE(self, table);
63
- Check_Type(rows, T_ARRAY);
64
-
65
- VALUE json_str = rb_funcall(rows, rb_intern("to_json"), 0);
85
+ VALUE json_str = rb_funcall(table_rows_to_s(rows), rb_intern("to_json"), 0);
66
86
  unsigned long long new_handle = lipgloss_table_rows(table->handle, StringValueCStr(json_str));
67
87
 
68
88
  return table_wrap(rb_class_of(self), new_handle);
data/go/go.mod CHANGED
@@ -2,7 +2,11 @@ module github.com/marcoroth/lipgloss-ruby/go
2
2
 
3
3
  go 1.23.0
4
4
 
5
- require github.com/charmbracelet/lipgloss v1.1.0
5
+ require (
6
+ github.com/charmbracelet/lipgloss v1.1.0
7
+ github.com/lucasb-eyer/go-colorful v1.2.0
8
+ github.com/muesli/termenv v0.16.0
9
+ )
6
10
 
7
11
  require (
8
12
  github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
@@ -10,10 +14,8 @@ require (
10
14
  github.com/charmbracelet/x/ansi v0.8.0 // indirect
11
15
  github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
12
16
  github.com/charmbracelet/x/term v0.2.1 // indirect
13
- github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
14
17
  github.com/mattn/go-isatty v0.0.20 // indirect
15
18
  github.com/mattn/go-runewidth v0.0.16 // indirect
16
- github.com/muesli/termenv v0.16.0 // indirect
17
19
  github.com/rivo/uniseg v0.4.7 // indirect
18
20
  github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
19
21
  golang.org/x/sys v0.30.0 // indirect
data/go/layout.go CHANGED
@@ -5,6 +5,7 @@ import "C"
5
5
  import (
6
6
  "encoding/json"
7
7
  "github.com/charmbracelet/lipgloss"
8
+ "github.com/muesli/termenv"
8
9
  )
9
10
 
10
11
  //export lipgloss_join_horizontal
@@ -111,3 +112,18 @@ func lipgloss_has_dark_background() C.int {
111
112
 
112
113
  return 0
113
114
  }
115
+
116
+ //export lipgloss_color_profile
117
+ func lipgloss_color_profile() C.int {
118
+ return C.int(lipgloss.ColorProfile())
119
+ }
120
+
121
+ //export lipgloss_set_color_profile
122
+ func lipgloss_set_color_profile(profile C.int) {
123
+ lipgloss.SetColorProfile(termenv.Profile(profile))
124
+ }
125
+
126
+ //export lipgloss_set_has_dark_background
127
+ func lipgloss_set_has_dark_background(dark C.int) {
128
+ lipgloss.SetHasDarkBackground(dark != 0)
129
+ }
data/go/style_hash.go ADDED
@@ -0,0 +1,144 @@
1
+ package main
2
+
3
+ import "C"
4
+
5
+ import (
6
+ "encoding/json"
7
+
8
+ "github.com/charmbracelet/lipgloss"
9
+ )
10
+
11
+ func colorToValue(tc lipgloss.TerminalColor) any {
12
+ switch c := tc.(type) {
13
+ case nil, lipgloss.NoColor:
14
+ return nil
15
+ case lipgloss.Color:
16
+ if c == "" {
17
+ return nil
18
+ }
19
+ return string(c)
20
+ case lipgloss.AdaptiveColor:
21
+ return map[string]any{"light": c.Light, "dark": c.Dark}
22
+ case lipgloss.CompleteColor:
23
+ return map[string]any{"true_color": c.TrueColor, "ansi256": c.ANSI256, "ansi": c.ANSI}
24
+ case lipgloss.CompleteAdaptiveColor:
25
+ return map[string]any{
26
+ "light": map[string]any{"true_color": c.Light.TrueColor, "ansi256": c.Light.ANSI256, "ansi": c.Light.ANSI},
27
+ "dark": map[string]any{"true_color": c.Dark.TrueColor, "ansi256": c.Dark.ANSI256, "ansi": c.Dark.ANSI},
28
+ }
29
+ }
30
+ return nil
31
+ }
32
+
33
+ func borderToValue(b lipgloss.Border) map[string]any {
34
+ return map[string]any{
35
+ "top": b.Top,
36
+ "bottom": b.Bottom,
37
+ "left": b.Left,
38
+ "right": b.Right,
39
+ "top_left": b.TopLeft,
40
+ "top_right": b.TopRight,
41
+ "bottom_left": b.BottomLeft,
42
+ "bottom_right": b.BottomRight,
43
+ "middle_left": b.MiddleLeft,
44
+ "middle_right": b.MiddleRight,
45
+ "middle": b.Middle,
46
+ "middle_top": b.MiddleTop,
47
+ "middle_bottom": b.MiddleBottom,
48
+ }
49
+ }
50
+
51
+ func setIf(h map[string]any, key string, value any) {
52
+ switch v := value.(type) {
53
+ case nil:
54
+ return
55
+ case bool:
56
+ if !v {
57
+ return
58
+ }
59
+ case int:
60
+ if v == 0 {
61
+ return
62
+ }
63
+ }
64
+ h[key] = value
65
+ }
66
+
67
+ func styleToHash(s lipgloss.Style) map[string]any {
68
+ h := map[string]any{}
69
+
70
+ setIf(h, "bold", s.GetBold())
71
+ setIf(h, "italic", s.GetItalic())
72
+ setIf(h, "underline", s.GetUnderline())
73
+ setIf(h, "strikethrough", s.GetStrikethrough())
74
+ setIf(h, "reverse", s.GetReverse())
75
+ setIf(h, "blink", s.GetBlink())
76
+ setIf(h, "faint", s.GetFaint())
77
+ setIf(h, "inline", s.GetInline())
78
+
79
+ setIf(h, "foreground", colorToValue(s.GetForeground()))
80
+ setIf(h, "background", colorToValue(s.GetBackground()))
81
+
82
+ setIf(h, "width", s.GetWidth())
83
+ setIf(h, "height", s.GetHeight())
84
+ setIf(h, "max_width", s.GetMaxWidth())
85
+ setIf(h, "max_height", s.GetMaxHeight())
86
+
87
+ if horizontal := s.GetAlignHorizontal(); horizontal != lipgloss.Left {
88
+ h["align_horizontal"] = float64(horizontal)
89
+ }
90
+ if vertical := s.GetAlignVertical(); vertical != lipgloss.Top {
91
+ h["align_vertical"] = float64(vertical)
92
+ }
93
+
94
+ if t, r, b, l := s.GetPadding(); t+r+b+l > 0 {
95
+ h["padding"] = []int{t, r, b, l}
96
+ }
97
+ if t, r, b, l := s.GetMargin(); t+r+b+l > 0 {
98
+ h["margin"] = []int{t, r, b, l}
99
+ }
100
+
101
+ border, top, right, bottom, left := s.GetBorder()
102
+ if top || right || bottom || left {
103
+ h["border"] = borderToValue(border)
104
+ h["border_sides"] = []bool{top, right, bottom, left}
105
+
106
+ foregrounds := []any{
107
+ colorToValue(s.GetBorderTopForeground()),
108
+ colorToValue(s.GetBorderRightForeground()),
109
+ colorToValue(s.GetBorderBottomForeground()),
110
+ colorToValue(s.GetBorderLeftForeground()),
111
+ }
112
+ backgrounds := []any{
113
+ colorToValue(s.GetBorderTopBackground()),
114
+ colorToValue(s.GetBorderRightBackground()),
115
+ colorToValue(s.GetBorderBottomBackground()),
116
+ colorToValue(s.GetBorderLeftBackground()),
117
+ }
118
+
119
+ for _, c := range foregrounds {
120
+ if c != nil {
121
+ h["border_foreground"] = foregrounds
122
+ break
123
+ }
124
+ }
125
+ for _, c := range backgrounds {
126
+ if c != nil {
127
+ h["border_background"] = backgrounds
128
+ break
129
+ }
130
+ }
131
+ }
132
+
133
+ return h
134
+ }
135
+
136
+ //export lipgloss_style_to_json
137
+ func lipgloss_style_to_json(id C.ulonglong) *C.char {
138
+ data, err := json.Marshal(styleToHash(getStyle(uint64(id))))
139
+ if err != nil {
140
+ return C.CString("{}")
141
+ }
142
+
143
+ return C.CString(string(data))
144
+ }
@@ -20,5 +20,10 @@ module Lipgloss
20
20
  def align_vertical(position)
21
21
  _align_vertical(Position.resolve(position))
22
22
  end
23
+
24
+ # @rbs return: Hash[Symbol, untyped]
25
+ def to_h
26
+ JSON.parse(_to_json, symbolize_names: true)
27
+ end
23
28
  end
24
29
  end
@@ -2,5 +2,5 @@
2
2
  # rbs_inline: enabled
3
3
 
4
4
  module Lipgloss
5
- VERSION = "0.2.2" #: String
5
+ VERSION = "0.2.3" #: String
6
6
  end
data/lib/lipgloss.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  # rbs_inline: enabled
3
3
 
4
+ require "json"
4
5
  require_relative "lipgloss/version"
5
6
 
6
7
  begin
@@ -33,6 +34,11 @@ module Lipgloss
33
34
 
34
35
  NO_TAB_CONVERSION = -1 #: Integer
35
36
 
37
+ # @rbs!
38
+ # type color_profile = :true_color | :ansi256 | :ansi | :ascii
39
+
40
+ COLOR_PROFILES = [:true_color, :ansi256, :ansi, :ascii].freeze #: Array[color_profile]
41
+
36
42
  class << self
37
43
  # @rbs position: Position::position_value
38
44
  # @rbs *strings: String
@@ -73,5 +79,20 @@ module Lipgloss
73
79
  def place_vertical(height, position, string)
74
80
  _place_vertical(height, Position.resolve(position), string)
75
81
  end
82
+
83
+ # @rbs return: color_profile
84
+ def color_profile
85
+ COLOR_PROFILES.fetch(_color_profile)
86
+ end
87
+
88
+ # @rbs profile: color_profile
89
+ # @rbs return: void
90
+ def color_profile=(profile)
91
+ index = COLOR_PROFILES.index(profile)
92
+
93
+ raise ArgumentError, "unknown color profile #{profile.inspect}, expected one of #{COLOR_PROFILES.inspect}" unless index
94
+
95
+ _set_color_profile(index)
96
+ end
76
97
  end
77
98
  end
@@ -10,6 +10,9 @@ module Lipgloss
10
10
  def self._place_horizontal: (Integer width, Float position, String string) -> String
11
11
  def self._place_vertical: (Integer height, Float position, String string) -> String
12
12
  def self.has_dark_background?: () -> bool
13
+ def self.has_dark_background=: (bool dark) -> bool
14
+ def self._color_profile: () -> Integer
15
+ def self._set_color_profile: (Integer profile) -> Integer
13
16
  def self.upstream_version: () -> String
14
17
  def self.version: () -> String
15
18
 
@@ -92,6 +95,7 @@ module Lipgloss
92
95
 
93
96
  def set_string: (String string) -> Style
94
97
  def inherit: (Style other) -> Style
98
+ def _to_json: () -> String
95
99
 
96
100
  def unset_bold: () -> Style
97
101
  def unset_italic: () -> Style
@@ -118,9 +122,9 @@ module Lipgloss
118
122
 
119
123
  class Table
120
124
  def initialize: () -> void
121
- def headers: (Array[String] headers) -> Table
122
- def row: (Array[String] row) -> Table
123
- def rows: (Array[Array[String]] rows) -> Table
125
+ def headers: (Array[_ToS] headers) -> Table
126
+ def row: (Array[_ToS] row) -> Table
127
+ def rows: (Array[Array[_ToS]] rows) -> Table
124
128
  def border: (Symbol border_type) -> Table
125
129
  def border_style: (Style style) -> Table
126
130
  def border_top: (bool value) -> Table
@@ -13,5 +13,8 @@ module Lipgloss
13
13
  # @rbs position: Position::position_value
14
14
  # @rbs return: Style
15
15
  def align_vertical: (Position::position_value position) -> Style
16
+
17
+ # @rbs return: Hash[Symbol, untyped]
18
+ def to_h: () -> Hash[Symbol, untyped]
16
19
  end
17
20
  end
data/sig/lipgloss.rbs CHANGED
@@ -27,6 +27,10 @@ module Lipgloss
27
27
 
28
28
  NO_TAB_CONVERSION: Integer
29
29
 
30
+ type color_profile = :true_color | :ansi256 | :ansi | :ascii
31
+
32
+ COLOR_PROFILES: Array[color_profile]
33
+
30
34
  # @rbs position: Position::position_value
31
35
  # @rbs *strings: String
32
36
  # @rbs return: String
@@ -56,4 +60,11 @@ module Lipgloss
56
60
  # @rbs string: String
57
61
  # @rbs return: String
58
62
  def self.place_vertical: (Integer height, Position::position_value position, String string) -> String
63
+
64
+ # @rbs return: color_profile
65
+ def self.color_profile: () -> color_profile
66
+
67
+ # @rbs profile: color_profile
68
+ # @rbs return: void
69
+ def self.color_profile=: (color_profile profile) -> void
59
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lipgloss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Roth
@@ -38,6 +38,7 @@ files:
38
38
  - go/list.go
39
39
  - go/style.go
40
40
  - go/style_border.go
41
+ - go/style_hash.go
41
42
  - go/style_spacing.go
42
43
  - go/style_unset.go
43
44
  - go/table.go
@@ -80,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  - !ruby/object:Gem::Version
81
82
  version: '0'
82
83
  requirements: []
83
- rubygems_version: 4.0.3
84
+ rubygems_version: 4.0.20
84
85
  specification_version: 4
85
86
  summary: Ruby wrapper for Charm's lipgloss. CSS-like terminal styling library.
86
87
  test_files: []