浏览代码

Wire up the OSC current-directory command to the Mac sample app

Miguel de Icaza 4 年之前
父节点
当前提交
4abff67a68

+ 6 - 0
Sources/SwiftTerm/Apple/AppleTerminalView.swift

@@ -222,6 +222,12 @@ extension TerminalView {
         terminal.updateFullScreen ()
     }
     
+    public func hostCurrentDirectoryUpdated (source: Terminal)
+    {
+        terminalDelegate?.hostCurrentDirectoryUpdate(source: self, directory: terminal.hostCurrentDirectory)
+    }
+
+    
     /// Installs the new colors as the default colors and recomputes the
     /// current and ansi palette.   This installs both the colors into the terminal
     /// engine and updates the UI accordingly.

+ 5 - 0
Sources/SwiftTerm/Apple/TerminalViewDelegate.swift

@@ -23,6 +23,11 @@ public protocol TerminalViewDelegate: class {
      */
     func setTerminalTitle(source: TerminalView, title: String)
   
+    /**
+     * Invoked when the OSC command 7 for "current directory has changed" command is sent
+     */
+    func hostCurrentDirectoryUpdate (source: TerminalView, directory: String?)
+    
     /**
      * Request that date be sent to the application running inside the terminal.
      * - Parameter data: Slice of data that should be sent

+ 12 - 0
Sources/SwiftTerm/Mac/MacLocalTerminalView.swift

@@ -25,6 +25,13 @@ public protocol LocalProcessTerminalViewDelegate {
      */
     func setTerminalTitle(source: LocalProcessTerminalView, title: String)
 
+    /**
+     * Invoked when the OSC command 7 for "current directory has changed" command is sent
+     * - Parameter source: the sending instance
+     * - Parameter directory: the new working directory
+     */
+    func hostCurrentDirectoryUpdate (source: TerminalView, directory: String?)
+
     /**
      * This method will be invoked when the child process started by `startProcess` has terminated.
      * - Parameter source: the local process that terminated
@@ -97,6 +104,11 @@ public class LocalProcessTerminalView: TerminalView, TerminalViewDelegate, Local
         processDelegate?.setTerminalTitle (source: self, title: title)
     }
 
+    public func hostCurrentDirectoryUpdate(source: TerminalView, directory: String?) {
+        processDelegate?.hostCurrentDirectoryUpdate(source: source, directory: directory)
+    }
+    
+
     /**
      * This method is invoked when input from the user needs to be sent to the client
      */

+ 28 - 1
TerminalApp/MacTerminal/ViewController.swift

@@ -15,6 +15,8 @@ class ViewController: NSViewController, LocalProcessTerminalViewDelegate, NSUser
     var changingSize = false
     var logging: Bool = false
     var zoomGesture: NSMagnificationGestureRecognizer?
+    var postedTitle: String = ""
+    var postedDirectory: String? = nil
     
     func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {
         if changingSize {
@@ -31,8 +33,33 @@ class ViewController: NSViewController, LocalProcessTerminalViewDelegate, NSUser
         changingSize = false
     }
     
+    func updateWindowTitle ()
+    {
+        var newTitle: String
+        if let dir = postedDirectory {
+            if let uri = URL(string: dir) {
+                if postedTitle == "" {
+                    newTitle = uri.path
+                } else {
+                    newTitle = "\(postedTitle) - \(uri.path)"
+                }
+            } else {
+                newTitle = postedTitle
+            }
+        } else {
+            newTitle = postedTitle
+        }
+        view.window?.title = newTitle
+    }
+    
     func setTerminalTitle(source: LocalProcessTerminalView, title: String) {
-        view.window?.title = title
+        postedTitle = title
+        updateWindowTitle ()
+    }
+    
+    func hostCurrentDirectoryUpdate (source: TerminalView, directory: String?) {
+        self.postedDirectory = directory
+        updateWindowTitle()
     }
     
     func processTerminated(source: TerminalView, exitCode: Int32?) {