Guest User

Untitled

a guest
May 21st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. /*/../usr/bin/true
  3. source="$0"
  4. compiled="$0".cache
  5. if [[ "$source" -nt "$compiled" ]]; then
  6. swiftc "$source" -o "$compiled" || exit
  7. fi
  8. "$compiled" "$@"
  9. exit
  10. */
  11.  
  12. import Foundation
  13.  
  14. extension String {
  15. subscript(value: PartialRangeUpTo<Int>) -> Substring {
  16. get {
  17. return self[..<index(startIndex, offsetBy: value.upperBound)]
  18. }
  19. }
  20.  
  21. subscript(value: PartialRangeThrough<Int>) -> Substring {
  22. get {
  23. return self[...index(startIndex, offsetBy: value.upperBound)]
  24. }
  25. }
  26.  
  27. subscript(value: PartialRangeFrom<Int>) -> Substring {
  28. get {
  29. return self[index(startIndex, offsetBy: value.lowerBound)...]
  30. }
  31. }
  32. }
  33.  
  34. public struct StderrOutputStream: TextOutputStream {
  35. public mutating func write(_ string: String) { fputs(string, stderr) }
  36. }
  37. public var STDERR = StderrOutputStream()
  38.  
  39. let ENV = ProcessInfo.processInfo.environment
  40.  
  41. struct Item: Encodable {
  42. var valid: Bool
  43. var uid: String
  44. var title: String
  45. var subtitle: String?
  46. var arg: String?
  47. var text: Text?
  48.  
  49. struct Text: Encodable {
  50. var copy: String
  51. var largetype: String
  52. }
  53. }
  54.  
  55. struct Response: Encodable {
  56. let items: [Item]
  57. }
  58.  
  59. func respond(_ items: [Item]) throws {
  60. let d = try JSONEncoder().encode(Response(items: items))
  61. let s = String(decoding: d, as: UTF8.self)
  62. print(s)
  63. }
  64.  
  65. func respond(_ item: Item) throws {
  66. try respond([item])
  67. }
  68.  
  69. func system(stdin: Data? = nil, _ cmd: String...) throws -> String {
  70. let p = Process()
  71. p.executableURL = URL(fileURLWithPath: cmd[0])
  72. p.arguments = Array(cmd[1...])
  73. p.standardOutput = Pipe()
  74.  
  75. if let d = stdin {
  76. let input = Pipe()
  77. p.standardInput = input
  78.  
  79. input.fileHandleForWriting.write(d)
  80. input.fileHandleForWriting.closeFile()
  81. }
  82.  
  83. try p.run()
  84. p.waitUntilExit()
  85. let d = (p.standardOutput as! Pipe).fileHandleForReading.readDataToEndOfFile()
  86. return String(decoding: d, as: UTF8.self)
  87. }
  88.  
  89. var q = CommandLine.arguments[1]
  90. if q == "" { q = "0" }
  91. print(q, to: &STDERR)
  92.  
  93. struct QalcError: LocalizedError {
  94. let message: String
  95.  
  96. var errorDescription: String? { return message }
  97. }
  98.  
  99. do {
  100. let history = ((try? String(contentsOfFile: ENV["alfred_workflow_data"]! + "/history").components(separatedBy: "\n")) ?? []).enumerated()
  101. let varsString = history.map { "variable r\($0.offset) \($0.element)" }.joined(separator: "\n")
  102. let vars = Data(varsString.utf8)
  103. print(varsString, to: &STDERR)
  104.  
  105. let out = try system(stdin: vars, "/usr/local/bin/qalc", "+u8", "-f", "-", "-s", "update_exchange_rates 1days", q)
  106.  
  107. print(out, to: &STDERR)
  108. let lines = out.components(separatedBy: "\n")
  109.  
  110. let errorPrefix = "error: "
  111. let warningPrefix = "warning: "
  112.  
  113. let result: String
  114. let warning: String?
  115.  
  116. if out.starts(with: errorPrefix) {
  117. throw QalcError(message: String(lines[0][errorPrefix.count...]))
  118. } else if out.starts(with: warningPrefix) {
  119. warning = String(lines[0][warningPrefix.count...])
  120. result = lines[1]
  121. } else {
  122. warning = nil
  123. result = lines[0]
  124. }
  125.  
  126. let resultValue = result.components(separatedBy: " = ")[1]
  127.  
  128. let resultItem = Item(
  129. valid: true,
  130. uid: "swift-result",
  131. title: result,
  132. subtitle: warning,
  133. arg: resultValue,
  134. text: Item.Text(
  135. copy: resultValue,
  136. largetype: result
  137. )
  138. )
  139.  
  140. let historyItems = history.map {
  141. Item(
  142. valid: true,
  143. uid: UUID().uuidString,
  144. title: "\($0.element)",
  145. subtitle: "r\($0.offset) - history",
  146. arg: $0.element,
  147. text: Item.Text(
  148. copy: resultValue,
  149. largetype: resultValue
  150. )
  151. )
  152. }
  153.  
  154. try respond([resultItem] + historyItems)
  155. } catch {
  156. print("\(error)", to: &STDERR)
  157. try! respond(
  158. Item(
  159. valid: false,
  160. uid: "rip",
  161. title: "\(error.localizedDescription)",
  162. subtitle: "error",
  163. arg: nil,
  164. text: nil
  165. )
  166. )
  167. }
Add Comment
Please, Sign In to add comment