@@ -5,12 +5,13 @@ local log = require("neo-tree.log")
55
66local M = {}
77
8+ local strwidth = vim .api .nvim_strwidth
89local calc_rendered_width = function (rendered_item )
910 local width = 0
1011
1112 for _ , item in ipairs (rendered_item ) do
1213 if item .text then
13- width = width + vim . fn . strchars (item .text )
14+ width = width + strwidth (item .text )
1415 end
1516 end
1617
@@ -53,7 +54,7 @@ local render_content = function(config, node, state, context)
5354 local add_padding = function (rendered_item , should_pad )
5455 for _ , data in ipairs (rendered_item ) do
5556 if data .text then
56- local padding = (should_pad and # data .text and data .text :sub (1 , 1 ) ~= " " ) and " " or " "
57+ local padding = (should_pad and # data .text > 0 and data .text :sub (1 , 1 ) ~= " " ) and " " or " "
5758 data .text = padding .. data .text
5859 should_pad = data .text :sub (# data .text ) ~= " "
5960 end
@@ -97,35 +98,36 @@ local render_content = function(config, node, state, context)
9798 return context
9899end
99100
101+ local truncate = utils .truncate_by_cell
102+
100103--- Takes a list of rendered components and truncates them to fit the container width
101104--- @param layer table The list of rendered components.
102105--- @param skip_count number The number of characters to skip from the begining /left.
103- --- @param max_length number The maximum number of characters to return.
104- local truncate_layer_keep_left = function (layer , skip_count , max_length )
106+ --- @param max_width number The maximum number of characters to return.
107+ local truncate_layer_keep_left = function (layer , skip_count , max_width )
105108 local result = {}
106109 local taken = 0
107110 local skipped = 0
108111 for _ , item in ipairs (layer ) do
109112 local remaining_to_skip = skip_count - skipped
113+ local text_width = strwidth (item .text )
110114 if remaining_to_skip > 0 then
111- if # item . text <= remaining_to_skip then
112- skipped = skipped + vim . fn . strchars ( item . text )
115+ if text_width <= remaining_to_skip then
116+ skipped = skipped + text_width
113117 item .text = " "
114118 else
115- item .text = item .text : sub ( remaining_to_skip )
116- if # item . text + taken > max_length then
117- item .text = item .text : sub ( 1 , max_length - taken )
119+ item .text , text_width = truncate ( item .text , text_width - remaining_to_skip , " right " )
120+ if text_width > max_width - taken then
121+ item .text , text_width = truncate ( item .text , max_width - taken )
118122 end
119123 table.insert (result , item )
120- taken = taken + # item . text
124+ taken = taken + text_width
121125 skipped = skipped + remaining_to_skip
122126 end
123- elseif taken <= max_length then
124- if # item .text + taken > max_length then
125- item .text = item .text :sub (1 , max_length - taken )
126- end
127+ elseif taken <= max_width then
128+ item .text , text_width = truncate (item .text , max_width - taken )
127129 table.insert (result , item )
128- taken = taken + vim . fn . strchars ( item . text )
130+ taken = taken + text_width
129131 end
130132 end
131133 return result
@@ -134,39 +136,34 @@ end
134136--- Takes a list of rendered components and truncates them to fit the container width
135137--- @param layer table The list of rendered components.
136138--- @param skip_count number The number of characters to skip from the end /right.
137- --- @param max_length number The maximum number of characters to return.
138- local truncate_layer_keep_right = function (layer , skip_count , max_length )
139+ --- @param max_width number The maximum number of characters to return.
140+ local truncate_layer_keep_right = function (layer , skip_count , max_width )
139141 local result = {}
140142 local taken = 0
141143 local skipped = 0
142- local i = # layer
143- while i > 0 do
144+ for i = # layer , 1 , - 1 do
144145 local item = layer [i ]
145- i = i - 1
146- local text_length = vim .fn .strchars (item .text )
146+ local text_width = strwidth (item .text )
147147 local remaining_to_skip = skip_count - skipped
148148 if remaining_to_skip > 0 then
149- if text_length <= remaining_to_skip then
150- skipped = skipped + text_length
149+ if text_width <= remaining_to_skip then
150+ skipped = skipped + text_width
151151 item .text = " "
152152 else
153- item .text = vim .fn .strcharpart (item .text , 0 , text_length - remaining_to_skip )
154- text_length = vim .fn .strchars (item .text )
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 )
153+ item .text , text_width = truncate (item .text , text_width - remaining_to_skip )
154+ if text_width > max_width - taken then
155+ item .text , text_width = truncate (item .text , max_width - taken , " right" )
158156 end
159157 table.insert (result , item )
160- taken = taken + text_length
158+ taken = taken + text_width
161159 skipped = skipped + remaining_to_skip
162160 end
163- elseif taken <= max_length then
164- if text_length + taken > max_length then
165- item .text = vim .fn .strcharpart (item .text , text_length - (max_length - taken ))
166- text_length = vim .fn .strchars (item .text )
161+ elseif taken <= max_width then
162+ if text_width > max_width - taken then
163+ item .text , text_width = truncate (item .text , max_width - taken , " right" )
167164 end
168165 table.insert (result , item )
169- taken = taken + text_length
166+ taken = taken + text_width
170167 end
171168 end
172169 return result
0 commit comments