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. FileManager.default.changeCurrentDirectoryPath (FileManager.default.homeDirectoryForCurrentUser.path)
  84. terminal.startProcess (executable: "/bin/bash", execName: "-bash")
  85. view.addSubview(terminal)
  86. logging = NSUserDefaultsController.shared.defaults.bool(forKey: "LogHostOutput")
  87. updateLogging ()
  88. }
  89. @objc
  90. func zoomGestureHandler (_ sender: NSMagnificationGestureRecognizer) {
  91. if sender.magnification > 0 {
  92. biggerFont (sender)
  93. } else {
  94. smallerFont(sender)
  95. }
  96. }
  97. override func viewDidLayout() {
  98. super.viewDidLayout()
  99. changingSize = true
  100. terminal.frame = view.frame
  101. changingSize = false
  102. terminal.needsLayout = true
  103. }
  104. @objc @IBAction
  105. func set80x25 (_ source: AnyObject)
  106. {
  107. terminal.resize(cols: 80, rows: 25)
  108. }
  109. var lowerCol = 80
  110. var lowerRow = 25
  111. var higherCol = 160
  112. var higherRow = 60
  113. func queueNextSize ()
  114. {
  115. // If they requested a stop
  116. if resizificating == 0 {
  117. return
  118. }
  119. var next = terminal.getTerminal().getDims ()
  120. if resizificating > 0 {
  121. if next.cols < higherCol {
  122. next.cols += 1
  123. }
  124. if next.rows < higherRow {
  125. next.rows += 1
  126. }
  127. } else {
  128. if next.cols > lowerCol {
  129. next.cols -= 1
  130. }
  131. if next.rows > lowerRow {
  132. next.rows -= 1
  133. }
  134. }
  135. terminal.resize (cols: next.cols, rows: next.rows)
  136. var direction = resizificating
  137. if next.rows == higherRow && next.cols == higherCol {
  138. direction = -1
  139. }
  140. if next.rows == lowerRow && next.cols == lowerCol {
  141. direction = 1
  142. }
  143. DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) {
  144. self.resizificating = direction
  145. self.queueNextSize()
  146. }
  147. }
  148. var resizificating = 0
  149. @objc @IBAction
  150. func resizificator (_ source: AnyObject)
  151. {
  152. if resizificating != 1 {
  153. resizificating = 1
  154. queueNextSize ()
  155. } else {
  156. resizificating = 0
  157. }
  158. }
  159. @objc @IBAction
  160. func resizificatorDown (_ source: AnyObject)
  161. {
  162. if resizificating != -1 {
  163. resizificating = -1
  164. queueNextSize ()
  165. } else {
  166. resizificating = 0
  167. }
  168. }
  169. @objc @IBAction
  170. func allowMouseReporting (_ source: AnyObject)
  171. {
  172. terminal.allowMouseReporting.toggle ()
  173. }
  174. @objc @IBAction
  175. func exportBuffer (_ source: AnyObject)
  176. {
  177. saveData { self.terminal.getTerminal().getBufferAsData () }
  178. }
  179. @objc @IBAction
  180. func exportSelection (_ source: AnyObject)
  181. {
  182. saveData {
  183. if let str = self.terminal.getSelection () {
  184. return str.data (using: .utf8) ?? Data ()
  185. }
  186. return Data ()
  187. }
  188. }
  189. func saveData (_ getData: @escaping () -> Data)
  190. {
  191. let savePanel = NSSavePanel ()
  192. savePanel.canCreateDirectories = true
  193. savePanel.allowedFileTypes = ["txt"]
  194. savePanel.title = "Export Buffer Contents As Text"
  195. savePanel.nameFieldStringValue = "TerminalCapture"
  196. savePanel.begin { (result) in
  197. if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
  198. let data = getData ()
  199. if let url = savePanel.url {
  200. do {
  201. try data.write(to: url)
  202. } catch let error as NSError {
  203. let alert = NSAlert (error: error)
  204. alert.runModal()
  205. }
  206. }
  207. }
  208. }
  209. }
  210. @objc @IBAction
  211. func softReset (_ source: AnyObject)
  212. {
  213. terminal.getTerminal().softReset ()
  214. terminal.setNeedsDisplay(terminal.frame)
  215. }
  216. @objc @IBAction
  217. func hardReset (_ source: AnyObject)
  218. {
  219. terminal.getTerminal().resetToInitialState ()
  220. terminal.setNeedsDisplay(terminal.frame)
  221. }
  222. @objc @IBAction
  223. func toggleOptionAsMetaKey (_ source: AnyObject)
  224. {
  225. terminal.optionAsMetaKey.toggle ()
  226. }
  227. @objc @IBAction
  228. func biggerFont (_ source: AnyObject)
  229. {
  230. let size = terminal.font.pointSize
  231. guard size < 72 else {
  232. return
  233. }
  234. terminal.font = NSFont.monospacedSystemFont(ofSize: size+1, weight: .regular)
  235. }
  236. @objc @IBAction
  237. func smallerFont (_ source: AnyObject)
  238. {
  239. let size = terminal.font.pointSize
  240. guard size > 5 else {
  241. return
  242. }
  243. terminal.font = NSFont.monospacedSystemFont(ofSize: size-1, weight: .regular)
  244. }
  245. @objc @IBAction
  246. func defaultFontSize (_ source: AnyObject)
  247. {
  248. terminal.font = NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
  249. }
  250. @objc @IBAction
  251. func addTab (_ source: AnyObject)
  252. {
  253. // if let win = view.window {
  254. // win.tabbingMode = .preferred
  255. // if let wc = win.windowController {
  256. // if let d = wc.document as? Document {
  257. // do {
  258. // let x = Document()
  259. // x.makeWindowControllers()
  260. //
  261. // try NSDocumentController.shared.newDocument(self)
  262. // } catch {}
  263. // print ("\(d.debugDescription)")
  264. // }
  265. // }
  266. // }
  267. // win.tabbingMode = .preferred
  268. // win.addTabbedWindow(win, ordered: .above)
  269. //
  270. // if let wc = win.windowController {
  271. // wc.newWindowForTab(self()
  272. // wc.showWindow(source)
  273. // }
  274. // }
  275. }
  276. func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool
  277. {
  278. if item.action == #selector(debugToggleHostLogging(_:)) {
  279. if let m = item as? NSMenuItem {
  280. m.state = logging ? NSControl.StateValue.on : NSControl.StateValue.off
  281. }
  282. }
  283. if item.action == #selector(resizificator(_:)) {
  284. if let m = item as? NSMenuItem {
  285. m.state = resizificating == 1 ? NSControl.StateValue.on : NSControl.StateValue.off
  286. }
  287. }
  288. if item.action == #selector(resizificatorDown(_:)) {
  289. if let m = item as? NSMenuItem {
  290. m.state = resizificating == -1 ? NSControl.StateValue.on : NSControl.StateValue.off
  291. }
  292. }
  293. if item.action == #selector(allowMouseReporting(_:)) {
  294. if let m = item as? NSMenuItem {
  295. m.state = terminal.allowMouseReporting ? NSControl.StateValue.on : NSControl.StateValue.off
  296. }
  297. }
  298. if item.action == #selector(toggleOptionAsMetaKey(_:)) {
  299. if let m = item as? NSMenuItem {
  300. m.state = terminal.optionAsMetaKey ? NSControl.StateValue.on : NSControl.StateValue.off
  301. }
  302. }
  303. // Only enable "Export selection" if we have a selection
  304. if item.action == #selector(exportSelection(_:)) {
  305. return terminal.selectionActive
  306. }
  307. return true
  308. }
  309. @objc @IBAction
  310. func debugToggleHostLogging (_ source: AnyObject)
  311. {
  312. logging = !logging
  313. updateLogging()
  314. }
  315. }