1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import CoreCommands
import PackageGraph
import PackageModel
import var TSCBasic.stdoutStream
import class TSCBasic.TerminalController
fileprivate extension TerminalController {
func clearScreen() {
write("\u{001b}[2J")
write("\u{001b}[H")
flush()
}
}
/// A stack of "cards" to display one at a time at the command line.
struct CardStack {
var terminal: TerminalController
/// The representation of a stack of cards.
var cards = [Card]()
/// The tool used for eventually building and running a chosen snippet.
var swiftCommandState: SwiftCommandState
/// When true, the escape sequence for clearing the terminal should be
/// printed first.
private var needsToClearScreen = true
init(package: ResolvedPackage, snippetGroups: [SnippetGroup], swiftCommandState: SwiftCommandState) {
// this interaction is done on stdout
self.terminal = TerminalController(stream: TSCBasic.stdoutStream)!
self.cards = [TopCard(package: package, snippetGroups: snippetGroups, swiftCommandState: swiftCommandState)]
self.swiftCommandState = swiftCommandState
}
mutating func push(_ card: Card) {
cards.append(card)
}
mutating func pop() {
cards.removeLast()
}
mutating func clear() {
cards.removeAll()
}
func askForLineInput(prompt: String?) -> String? {
if let prompt {
print(brightBlack { prompt }.terminalString())
}
terminal.write(">>> ", inColor: .green, bold: true)
return readLine(strippingNewline: true)
}
mutating func run() {
var inputFinished = false
while !inputFinished {
guard let top = cards.last else {
break
}
if needsToClearScreen {
terminal.clearScreen()
needsToClearScreen = false
}
print(top.render())
// Assume input finished until proven otherwise, i.e. when readLine returns
// `nil`.
inputFinished = true
askForLine: while let line = askForLineInput(prompt: top.inputPrompt) {
inputFinished = false
let trimmedLine = String(line.drop { $0.isWhitespace }
.reversed()
.drop { $0.isWhitespace }
.reversed())
let response = top.acceptLineInput(trimmedLine)
switch response {
case .none:
continue askForLine
case .push(let card):
push(card)
needsToClearScreen = true
break askForLine
case let .pop(error):
cards.removeLast()
if let error {
self.swiftCommandState.observabilityScope.emit(error)
needsToClearScreen = false
} else {
needsToClearScreen = !cards.isEmpty
}
break askForLine
case .quit:
return
}
}
}
}
}
|