Skip to content

Commit ee24b36

Browse files
committed
fix(block): better deal with carriage return characters (take 2)
1 parent 06db69a commit ee24b36

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lua/noice/text/block.lua

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local require = require("noice.util.lazy")
22

33
local Highlight = require("noice.text.highlight")
4+
local Util = require("noice.util")
45
local NuiLine = require("nui.line")
56
local Object = require("nui.object")
67

@@ -109,6 +110,15 @@ function Block:_append(content, highlight)
109110
if #self._lines == 0 then
110111
table.insert(self._lines, NuiLine())
111112
end
113+
if type(content) == "string" and true then
114+
-- handle carriage returns. They overwrite the line from the first character
115+
local cr = content:match("^.*()[\r]")
116+
if cr then
117+
table.remove(self._lines)
118+
table.insert(self._lines, NuiLine())
119+
content = content:sub(cr + 1)
120+
end
121+
end
112122
return self._lines[#self._lines]:append(content, highlight)
113123
end
114124

@@ -156,6 +166,8 @@ function Block:append(contents, highlight)
156166
-- Handle newlines
157167
---@type number|string|table, string
158168
local attr_id, text = unpack(content)
169+
-- msg_show messages can contain invalid \r characters
170+
text = text:gsub("%^M", "\r")
159171
text = text:gsub("\r\n", "\n")
160172

161173
---@type string|table|nil
@@ -168,23 +180,12 @@ function Block:append(contents, highlight)
168180

169181
while text ~= "" do
170182
local nl = text:find("\n")
183+
local line = nl and text:sub(1, nl - 1) or text
184+
self:_append(line, hl_group)
171185
if nl then
172-
local str = text:sub(1, nl - 1)
173-
174-
-- handle carriage returns. They overwrite the line from the first character
175-
if str:find("\r") then
176-
local parts = vim.split(str, "\r", { plain = true })
177-
str = ""
178-
for _, p in ipairs(parts) do
179-
str = p .. str:sub(p:len() + 1)
180-
end
181-
end
182-
183-
self:_append(str, hl_group)
184186
self:newline()
185187
text = text:sub(nl + 1)
186188
else
187-
self:_append(text, hl_group)
188189
break
189190
end
190191
end

0 commit comments

Comments
 (0)