Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
- Type "help()" for more information.
- >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement