0
@IBAction func startLocalAction(_ sender: NSButton) {
    cmd(cmdStr: ["-c","cd Desktop;ls;git clone https://github.com/Fidetro/Masonry"])

}

@objc func receivedData(notification:NSNotification) {
    let fileHandle = notification.object
    if let handle = fileHandle as? FileHandle {
        let data = handle.availableData
        let result = String.init(data: data, encoding: .utf8)
        print(result)
        handle.waitForDataInBackgroundAndNotify()
    }
}

func cmd(cmdStr:[String]) {
    let outputPipe = Pipe()
    let process = Process()
    process.launchPath = "/bin/bash"
    process.currentDirectoryPath = NSHomeDirectory()
    //        let inputPipe = Pipe()
    process.arguments = cmdStr
    process.standardOutput = outputPipe
    //        process.standardInput = inputPipe
    let fileHandle = outputPipe.fileHandleForReading

    fileHandle.waitForDataInBackgroundAndNotify()
    NotificationCenter.default.addObserver(self, selector: .receivedData, name: NSNotification.Name.NSFileHandleDataAvailable, object: fileHandle)
    process.launch()
    fileHandle.waitForDataInBackgroundAndNotify()
}

output:

Cloning into 'Masonry'...
remote: Counting objects: 2650, done.        
Receiving objects:   0% (1/2650)   
Receiving objects:   1% (27/2650)   
Receiving objects:   2% (53/2650)   
Receiving objects:   3% (80/2650)   
Receiving objects:   4% (106/2650)   
Receiving objects:   4% (125/2650), 28.01 KiB | 20.00 KiB/s   

I want to get clone process percentage,I see output in terminal,but I don't know how to get I try Get Notification of task progress from NSTask,Getting process output using NSTask on-the-go?,but unused

Maarten Foukhar
  • 349
  • 1
  • 11
Karim
  • 322
  • 4
  • 20
  • According to https://stackoverflow.com/q/34820975, the output of "git clone" is written to stderr, not stdout. – Martin R Nov 21 '17 at 09:21
  • @MartinR thank, I change FileHandle `process.standardInput = inputPipe;let fileHandle = inputPipe.fileHandleForWriting`,but look like useless – Karim Nov 21 '17 at 09:31
  • Unless I am mistaken, you have to grab `process.standardError` instead of `process.standardOutput` – Martin R Nov 21 '17 at 09:32
  • @MartinR `process.standardError` catch `Cloning into 'Masonry'...` but can't catch other,sorry,maybe I am mistaken other – Karim Nov 22 '17 at 01:21

0 Answers0