@@ -5,12 +5,13 @@ local log = require("neo-tree.log")
5
5
6
6
local M = {}
7
7
8
+ local strwidth = vim .api .nvim_strwidth
8
9
local calc_rendered_width = function (rendered_item )
9
10
local width = 0
10
11
11
12
for _ , item in ipairs (rendered_item ) do
12
13
if item .text then
13
- width = width + vim . fn . strchars (item .text )
14
+ width = width + strwidth (item .text )
14
15
end
15
16
end
16
17
@@ -53,7 +54,7 @@ local render_content = function(config, node, state, context)
53
54
local add_padding = function (rendered_item , should_pad )
54
55
for _ , data in ipairs (rendered_item ) do
55
56
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 " "
57
58
data .text = padding .. data .text
58
59
should_pad = data .text :sub (# data .text ) ~= " "
59
60
end
@@ -97,35 +98,36 @@ local render_content = function(config, node, state, context)
97
98
return context
98
99
end
99
100
101
+ local truncate = utils .truncate_by_cell
102
+
100
103
--- Takes a list of rendered components and truncates them to fit the container width
101
104
--- @param layer table The list of rendered components.
102
105
--- @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 )
105
108
local result = {}
106
109
local taken = 0
107
110
local skipped = 0
108
111
for _ , item in ipairs (layer ) do
109
112
local remaining_to_skip = skip_count - skipped
113
+ local text_width = strwidth (item .text )
110
114
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
113
117
item .text = " "
114
118
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 )
118
122
end
119
123
table.insert (result , item )
120
- taken = taken + # item . text
124
+ taken = taken + text_width
121
125
skipped = skipped + remaining_to_skip
122
126
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 )
127
129
table.insert (result , item )
128
- taken = taken + vim . fn . strchars ( item . text )
130
+ taken = taken + text_width
129
131
end
130
132
end
131
133
return result
@@ -134,39 +136,34 @@ end
134
136
--- Takes a list of rendered components and truncates them to fit the container width
135
137
--- @param layer table The list of rendered components.
136
138
--- @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 )
139
141
local result = {}
140
142
local taken = 0
141
143
local skipped = 0
142
- local i = # layer
143
- while i > 0 do
144
+ for i = # layer , 1 , - 1 do
144
145
local item = layer [i ]
145
- i = i - 1
146
- local text_length = vim .fn .strchars (item .text )
146
+ local text_width = strwidth (item .text )
147
147
local remaining_to_skip = skip_count - skipped
148
148
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
151
151
item .text = " "
152
152
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" )
158
156
end
159
157
table.insert (result , item )
160
- taken = taken + text_length
158
+ taken = taken + text_width
161
159
skipped = skipped + remaining_to_skip
162
160
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" )
167
164
end
168
165
table.insert (result , item )
169
- taken = taken + text_length
166
+ taken = taken + text_width
170
167
end
171
168
end
172
169
return result
0 commit comments