Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing ANSI coloured packets brings some unexpected behaviours #145

Open
ilyamordasov opened this issue May 20, 2019 · 0 comments
Open

Comments

@ilyamordasov
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant