armadsen / ORSSerialPort

Serial port library for Objective-C and Swift macOS apps
MIT License
755 stars 183 forks source link

Parsing ANSI coloured packets brings some unexpected behaviours #145

Open ilyamordasov opened 5 years ago

ilyamordasov commented 5 years ago

My serial has mixed data format, some of outputs has \[0;32m...\[0m, and some of them as a plain text.

func serialPortWasOpened(_ serialPort: ORSSerialPort) {
let regex = try! NSRegularExpression(pattern: "\\[0;3.{1}m.*\\[0m", options: [])
let descriptor = ORSSerialPacketDescriptor(regularExpression: regex, maximumPacketLength: 512, userInfo: nil)
        self.serialPort!.startListeningForPackets(matching: descriptor)
    }

func serialPort(_ serialPort: ORSSerialPort, didReceivePacket packetData: Data, matching descriptor: ORSSerialPacketDescriptor) {
        if let string = NSString(data: packetData, encoding: String.Encoding.utf8.rawValue) {       
            textView.textStorage?.append(textView.appendANSIColoredText(string: string))
            print(">>> \(string) <<<")
        }
    }

...

extension NSTextView {
    func appendANSIColoredText(string: NSString) -> NSAttributedString {
        let color:NSColor;

        switch string {
            case _ where string.hasPrefix("[0;30m"): color = .black
            case _ where string.hasPrefix("[0;31m"): color = .red
            case _ where string.hasPrefix("[0;32m"): color = .green
            case _ where string.hasPrefix("[0;33m"): color = .yellow
            case _ where string.hasPrefix("[0;34m"): color = .blue
            case _ where string.hasPrefix("[0;35m"): color = .magenta
            case _ where string.hasPrefix("[0;36m"): color = .cyan
            case _ where string.hasPrefix("[0;37m"): color = .white
            case _ where string.hasPrefix("[0;39m"): color = .magenta
            default: color = .white
        }
        let str = ( string.hasPrefix("\[0;3") ) ? string.substring(with: NSRange(location: 6, length: string.length-11)) as NSString : string as NSString

        let attributes: [NSAttributedString.Key: Any] = [
            .font: NSFont(name:"Menlo", size: 12.0) as Any,
            .foregroundColor: color
        ]
        return NSAttributedString(string: str as String, attributes: attributes)
    }
}

My full output is here http://bit.ly/esp32_swift According the code you can see that data which were gotten via serialPort didReceivePacket have >>> <<< markers, but not all....

and it seems didReceivePacket makes async response, cause my output must end with

Type "help()" for more information.
>>>

But sometimes after these lines there are some additional text