Skip to content

Instantly share code, notes, and snippets.

@SamusAranX
Last active August 7, 2016 23:53
Show Gist options
  • Save SamusAranX/3a370370dd6d839f0772bab2ea4736e6 to your computer and use it in GitHub Desktop.
Save SamusAranX/3a370370dd6d839f0772bab2ea4736e6 to your computer and use it in GitHub Desktop.
Run this in a Swift playground with the .terminal profile that you want to parse in the playground's Resources folder. I wrote this because I wanted to copy the colors of an OS X terminal theme to my Linux Mint terminal but couldn't find a way to list all the colors in a .terminal file.
import Cocoa
extension NSColor {
var hexString: String {
if let convertedColor = self.colorUsingColorSpaceName(NSCalibratedRGBColorSpace) {
var redFloat: CGFloat = 0.0, greenFloat: CGFloat = 0.0, blueFloat: CGFloat = 0.0
convertedColor.getRed(&redFloat, green: &greenFloat, blue: &blueFloat, alpha: nil)
// the decimals are for when the cast to Int floor()s the floats, don't worry about it
let redInt = Int(redFloat * 255.99999),
greenInt = Int(greenFloat * 255.99999),
blueInt = Int(blueFloat * 255.99999)
let redHex = NSString(format: "%02x", redInt) as String,
greenHex = NSString(format: "%02x", greenInt) as String,
blueHex = NSString(format: "%02x", blueInt) as String
return "#" + redHex + greenHex + blueHex
}
return ""
}
}
func getTerminalColors(fromFile terminalFile: String) -> [(key: String, readableKey: String, color: NSColor, colorHex: String)] {
let terminalColorNames: [String] = [ "Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White" ]
let terminalColorNamesReadable = terminalColorNames.map({color in color}) +
terminalColorNames.map({color in "Bright \(color)"}) +
[ "Background", "Text", "Bold Text", "Cursor", "Selection" ]
let terminalColorKeys = terminalColorNames.map({color in "ANSI\(color)Color"}) +
terminalColorNames.map({color in "ANSIBright\(color)Color"}) +
[ "BackgroundColor", "TextColor", "TextBoldColor", "CursorColor", "SelectionColor" ]
let dict = NSDictionary(contentsOfFile: terminalFile) as! Dictionary<String, AnyObject>
var returnArray = [(key: String, readableKey: String, color: NSColor, colorHex: String)]()
for key in terminalColorKeys {
if let value = dict[key] {
if value is NSData {
if let colObj = NSKeyedUnarchiver.unarchiveObjectWithData(value as! NSData),
col = colObj as? NSColor {
let readableKey = terminalColorNamesReadable[terminalColorKeys.indexOf(key)!]
let colorTuple = (key: key, readableKey: readableKey, color: col, colorHex: col.hexString)
returnArray.append(colorTuple)
}
}
}
}
return returnArray
}
// Get the terminal theme here: https://twitter.com/panic/status/558389225612005376
// In this example, I just print the readable key and the hex color
if let path = NSBundle.mainBundle().pathForResource("Panic Palette", ofType: "terminal") {
let termColors = getTerminalColors(fromFile: path)
for t in termColors {
print("\(t.readableKey): \(t.colorHex)")
}
} else {
print("Couldn't load property list, something went wrong")
}
// Issues? https://twitter.com/SamusAranX
// getTerminalColors() returns an array of named tuples.
// Each tuple has the String properties "key", "readableKey", and "colorHex" and the NSColor property "color".
// For Panic Palette.terminal, the output of this gist looks like this:
// Black: #111c2a
// Red: #f82939
// Green: #3beb2a
// Yellow: #fcc146
// Blue: #2d4562
// Magenta: #6f4ebc
// Cyan: #63cce3
// White: #838c95
// Bright Black: #454640
// Bright Red: #f82939
// Bright Green: #3beb2a
// Bright Yellow: #fcc146
// Bright Blue: #2d4562
// Bright Magenta: #6f4ebc
// Bright Cyan: #63cce3
// Bright White: #838c95
// Background: #111c2a
// Text: #f6f7ee
// Bold Text: #ffffff
// Cursor: #f6f7ee
// Selection: #ffd8a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment