2016-09-11 15:28:56 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
class PrefUtils {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate static let whitespaceCharSet = CharacterSet.whitespaces
|
2016-09-11 15:28:56 +03:00
|
|
|
|
|
|
|
static func ignorePatterns(fromString str: String) -> Set<FileItemIgnorePattern> {
|
2016-09-25 18:50:33 +03:00
|
|
|
if str.trimmingCharacters(in: self.whitespaceCharSet).characters.count == 0 {
|
2016-09-11 15:28:56 +03:00
|
|
|
return Set()
|
|
|
|
}
|
|
|
|
|
|
|
|
let patterns: [FileItemIgnorePattern] = str
|
2016-09-25 18:50:33 +03:00
|
|
|
.components(separatedBy: ",")
|
2016-09-11 15:28:56 +03:00
|
|
|
.flatMap {
|
2016-09-25 18:50:33 +03:00
|
|
|
let trimmed = $0.trimmingCharacters(in: self.whitespaceCharSet)
|
2016-09-11 15:28:56 +03:00
|
|
|
if trimmed.characters.count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileItemIgnorePattern(pattern: trimmed)
|
|
|
|
}
|
2016-11-20 12:51:15 +03:00
|
|
|
|
2016-09-11 15:28:56 +03:00
|
|
|
return Set(patterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func ignorePatternString(fromSet set: Set<FileItemIgnorePattern>) -> String {
|
2016-09-11 16:22:56 +03:00
|
|
|
return Array(set)
|
2016-09-11 15:28:56 +03:00
|
|
|
.map { $0.pattern }
|
2016-09-25 18:50:33 +03:00
|
|
|
.sorted()
|
|
|
|
.joined(separator: ", ")
|
2016-09-11 15:28:56 +03:00
|
|
|
}
|
2016-11-19 15:29:18 +03:00
|
|
|
|
|
|
|
static func value<T>(from dict: [String: Any], for key: String) -> T? {
|
|
|
|
return dict[key] as? T
|
|
|
|
}
|
|
|
|
|
|
|
|
static func value<T>(from dict: [String: Any], for key: String, default defaultValue: T) -> T {
|
|
|
|
return dict[key] as? T ?? defaultValue
|
|
|
|
}
|
|
|
|
|
2016-12-05 00:03:40 +03:00
|
|
|
static func dict(from dict: [String: Any], for key: String) -> [String: Any]? {
|
|
|
|
return dict[key] as? [String: Any]
|
|
|
|
}
|
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
static func float(from dict: [String: Any], for key: String, default defaultValue: Float) -> Float {
|
|
|
|
return (dict[key] as? NSNumber)?.floatValue ?? defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
static func float(from dict: [String: Any], for key: String) -> Float? {
|
|
|
|
guard let number = dict[key] as? NSNumber else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return number.floatValue
|
|
|
|
}
|
|
|
|
|
|
|
|
static func bool(from dict: [String: Any], for key: String) -> Bool? {
|
|
|
|
guard let number = dict[key] as? NSNumber else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return number.boolValue
|
|
|
|
}
|
|
|
|
|
|
|
|
static func bool(from dict: [String: Any], for key: String, default defaultValue: Bool) -> Bool {
|
|
|
|
return (dict[key] as? NSNumber)?.boolValue ?? defaultValue
|
|
|
|
}
|
|
|
|
|
2017-01-01 09:52:02 +03:00
|
|
|
static func string(from dict: [String: Any], for key: String) -> String? {
|
|
|
|
return dict[key] as? String
|
|
|
|
}
|
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
static func saneFont(_ fontName: String, fontSize: CGFloat) -> NSFont {
|
|
|
|
var editorFont = NSFont(name: fontName, size: fontSize) ?? NeoVimView.defaultFont
|
|
|
|
if !editorFont.isFixedPitch {
|
|
|
|
editorFont = NSFontManager.shared().convert(NeoVimView.defaultFont, toSize: editorFont.pointSize)
|
|
|
|
}
|
|
|
|
if editorFont.pointSize < NeoVimView.minFontSize || editorFont.pointSize > NeoVimView.maxFontSize {
|
|
|
|
editorFont = NSFontManager.shared().convert(editorFont, toSize: NeoVimView.defaultFont.pointSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return editorFont
|
|
|
|
}
|
|
|
|
|
|
|
|
static func saneLinespacing(_ fLinespacing: Float) -> CGFloat {
|
|
|
|
let linespacing = CGFloat(fLinespacing)
|
|
|
|
guard linespacing >= NeoVimView.minLinespacing && linespacing <= NeoVimView.maxLinespacing else {
|
|
|
|
return NeoVimView.defaultLinespacing
|
|
|
|
}
|
|
|
|
|
|
|
|
return linespacing
|
|
|
|
}
|
2016-11-20 12:51:15 +03:00
|
|
|
|
|
|
|
static func location(from strValue: String) -> WorkspaceBarLocation? {
|
|
|
|
switch strValue {
|
|
|
|
case "top": return .top
|
|
|
|
case "right": return .right
|
|
|
|
case "bottom": return .bottom
|
|
|
|
case "left": return .left
|
|
|
|
default: return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func locationAsString(for loc: WorkspaceBarLocation) -> String {
|
|
|
|
switch loc {
|
|
|
|
case .top: return "top"
|
|
|
|
case .right: return "right"
|
|
|
|
case .bottom: return "bottom"
|
|
|
|
case .left: return "left"
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 18:50:33 +03:00
|
|
|
}
|