@@ -1174,23 +1174,29 @@ open class Terminal {
11741174 for _ in 1 ..< n {
11751175 x. append ( readingBuffer. getNext ( ) )
11761176 }
1177- x. append ( 0 )
1178- x. withUnsafeBytes { ptr in
1179- let unsafeBound = ptr. bindMemory ( to: UInt8 . self)
1180- let unsafePointer = unsafeBound. baseAddress!
1181-
1182- let s = String ( cString: unsafePointer)
1183- ch = s. first ?? Character ( " " )
1184-
1185- // Now the challenge is that we have a character, not a rune, and we want to compute
1186- // the width of it.
1187- if ch. unicodeScalars. count == 1 {
1188- chWidth = UnicodeUtil . columnWidth ( rune: ch. unicodeScalars. first!)
1189- } else {
1190- chWidth = 0
1191- for scalar in ch. unicodeScalars {
1192- chWidth = max ( chWidth, UnicodeUtil . columnWidth ( rune: scalar) )
1193- }
1177+
1178+ var iterator = x. makeIterator ( )
1179+ var decoder = UTF8 ( )
1180+ switch decoder. decode ( & iterator) {
1181+ case . scalarValue( let scalar) :
1182+ ch = Character ( scalar)
1183+ default :
1184+ // Invalid UTF-8 sequence, fall back to interpreting the first byte
1185+ let rune = UnicodeScalar ( code)
1186+ chWidth = UnicodeUtil . columnWidth ( rune: rune)
1187+ let charData = CharData ( attribute: curAttr, scalar: rune, size: Int8 ( chWidth) )
1188+ buffer. insertCharacter ( charData)
1189+ continue
1190+ }
1191+
1192+ // Now the challenge is that we have a character, not a rune, and we want to compute
1193+ // the width of it.
1194+ if ch. unicodeScalars. count == 1 {
1195+ chWidth = UnicodeUtil . columnWidth ( rune: ch. unicodeScalars. first!)
1196+ } else {
1197+ chWidth = 0
1198+ for scalar in ch. unicodeScalars {
1199+ chWidth = max ( chWidth, UnicodeUtil . columnWidth ( rune: scalar) )
11941200 }
11951201 }
11961202 } else {
@@ -1239,7 +1245,15 @@ open class Terminal {
12391245 // If the resulting string is 1 grapheme cluster, then it combined properly
12401246 if newStr. count == 1 {
12411247 if let newCh = newStr. first {
1242- cd. setValue ( char: newCh, size: Int32 ( cd. width) )
1248+ switch firstScalar. value {
1249+ // This is the "This should use color modifier" on the previous item
1250+ // and we are going to take this to mean two columns
1251+ // See https://github.com/migueldeicaza/SwiftTerm/pull/412
1252+ case 0xFE0F :
1253+ cd. setValue ( char: newCh, size: 2 )
1254+ default :
1255+ cd. setValue ( char: newCh, size: Int32 ( cd. width) )
1256+ }
12431257 existingLine [ lastx] = cd
12441258 updateRange ( last. y)
12451259 continue
0 commit comments