@@ -169,11 +169,11 @@ public class LineReader {
169
169
strippingNewline: strippingNewline)
170
170
}
171
171
}
172
-
172
+
173
173
private func readLineUnsupported( prompt: String ,
174
174
maxCount: Int ? ,
175
175
strippingNewline: Bool ) throws -> String {
176
- print ( prompt, terminator: " " )
176
+ Swift . print ( prompt, terminator: " " )
177
177
if let line = Swift . readLine ( strippingNewline: strippingNewline) {
178
178
return maxCount != nil ? String ( line. prefix ( maxCount!) ) : line
179
179
} else {
@@ -188,7 +188,13 @@ public class LineReader {
188
188
readProperties: TextProperties ,
189
189
parenProperties: TextProperties ) throws -> String {
190
190
var line : String = " "
191
+ if fileno ( stdout) == self . outputFile {
192
+ fflush ( stdout)
193
+ }
191
194
try self . withRawMode {
195
+ if let col = self . cursorColumn, col > 1 {
196
+ try self . output ( text: " \n " + AnsiCodes. setCursorColumn ( 0 ) )
197
+ }
192
198
try self . output ( text: promptProperties. apply ( to: prompt) )
193
199
let editState = EditState ( prompt: prompt,
194
200
maxCount: maxCount,
@@ -459,30 +465,22 @@ public class LineReader {
459
465
return nil
460
466
}
461
467
var buf = [ UInt8] ( )
462
- var i = 0
463
468
while true {
464
469
if let c = self . readByte ( ) {
465
- buf [ i] = c
470
+ if c == 82 { // "R"
471
+ break
472
+ }
473
+ buf. append ( c)
466
474
} else {
467
475
return nil
468
476
}
469
- if buf [ i] == 82 { // "R"
470
- break
471
- }
472
- i += 1
473
- }
474
- // Check the first characters are the escape code
475
- if buf [ 0 ] != 0x1B || buf [ 1 ] != 0x5B {
476
- return nil
477
- }
478
- let positionText = String ( bytes: buf [ 2 ..< buf. count] , encoding: . utf8)
479
- guard let rowCol = positionText? . split ( separator: " ; " ) else {
480
- return nil
481
477
}
482
- if rowCol. count != 2 {
478
+ guard buf [ 0 ] == 0x1B && buf [ 1 ] == 0x5B ,
479
+ let cursor = String ( bytes: buf [ 2 ..< buf. count] , encoding: . utf8) ? . split ( separator: " ; " ) ,
480
+ cursor. count == 2 else {
483
481
return nil
484
482
}
485
- return Int ( String ( rowCol [ 1 ] ) )
483
+ return Int ( String ( cursor [ 1 ] ) )
486
484
}
487
485
488
486
private var numColumns : Int {
@@ -593,7 +591,7 @@ public class LineReader {
593
591
}
594
592
595
593
private func output( text: String ) throws {
596
- if write ( outputFile, text, text. utf8. count) == - 1 {
594
+ if write ( self . outputFile, text, text. utf8. count) == - 1 {
597
595
throw LineReaderError . generalError ( " Unable to write to output " )
598
596
}
599
597
}
0 commit comments