Skip to content

Commit a122ec7

Browse files
committed
Add a mode to skip over null characters that were inserted due to 2-column characters for the copy operation
1 parent 74fdc91 commit a122ec7

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

Sources/SwiftTerm/Buffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,10 @@ public final class Buffer {
496496
}
497497
}
498498

499-
func translateBufferLineToString (lineIndex: Int, trimRight: Bool, startCol: Int = 0, endCol: Int = -1) -> String
499+
func translateBufferLineToString (lineIndex: Int, trimRight: Bool, startCol: Int = 0, endCol: Int = -1, skipNullCellsFollowingWide: Bool = false) -> String
500500
{
501501
let line = _lines [lineIndex]
502-
return line.translateToString(trimRight: trimRight, startCol: startCol, endCol: endCol)
502+
return line.translateToString(trimRight: trimRight, startCol: startCol, endCol: endCol, skipNullCellsFollowingWide: skipNullCellsFollowingWide)
503503
}
504504

505505
func setupTabStops (index: Int = -1, tabStopWidth: Int)

Sources/SwiftTerm/BufferLine.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,37 @@ public final class BufferLine: CustomDebugStringConvertible {
247247
/// - Parameter startCol: the starting column to copy the data from, defaults toe zero if not provided
248248
/// - Parameter endCol: the end column (not included) to consume. If the value -1, this copies all the way to the end
249249
/// - Returns: a string containing the contents of the BufferLine from [startCol..<endCol]
250-
public func translateToString (trimRight: Bool = false, startCol: Int = 0, endCol: Int = -1) -> String
250+
public func translateToString (trimRight: Bool = false, startCol: Int = 0, endCol: Int = -1, skipNullCellsFollowingWide: Bool = false) -> String
251251
{
252252
var ec = endCol == -1 ? dataSize : endCol
253253
if trimRight {
254254
ec = max (startCol, min (ec, getTrimmedLength()))
255255
}
256+
let limit = max(ec, startCol)
257+
if !skipNullCellsFollowingWide {
258+
var result = ""
259+
for i in startCol..<limit {
260+
result.append (data [i].getCharacter ())
261+
}
262+
return result
263+
}
256264
var result = ""
257-
for i in startCol..<max(ec,startCol) {
258-
result.append (data [i].getCharacter ())
265+
var idx = startCol
266+
while idx < limit {
267+
if idx > 0 && data [idx].code == 0 && data [idx-1].width == 2 {
268+
idx += 1
269+
continue
270+
}
271+
let cell = data [idx]
272+
result.append (cell.getCharacter ())
273+
if cell.width == 2 {
274+
let nextIndex = idx + 1
275+
if nextIndex < limit && data [nextIndex].code == 0 {
276+
idx += 2
277+
continue
278+
}
279+
}
280+
idx += 1
259281
}
260282
return result
261283
}
@@ -275,4 +297,3 @@ public final class BufferLine: CustomDebugStringConvertible {
275297
}
276298
}
277299
}
278-

Sources/SwiftTerm/Terminal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5367,7 +5367,7 @@ open class Terminal {
53675367

53685368
func translateBufferLineToString (buffer: Buffer, line: Int, start: Int, end: Int) -> String
53695369
{
5370-
buffer.translateBufferLineToString(lineIndex: line, trimRight: true, startCol: start, endCol: end).replacingOccurrences(of: "\u{0}", with: " ")
5370+
buffer.translateBufferLineToString(lineIndex: line, trimRight: true, startCol: start, endCol: end, skipNullCellsFollowingWide: true).replacingOccurrences(of: "\u{0}", with: " ")
53715371
}
53725372
}
53735373

0 commit comments

Comments
 (0)