ViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // ViewController.swift
  3. // MacTerminal
  4. //
  5. // Created by Miguel de Icaza on 3/11/20.
  6. // Copyright © 2020 Miguel de Icaza. All rights reserved.
  7. //
  8. import Cocoa
  9. import SwiftTerm
  10. class ViewController: NSViewController, LocalProcessTerminalViewDelegate, NSUserInterfaceValidations {
  11. @IBOutlet var loggingMenuItem: NSMenuItem?
  12. var changingSize = false
  13. var logging: Bool = false
  14. var zoomGesture: NSMagnificationGestureRecognizer?
  15. var postedTitle: String = ""
  16. var postedDirectory: String? = nil
  17. func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {
  18. if changingSize {
  19. return
  20. }
  21. changingSize = true
  22. //var border = view.window!.frame - view.frame
  23. var newFrame = terminal.getOptimalFrameSize ()
  24. let windowFrame = view.window!.frame
  25. newFrame = CGRect (x: windowFrame.minX, y: windowFrame.minY, width: newFrame.width, height: windowFrame.height - view.frame.height + newFrame.height)
  26. view.window?.setFrame(newFrame, display: true, animate: true)
  27. changingSize = false
  28. }
  29. func updateWindowTitle ()
  30. {
  31. var newTitle: String
  32. if let dir = postedDirectory {
  33. if let uri = URL(string: dir) {
  34. if postedTitle == "" {
  35. newTitle = uri.path
  36. } else {
  37. newTitle = "\(postedTitle) - \(uri.path)"
  38. }
  39. } else {
  40. newTitle = postedTitle
  41. }
  42. } else {
  43. newTitle = postedTitle
  44. }
  45. view.window?.title = newTitle
  46. }
  47. func setTerminalTitle(source: LocalProcessTerminalView, title: String) {
  48. postedTitle = title
  49. updateWindowTitle ()
  50. }
  51. func hostCurrentDirectoryUpdate (source: TerminalView, directory: String?) {
  52. self.postedDirectory = directory
  53. updateWindowTitle()
  54. }
  55. func processTerminated(source: TerminalView, exitCode: Int32?) {
  56. view.window?.close()
  57. if let e = exitCode {
  58. print ("Process terminated with code: \(e)")
  59. } else {
  60. print ("Process vanished")
  61. }
  62. }
  63. var terminal: LocalProcessTerminalView!
  64. static var lastTerminal: LocalProcessTerminalView!
  65. func getBufferAsData () -> Data
  66. {
  67. return terminal.getTerminal().getBufferAsData ()
  68. }
  69. func updateLogging ()
  70. {
  71. let path = logging ? "/Users/miguel/Downloads/Logs" : nil
  72. terminal.setHostLogging (directory: path)
  73. NSUserDefaultsController.shared.defaults.set (logging, forKey: "LogHostOutput")
  74. }
  75. override func viewDidLoad() {
  76. super.viewDidLoad()
  77. terminal = LocalProcessTerminalView(frame: view.frame)
  78. zoomGesture = NSMagnificationGestureRecognizer(target: self, action: #selector(zoomGestureHandler))
  79. terminal.addGestureRecognizer(zoomGesture!)
  80. ViewController.lastTerminal = terminal
  81. terminal.processDelegate = self
  82. terminal.feed(text: "Welcome to SwiftTerm")
  83. terminal.startProcess ()
  84. view.addSubview(terminal)
  85. logging = NSUserDefaultsController.shared.defaults.bool(forKey: "LogHostOutput")
  86. updateLogging ()
  87. }
  88. @objc
  89. func zoomGestureHandler (_ sender: NSMagnificationGestureRecognizer) {
  90. if sender.magnification > 0 {
  91. biggerFont (sender)
  92. } else {
  93. smallerFont(sender)
  94. }
  95. }
  96. override func viewDidLayout() {
  97. super.viewDidLayout()
  98. changingSize = true
  99. terminal.frame = view.frame
  100. changingSize = false
  101. terminal.needsLayout = true
  102. }
  103. @objc @IBAction
  104. func set80x25 (_ source: AnyObject)
  105. {
  106. terminal.resize(cols: 80, rows: 25)
  107. }
  108. var lowerCol = 80
  109. var lowerRow = 25
  110. var higherCol = 160
  111. var higherRow = 60
  112. func queueNextSize ()
  113. {
  114. // If they requested a stop
  115. if resizificating == 0 {
  116. return
  117. }
  118. var next = terminal.getTerminal().getDims ()
  119. if resizificating > 0 {
  120. if next.cols < higherCol {
  121. next.cols += 1
  122. }
  123. if next.rows < higherRow {
  124. next.rows += 1
  125. }
  126. } else {
  127. if next.cols > lowerCol {
  128. next.cols -= 1
  129. }
  130. if next.rows > lowerRow {
  131. next.rows -= 1
  132. }
  133. }
  134. terminal.resize (cols: next.cols, rows: next.rows)
  135. var direction = resizificating
  136. if next.rows == higherRow && next.cols == higherCol {
  137. direction = -1
  138. }
  139. if next.rows == lowerRow && next.cols == lowerCol {
  140. direction = 1
  141. }
  142. DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) {
  143. self.resizificating = direction
  144. self.queueNextSize()
  145. }
  146. }
  147. var resizificating = 0
  148. @objc @IBAction
  149. func resizificator (_ source: AnyObject)
  150. {
  151. if resizificating != 1 {
  152. resizificating = 1
  153. queueNextSize ()
  154. } else {
  155. resizificating = 0
  156. }
  157. }
  158. @objc @IBAction
  159. func resizificatorDown (_ source: AnyObject)
  160. {
  161. if resizificating != -1 {
  162. resizificating = -1
  163. queueNextSize ()
  164. } else {
  165. resizificating = 0
  166. }
  167. }
  168. @objc @IBAction
  169. func allowMouseReporting (_ source: AnyObject)
  170. {
  171. terminal.allowMouseReporting.toggle ()
  172. }
  173. @objc @IBAction
  174. func exportBuffer (_ source: AnyObject)
  175. {
  176. saveData { self.terminal.getTerminal().getBufferAsData () }
  177. }
  178. @objc @IBAction
  179. func exportSelection (_ source: AnyObject)
  180. {
  181. saveData {
  182. if let str = self.terminal.getSelection () {
  183. return str.data (using: .utf8) ?? Data ()
  184. }
  185. return Data ()
  186. }
  187. }
  188. func saveData (_ getData: @escaping () -> Data)
  189. {
  190. let savePanel = NSSavePanel ()
  191. savePanel.canCreateDirectories = true
  192. savePanel.allowedFileTypes = ["txt"]
  193. savePanel.title = "Export Buffer Contents As Text"
  194. savePanel.nameFieldStringValue = "TerminalCapture"
  195. savePanel.begin { (result) in
  196. if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
  197. let data = getData ()
  198. if let url = savePanel.url {
  199. do {
  200. try data.write(to: url)
  201. } catch let error as NSError {
  202. let alert = NSAlert (error: error)
  203. alert.runModal()
  204. }
  205. }
  206. }
  207. }
  208. }
  209. @objc @IBAction
  210. func softReset (_ source: AnyObject)
  211. {
  212. terminal.getTerminal().softReset ()
  213. terminal.setNeedsDisplay(terminal.frame)
  214. }
  215. @objc @IBAction
  216. func hardReset (_ source: AnyObject)
  217. {
  218. terminal.getTerminal().resetToInitialState ()
  219. terminal.setNeedsDisplay(terminal.frame)
  220. }
  221. @objc @IBAction
  222. func toggleOptionAsMetaKey (_ source: AnyObject)
  223. {
  224. terminal.optionAsMetaKey.toggle ()
  225. }
  226. @objc @IBAction
  227. func biggerFont (_ source: AnyObject)
  228. {
  229. let size = terminal.font.pointSize
  230. guard size < 72 else {
  231. return
  232. }
  233. terminal.font = NSFont.monospacedSystemFont(ofSize: size+1, weight: .regular)
  234. }
  235. @objc @IBAction
  236. func smallerFont (_ source: AnyObject)
  237. {
  238. let size = terminal.font.pointSize
  239. guard size > 5 else {
  240. return
  241. }
  242. terminal.font = NSFont.monospacedSystemFont(ofSize: size-1, weight: .regular)
  243. }
  244. @objc @IBAction
  245. func defaultFontSize (_ source: AnyObject)
  246. {
  247. terminal.font = NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
  248. }
  249. @objc @IBAction
  250. func addTab (_ source: AnyObject)
  251. {
  252. // if let win = view.window {
  253. // win.tabbingMode = .preferred
  254. // if let wc = win.windowController {
  255. // if let d = wc.document as? Document {
  256. // do {
  257. // let x = Document()
  258. // x.makeWindowControllers()
  259. //
  260. // try NSDocumentController.shared.newDocument(self)
  261. // } catch {}
  262. // print ("\(d.debugDescription)")
  263. // }
  264. // }
  265. // }
  266. // win.tabbingMode = .preferred
  267. // win.addTabbedWindow(win, ordered: .above)
  268. //
  269. // if let wc = win.windowController {
  270. // wc.newWindowForTab(self()
  271. // wc.showWindow(source)
  272. // }
  273. // }
  274. }
  275. func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool
  276. {
  277. if item.action == #selector(debugToggleHostLogging(_:)) {
  278. if let m = item as? NSMenuItem {
  279. m.state = logging ? NSControl.StateValue.on : NSControl.StateValue.off
  280. }
  281. }
  282. if item.action == #selector(resizificator(_:)) {
  283. if let m = item as? NSMenuItem {
  284. m.state = resizificating == 1 ? NSControl.StateValue.on : NSControl.StateValue.off
  285. }
  286. }
  287. if item.action == #selector(resizificatorDown(_:)) {
  288. if let m = item as? NSMenuItem {
  289. m.state = resizificating == -1 ? NSControl.StateValue.on : NSControl.StateValue.off
  290. }
  291. }
  292. if item.action == #selector(allowMouseReporting(_:)) {
  293. if let m = item as? NSMenuItem {
  294. m.state = terminal.allowMouseReporting ? NSControl.StateValue.on : NSControl.StateValue.off
  295. }
  296. }
  297. if item.action == #selector(toggleOptionAsMetaKey(_:)) {
  298. if let m = item as? NSMenuItem {
  299. m.state = terminal.optionAsMetaKey ? NSControl.StateValue.on : NSControl.StateValue.off
  300. }
  301. }
  302. // Only enable "Export selection" if we have a selection
  303. if item.action == #selector(exportSelection(_:)) {
  304. return terminal.selectionActive
  305. }
  306. return true
  307. }
  308. @objc @IBAction
  309. func debugToggleHostLogging (_ source: AnyObject)
  310. {
  311. logging = !logging
  312. updateLogging()
  313. }
  314. }