Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. func serialPortWasOpened(_ serialPort: ORSSerialPort) {
  2. let regex = try! NSRegularExpression(pattern: "\[0;3.{1}m.*\[0m", options: [])
  3. let descriptor = ORSSerialPacketDescriptor(regularExpression: regex, maximumPacketLength: 512, userInfo: nil)
  4. self.serialPort!.startListeningForPackets(matching: descriptor)
  5. }
  6.  
  7. func serialPort(_ serialPort: ORSSerialPort, didReceivePacket packetData: Data, matching descriptor: ORSSerialPacketDescriptor) {
  8. if let string = NSString(data: packetData, encoding: String.Encoding.utf8.rawValue) {
  9. textView.textStorage?.append(textView.appendANSIColoredText(string: string))
  10. print(">>> (string) <<<")
  11. }
  12. }
  13.  
  14. ...
  15.  
  16. extension NSTextView {
  17. func appendANSIColoredText(string: NSString) -> NSAttributedString {
  18. let color:NSColor;
  19.  
  20. switch string {
  21. case _ where string.hasPrefix("[0;30m"): color = .black
  22. case _ where string.hasPrefix("[0;31m"): color = .red
  23. case _ where string.hasPrefix("[0;32m"): color = .green
  24. case _ where string.hasPrefix("[0;33m"): color = .yellow
  25. case _ where string.hasPrefix("[0;34m"): color = .blue
  26. case _ where string.hasPrefix("[0;35m"): color = .magenta
  27. case _ where string.hasPrefix("[0;36m"): color = .cyan
  28. case _ where string.hasPrefix("[0;37m"): color = .white
  29. case _ where string.hasPrefix("[0;39m"): color = .magenta
  30. default: color = .white
  31. }
  32. let str = ( string.hasPrefix("[0;3") ) ? string.substring(with: NSRange(location: 6, length: string.length-11)) as NSString : string as NSString
  33.  
  34. let attributes: [NSAttributedString.Key: Any] = [
  35. .font: NSFont(name:"Menlo", size: 12.0) as Any,
  36. .foregroundColor: color
  37. ]
  38. return NSAttributedString(string: str as String, attributes: attributes)
  39. }
  40. }
  41.  
  42. Type "help()" for more information.
  43. >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement