@@ -49,19 +49,36 @@ local calc_container_width = function(config, node, state, context)
4949end
5050
5151local render_content = function (config , node , state , context )
52- local max_width = 0
52+ local add_padding = function (rendered_item , should_pad )
53+ for _ , data in ipairs (rendered_item ) do
54+ if data .text then
55+ local padding = (should_pad and # data .text and data .text :sub (1 , 1 ) ~= " " ) and " " or " "
56+ data .text = padding .. data .text
57+ should_pad = data .text :sub (# data .text ) ~= " "
58+ end
59+ end
60+ return should_pad
61+ end
5362
63+ local max_width = 0
5464 local grouped_by_zindex = utils .group_by (config .content , " zindex" )
65+
5566 for zindex , items in pairs (grouped_by_zindex ) do
67+ local should_pad = { left = false , right = false }
5668 local zindex_rendered = { left = {}, right = {} }
5769 local rendered_width = 0
70+
5871 for _ , item in ipairs (items ) do
5972 local rendered_item = renderer .render_component (item , node , state , context .available_width )
6073 if rendered_item then
61- vim .list_extend (zindex_rendered [item .align or " left" ], rendered_item )
74+ local align = item .align or " left"
75+ should_pad [align ] = add_padding (rendered_item , should_pad [align ])
76+
77+ vim .list_extend (zindex_rendered [align ], rendered_item )
6278 rendered_width = rendered_width + calc_rendered_width (rendered_item )
6379 end
6480 end
81+
6582 max_width = math.max (max_width , rendered_width )
6683 grouped_by_zindex [zindex ] = zindex_rendered
6784 end
@@ -120,21 +137,24 @@ local truncate_layer_keep_right = function(layer, skip_count, max_length)
120137 local text_length = vim .fn .strchars (item .text )
121138 local remaining_to_skip = skip_count - skipped
122139 if remaining_to_skip > 0 then
123- if # item . text <= remaining_to_skip then
140+ if text_length <= remaining_to_skip then
124141 skipped = skipped + text_length
125142 item .text = " "
126143 else
127- item .text = item .text :sub (1 , text_length - remaining_to_skip )
128- if # item .text + taken > max_length then
129- item .text = item .text :sub (text_length - (max_length - taken ))
144+ item .text = vim .fn .strcharpart (item .text , 0 , text_length - remaining_to_skip )
145+ text_length = vim .fn .strchars (item .text )
146+ if text_length + taken > max_length then
147+ item .text = vim .fn .strcharpart (item .text , text_length - (max_length - taken ))
148+ text_length = vim .fn .strchars (item .text )
130149 end
131150 table.insert (result , item )
132151 taken = taken + text_length
133152 skipped = skipped + remaining_to_skip
134153 end
135154 elseif taken <= max_length then
136- if # item .text + taken > max_length then
137- item .text = item .text :sub (text_length - (max_length - taken ))
155+ if text_length + taken > max_length then
156+ item .text = vim .fn .strcharpart (item .text , text_length - (max_length - taken ))
157+ text_length = vim .fn .strchars (item .text )
138158 end
139159 table.insert (result , item )
140160 taken = taken + text_length
@@ -149,21 +169,22 @@ local fade_content = function(layer, fade_char_count)
149169 return
150170 end
151171 local hl = layer [# layer ].highlight or " Normal"
152- local fade0 = highlights .get_faded_highlight_group (hl , 0.68 )
153- local fade1 = highlights .get_faded_highlight_group (hl , 0.6 )
154- local fade2 = highlights .get_faded_highlight_group (hl , 0.35 )
155- if # text >= 3 and fade_char_count >= 3 then
156- layer [# layer ].text = text :sub (1 , # text - 3 )
157- table.insert (layer , { text = text :sub (# text - 2 , - 3 ), highlight = fade0 })
158- table.insert (layer , { text = text :sub (# text - 1 , - 2 ), highlight = fade1 })
159- table.insert (layer , { text = text :sub (# text ), highlight = fade2 })
160- elseif # text >= 2 and fade_char_count >= 2 then
161- layer [# layer ].text = text :sub (1 , # text - 2 )
162- table.insert (layer , { text = text :sub (# text - 1 , - 2 ), highlight = fade0 })
163- table.insert (layer , { text = text :sub (# text ), highlight = fade1 })
164- elseif # text >= 1 and fade_char_count >= 1 then
165- layer [# layer ].text = text :sub (1 , # text - 1 )
166- table.insert (layer , { text = text :sub (# text ), highlight = fade0 })
172+ local fade = {
173+ highlights .get_faded_highlight_group (hl , 0.68 ),
174+ highlights .get_faded_highlight_group (hl , 0.6 ),
175+ highlights .get_faded_highlight_group (hl , 0.35 ),
176+ }
177+
178+ for i = 3 , 1 , - 1 do
179+ if # text >= i and fade_char_count >= i then
180+ layer [# layer ].text = text :sub (1 , - i - 1 )
181+ for j = i , 1 , - 1 do
182+ -- force no padding for each faded character
183+ local entry = { text = text :sub (- j , - j ), highlight = fade [i - j + 1 ], no_padding = true }
184+ table.insert (layer , entry )
185+ end
186+ break
187+ end
167188 end
168189end
169190
@@ -268,6 +289,12 @@ local merge_content = function(context)
268289
269290 local result = {}
270291 vim .list_extend (result , left )
292+
293+ -- we do not pad between left and right side
294+ if # right >= 1 then
295+ right [1 ].no_padding = true
296+ end
297+
271298 vim .list_extend (result , right )
272299 context .merged_content = result
273300 log .trace (" wanted width: " , wanted_width , " actual width: " , context .container_width )
@@ -293,6 +320,11 @@ M.render = function(config, node, state, available_width)
293320 if context .has_right_content then
294321 state .has_right_content = true
295322 end
323+
324+ -- we still want padding between this container and the previous component
325+ if # context .merged_content > 0 then
326+ context .merged_content [1 ].no_padding = false
327+ end
296328 return context .merged_content , context .wanted_width
297329end
298330
0 commit comments