Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zentrope
Created February 2, 2020 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/445cd01a353f78cfc45ca74133fa646f to your computer and use it in GitHub Desktop.
Save zentrope/445cd01a353f78cfc45ca74133fa646f to your computer and use it in GitHub Desktop.
private func handleMessage(_ message: IRC.Message) {
if let storage = chatTextView.textStorage,
let render = format(message) {
storage.append(render)
//storage.insert(render, at: storage.length)
}
chatTextView.scrollToEndOfDocument(self)
}
private let chatFont = NSFont.systemFont(ofSize: 14)
private let protocolFont = NSFont.systemFont(ofSize: 12)
private func format(_ message: IRC.Message) -> NSAttributedString? {
let p = NSMutableParagraphStyle()
p.paragraphSpacing = 14.0
switch message.command {
case .NOTICE:
let trailing = message.trailing.count > 0 ? String(message.trailing.dropFirst()) : ""
let s = NSMutableAttributedString(string: "\(trailing)\r\n")
// let r = NSMakeRange(0, s.length)
let attributes: [NSAttributedString.Key: Any] = [
.font: protocolFont,
.foregroundColor: NSColor.systemRed,
.paragraphStyle: p
]
// s.addAttribute(.font, value: protocolFont, range: r)
// s.addAttribute(.foregroundColor, value: NSColor.systemRed, range: r)
s.setAttributes(attributes, range: NSMakeRange(0, s.length))
return s as NSAttributedString
case .PRIVMSG:
let user = NSMutableAttributedString(string: message.nick + ": ")
user.addAttribute(.foregroundColor, value: NSColor.systemGreen, range: NSMakeRange(0, user.length))
let channel = NSMutableAttributedString(string: (message.parameters.count > 0 ? message.parameters[0] : "-") + " ")
channel.addAttribute(.foregroundColor, value: NSColor.systemBlue, range: NSMakeRange(0, channel.length))
let text = NSMutableAttributedString()
text.append(channel)
//text.append(NSAttributedString(string:" "))
text.append(user)
//text.append(NSAttributedString(string:": "))
text.append(NSAttributedString(string: (message.trailing.count > 0 ? String(message.trailing.dropFirst()) : "") + "\n"))
text.addAttribute(.font, value: chatFont, range: NSMakeRange(0, text.length))
return text as NSAttributedString
default:
let cmd = "\(message.command)".lowercased()
let ps = message.parameters.joined(separator: ", ")
let s = NSMutableAttributedString(string: "» \(cmd) \(ps) \(message.trailing)\r\n")
let r = NSMakeRange(0, s.length)
s.addAttribute(.font, value: protocolFont, range: r)
s.addAttribute(.foregroundColor, value: NSColor.systemGray, range: r)
return s as NSAttributedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment