Skip to content

Commit 3658edb

Browse files
committed
Fixed line reader to handle buffered output.
1 parent a0d60e8 commit 3658edb

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.2 (2020-02-01)
4+
- Fixed line reader to handle buffered output
5+
- Migrated project to Xcode 11.3
6+
37
## 0.3.1 (2019-09-23)
48
- Migrated project to Xcode 11.0
59
- Ported code to Swift 5.1

CommandLineKit.xcodeproj/project.pbxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
isa = PBXProject;
277277
attributes = {
278278
LastSwiftUpdateCheck = 0930;
279-
LastUpgradeCheck = 1100;
279+
LastUpgradeCheck = 1130;
280280
ORGANIZATIONNAME = "Matthias Zenger";
281281
TargetAttributes = {
282282
CC7A60E6207A62A5007376A0 = {
@@ -494,6 +494,7 @@
494494
isa = XCBuildConfiguration;
495495
buildSettings = {
496496
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
497+
CODE_SIGN_IDENTITY = "-";
497498
CODE_SIGN_STYLE = Automatic;
498499
DEVELOPMENT_TEAM = C72Z63N8M5;
499500
INFOPLIST_FILE = "$(SRCROOT)/Sources/CommandLineKitDemo/Info.plist";
@@ -512,6 +513,7 @@
512513
isa = XCBuildConfiguration;
513514
buildSettings = {
514515
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
516+
CODE_SIGN_IDENTITY = "-";
515517
CODE_SIGN_STYLE = Automatic;
516518
DEVELOPMENT_TEAM = C72Z63N8M5;
517519
INFOPLIST_FILE = "$(SRCROOT)/Sources/CommandLineKitDemo/Info.plist";

CommandLineKit.xcodeproj/xcshareddata/xcschemes/CommandLineKit.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1100"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

CommandLineKit.xcodeproj/xcshareddata/xcschemes/CommandLineKitDemo.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1100"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Platform: macOS](https://img.shields.io/badge/Platform-macOS-blue.svg?style=flat)](https://developer.apple.com/osx/)
44
[![Platform: Linux](https://img.shields.io/badge/Platform-Linux-blue.svg?style=flat)](https://www.ubuntu.com/)
55
[![Language: Swift 5.1](https://img.shields.io/badge/Language-Swift%205.1-green.svg?style=flat)](https://developer.apple.com/swift/)
6-
[![IDE: Xcode 11.0](https://img.shields.io/badge/IDE-Xcode%2011.0-orange.svg?style=flat)](https://developer.apple.com/xcode/)
6+
[![IDE: Xcode 11.3](https://img.shields.io/badge/IDE-Xcode%2011.3-orange.svg?style=flat)](https://developer.apple.com/xcode/)
77
[![Carthage: compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
88
[![License: BSD](https://img.shields.io/badge/License-BSD-lightgrey.svg?style=flat)](https://developers.google.com/open-source/licenses/bsd)
99

@@ -231,13 +231,13 @@ if let ln = LineReader() {
231231

232232
## Requirements
233233

234-
- [Xcode 11.0](https://developer.apple.com/xcode/)
234+
- [Xcode 11.3](https://developer.apple.com/xcode/)
235235
- [Swift 5.1](https://developer.apple.com/swift/)
236236
- [Carthage](https://github.com/Carthage/Carthage)
237237
- [Swift Package Manager](https://swift.org/package-manager/)
238238

239239
## Copyright
240240

241241
Author: Matthias Zenger (<matthias@objecthub.net>)
242-
Copyright © 2018-2019 Google LLC.
242+
Copyright © 2018-2020 Google LLC.
243243
_Please note: This is not an official Google product._

Sources/CommandLineKit/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.3.1</string>
18+
<string>0.3.2</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSHumanReadableCopyright</key>
22-
<string>Copyright © 2018-2019 Google LLC</string>
22+
<string>Copyright © 2018-2020 Google LLC</string>
2323
<key>NSPrincipalClass</key>
2424
<string></string>
2525
</dict>

Sources/CommandLineKit/LineReader.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/CommandLineKitTests/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>BNDL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.3.1</string>
18+
<string>0.3.2</string>
1919
<key>CFBundleVersion</key>
2020
<string>1</string>
2121
</dict>

0 commit comments

Comments
 (0)