99import UIKit
1010
1111protocol TerminalControllerDelegate {
12-
12+
1313 func refresh( attributedString: NSAttributedString , backgroundColor: UIColor )
1414 func activateBell( )
1515 func close( )
1616 func didReceiveError( error: Error )
17-
17+
1818 func openSettings( )
19-
19+
2020}
2121
2222class TerminalController : VT100 {
23-
23+
2424 var delegate : TerminalControllerDelegate ?
25-
25+
2626 private var updateQueue : DispatchQueue !
27-
27+
2828 private var stringSupplier = VT100StringSupplier ( )
29-
29+
3030 var colorMap : VT100ColorMap {
3131 get { return stringSupplier. colorMap! }
3232 set { stringSupplier. colorMap = newValue }
3333 }
34-
34+
3535 var fontMetrics : FontMetrics {
3636 get { return stringSupplier. fontMetrics! }
3737 set { stringSupplier. fontMetrics = newValue }
3838 }
39-
39+
4040 private var subProcess : SubProcess ?
4141 private var processEnded : Bool = false
42-
42+
4343 override var screenSize : ScreenSize {
4444 get { return super. screenSize }
4545
@@ -52,45 +52,45 @@ class TerminalController: VT100 {
5252 subProcess? . screenSize = screenSize
5353 }
5454 }
55-
55+
5656 override init ( ) {
5757 super. init ( )
58-
58+
5959 updateQueue = DispatchQueue ( label: String ( format: " au.com.hbang.NewTerm.update-queue-%p " , self ) )
60-
60+
6161 stringSupplier. screenBuffer = self
62-
62+
6363 NotificationCenter . default. addObserver ( self , selector: #selector( self . preferencesUpdated) , name: UserDefaults . didChangeNotification, object: nil )
6464 preferencesUpdated ( )
6565 }
66-
66+
6767 @objc func preferencesUpdated( ) {
6868 let preferences = Preferences . shared
6969 stringSupplier. colorMap = preferences. colorMap
7070 stringSupplier. fontMetrics = preferences. fontMetrics
71-
71+
7272 refresh ( )
7373 }
74-
74+
7575 // MARK: - Sub Process
76-
76+
7777 func startSubProcess( ) throws {
7878 processEnded = false
79-
79+
8080 subProcess = SubProcess ( )
8181 subProcess!. delegate = self
8282 try subProcess!. start ( )
8383 }
84-
84+
8585 func stopSubProcess( ) throws {
8686 processEnded = true
8787 try subProcess!. stop ( )
8888 }
89-
89+
9090}
9191
9292extension TerminalController : TerminalInputProtocol {
93-
93+
9494 func receiveKeyboardInput( data: Data ) {
9595 if processEnded {
9696 // we’ve told the user that pressing a key will close the tab, so close the tab
@@ -104,68 +104,68 @@ extension TerminalController: TerminalInputProtocol {
104104 func openSettings( ) {
105105 delegate!. openSettings ( )
106106 }
107-
107+
108108}
109109
110110// ScreenBufferRefreshDelegate
111111extension TerminalController {
112-
112+
113113 override func refresh( ) {
114114 super. refresh ( )
115-
115+
116116 // TODO: this is called due to -[VT100 init], and we aren’t ready yet… we’ll be called when we
117117 // are anyway, so don’t worry about it
118118 if updateQueue == nil {
119119 return
120120 }
121-
121+
122122 // TODO: we should handle the scrollback separately so it only appears if the user scrolls
123123 updateQueue. async {
124124 let attributedString = self . stringSupplier. attributedString ( ) !
125125 let backgroundColor = self . stringSupplier. colorMap!. background!
126-
126+
127127 DispatchQueue . main. async {
128128 self . delegate? . refresh ( attributedString: attributedString, backgroundColor: backgroundColor)
129129 }
130130 }
131131 }
132-
132+
133133 override func activateBell( ) {
134134 super. activateBell ( )
135135 delegate? . activateBell ( )
136136 }
137-
137+
138138}
139139
140140extension TerminalController : SubProcessDelegate {
141-
141+
142142 func subProcessDidConnect( ) {
143143 // yay
144144 }
145-
145+
146146 func subProcess( didReceiveData data: Data ) {
147147 // Simply forward the input stream down the VT100 processor. When it notices changes to the
148148 // screen, it should invoke our refresh delegate below.
149149 readInputStream ( data)
150150 }
151-
151+
152152 func subProcess( didDisconnectWithError error: Error ? ) {
153153 // we have nothing useful to do if the process has already ended
154154 if processEnded {
155155 return
156156 }
157-
157+
158158 processEnded = true
159159
160160 // show a message inside the terminal indicating that the session has ended, and that the user
161161 // can press any key to close the tab
162162 let message = " [ \( NSLocalizedString ( " PROCESS_COMPLETED_TITLE " , comment: " " ) ) ] \r \n \( NSLocalizedString ( " PROCESS_COMPLETED_MESSAGE " , comment: " " ) ) "
163163 readInputStream ( message. data ( using: . utf8) )
164164 }
165-
165+
166166 func subProcess( didReceiveError error: Error ) {
167167 NSLog ( " subprocess received error… %@ " , error as NSError )
168168 delegate? . didReceiveError ( error: error)
169169 }
170-
170+
171171}
0 commit comments