@@ -169,11 +169,11 @@ public class LineReader {
169169 strippingNewline: strippingNewline)
170170 }
171171 }
172-
172+
173173 private func readLineUnsupported( prompt: String ,
174174 maxCount: Int ? ,
175175 strippingNewline: Bool ) throws -> String {
176- print ( prompt, terminator: " " )
176+ Swift . print ( prompt, terminator: " " )
177177 if let line = Swift . readLine ( strippingNewline: strippingNewline) {
178178 return maxCount != nil ? String ( line. prefix ( maxCount!) ) : line
179179 } else {
@@ -188,7 +188,13 @@ public class LineReader {
188188 readProperties: TextProperties ,
189189 parenProperties: TextProperties ) throws -> String {
190190 var line : String = " "
191+ if fileno ( stdout) == self . outputFile {
192+ fflush ( stdout)
193+ }
191194 try self . withRawMode {
195+ if let col = self . cursorColumn, col > 1 {
196+ try self . output ( text: " \n " + AnsiCodes. setCursorColumn ( 0 ) )
197+ }
192198 try self . output ( text: promptProperties. apply ( to: prompt) )
193199 let editState = EditState ( prompt: prompt,
194200 maxCount: maxCount,
@@ -459,30 +465,22 @@ public class LineReader {
459465 return nil
460466 }
461467 var buf = [ UInt8] ( )
462- var i = 0
463468 while true {
464469 if let c = self . readByte ( ) {
465- buf [ i] = c
470+ if c == 82 { // "R"
471+ break
472+ }
473+ buf. append ( c)
466474 } else {
467475 return nil
468476 }
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
481477 }
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 {
483481 return nil
484482 }
485- return Int ( String ( rowCol [ 1 ] ) )
483+ return Int ( String ( cursor [ 1 ] ) )
486484 }
487485
488486 private var numColumns : Int {
@@ -593,7 +591,7 @@ public class LineReader {
593591 }
594592
595593 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 {
597595 throw LineReaderError . generalError ( " Unable to write to output " )
598596 }
599597 }
0 commit comments