Add more formatting options to the file output name prefs.
This commit is contained in:
7 files changed
+271
-47
No files matched your search
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14101" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14105" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<deployment identifier="macosx"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14101"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14105"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
@@ -50,7 +50,6 @@
|
||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
</tokenFieldCell>
|
||||
<connections>
|
||||
<binding destination="BZm-iR-6oK" name="value" keyPath="values.SBMovieFormat" id="mdW-QG-L7g"/>
|
||||
<outlet property="delegate" destination="-2" id="39Q-aI-o60"/>
|
||||
</connections>
|
||||
</tokenField>
|
||||
@@ -62,7 +61,6 @@
|
||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
</tokenFieldCell>
|
||||
<connections>
|
||||
<binding destination="BZm-iR-6oK" name="value" keyPath="values.SBTVShowFormat" id="Fa2-rF-dBm"/>
|
||||
<outlet property="delegate" destination="-2" id="mh9-CJ-zVx"/>
|
||||
</connections>
|
||||
</tokenField>
|
||||
|
||||
@@ -71,11 +71,11 @@ extension MP42File {
|
||||
return MetadataSearchTerms.none
|
||||
}
|
||||
|
||||
private func outputNameFormat(mediaKind: Int) -> [String]? {
|
||||
private func outputNameFormat(mediaKind: Int) -> [Token]? {
|
||||
if mediaKind == MetadataResult.MediaKindType.tvShow.rawValue {
|
||||
return UserDefaults.standard.stringArray(forKey: "SBTVShowFormat")
|
||||
return UserDefaults.standard.tokenArray(forKey: "SBTVShowFormat")
|
||||
} else if mediaKind == MetadataResult.MediaKindType.movie.rawValue {
|
||||
return UserDefaults.standard.stringArray(forKey: "SBMovieFormat")
|
||||
return UserDefaults.standard.tokenArray(forKey: "SBMovieFormat")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -102,15 +102,15 @@ extension MP42File {
|
||||
var name = ""
|
||||
|
||||
for token in format {
|
||||
if token.hasPrefix("{") && token.hasSuffix("}") {
|
||||
let trimmedToken = token.trimmingCharacters(in: separators)
|
||||
if token.isPlaceholder {
|
||||
let trimmedToken = token.text.trimmingCharacters(in: separators)
|
||||
let metadataItems = metadata.metadataItemsFiltered(byIdentifier: trimmedToken)
|
||||
|
||||
if let string = metadataItems.first?.stringValue {
|
||||
name.append(string)
|
||||
name.append(token.format(text: string))
|
||||
}
|
||||
} else {
|
||||
name.append(token)
|
||||
name.append(token.text)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,8 +34,16 @@ class OutputPrefsViewController: NSViewController, NSTokenFieldDelegate {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
movieField.tokenizingCharacterSet = CharacterSet(charactersIn: "%")
|
||||
tvShowField.tokenizingCharacterSet = CharacterSet(charactersIn: "%")
|
||||
movieField.tokenizingCharacterSet = CharacterSet(charactersIn: "/")
|
||||
tvShowField.tokenizingCharacterSet = CharacterSet(charactersIn: "/")
|
||||
|
||||
movieField.objectValue = UserDefaults.standard.tokenArray(forKey: "SBMovieFormat")
|
||||
tvShowField.objectValue = UserDefaults.standard.tokenArray(forKey: "SBTVShowFormat")
|
||||
}
|
||||
|
||||
private func save() {
|
||||
UserDefaults.standard.set(movieField.objectValue as! [Token], forKey: "SBMovieFormat")
|
||||
UserDefaults.standard.set(tvShowField.objectValue as! [Token], forKey: "SBTVShowFormat")
|
||||
}
|
||||
|
||||
// MARK: Actions
|
||||
@@ -79,19 +87,19 @@ class OutputPrefsViewController: NSViewController, NSTokenFieldDelegate {
|
||||
// MARK: Format Token Field Delegate
|
||||
|
||||
override func controlTextDidEndEditing(_ obj: Notification) {
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, displayStringForRepresentedObject representedObject: Any) -> String? {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return localizedMetadataKeyName(stringValue.trimmingCharacters(in: separators))
|
||||
if let token = representedObject as? Token {
|
||||
return localizedMetadataKeyName(token.text.trimmingCharacters(in: separators))
|
||||
}
|
||||
return representedObject as? String
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, styleForRepresentedObject representedObject: Any) -> NSTokenField.TokenStyle {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return .rounded
|
||||
if let token = representedObject as? Token {
|
||||
return token.isPlaceholder ? .rounded : .none
|
||||
}
|
||||
else {
|
||||
return .none
|
||||
@@ -99,19 +107,13 @@ class OutputPrefsViewController: NSViewController, NSTokenFieldDelegate {
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, representedObjectForEditing editingString: String) -> Any? {
|
||||
// Disable whitespace-trimming
|
||||
return editingString
|
||||
let control = editingString.hasPrefix("{") && editingString.hasSuffix("}")
|
||||
return Token(text: editingString, isPlaceholder: control)
|
||||
}
|
||||
|
||||
// func tokenField(_ tokenField: NSTokenField, completionsForSubstring substring: String, indexOfToken tokenIndex: Int, indexOfSelectedItem selectedIndex: UnsafeMutablePointer<Int>?) -> [Any]? {
|
||||
// matches = currentTokens.filter { $0.hasPrefix(substring) }
|
||||
// return matches
|
||||
//
|
||||
// }
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, editingStringForRepresentedObject representedObject: Any) -> String? {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return "%\(stringValue)%"
|
||||
if let token = representedObject as? Token {
|
||||
return token.isPlaceholder ? "/\(token.text)/" : token.text
|
||||
}
|
||||
return representedObject as? String
|
||||
}
|
||||
@@ -121,12 +123,96 @@ class OutputPrefsViewController: NSViewController, NSTokenFieldDelegate {
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, writeRepresentedObjects objects: [Any], to pboard: NSPasteboard) -> Bool {
|
||||
if let strings = objects as? [String] {
|
||||
let string = strings.reduce("", { "\($0)%\($1)" })
|
||||
if let tokens = objects as? [Token] {
|
||||
let string = tokens.reduce("", { "\($0)/{\($1.text)}" })
|
||||
pboard.setString(string, forType: NSPasteboard.PasteboardType.string)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MARK: Format Token Field Delegate Menu
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, hasMenuForRepresentedObject representedObject: Any) -> Bool {
|
||||
if let token = representedObject as? Token, token.isPlaceholder {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, menuForRepresentedObject representedObject: Any) -> NSMenu? {
|
||||
if let token = representedObject as? Token, token.isPlaceholder {
|
||||
return menu(for: token)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func setTokenCase(_ sender: NSMenuItem) {
|
||||
guard let token = sender.representedObject as? Token,
|
||||
let tokenCase = Token.Case(rawValue: sender.tag) else { return }
|
||||
|
||||
if token.textCase == tokenCase {
|
||||
token.textCase = .none
|
||||
} else {
|
||||
token.textCase = tokenCase
|
||||
}
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
@IBAction func setTokenPadding(_ sender: NSMenuItem) {
|
||||
guard let token = sender.representedObject as? Token,
|
||||
let tokenPadding = Token.Padding(rawValue: sender.tag) else { return }
|
||||
|
||||
if token.textPadding == tokenPadding {
|
||||
token.textPadding = .none
|
||||
} else {
|
||||
token.textPadding = tokenPadding
|
||||
}
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
private func menu(for token: Token) -> NSMenu {
|
||||
let menu = NSMenu(title: "Item Menu")
|
||||
menu.autoenablesItems = false
|
||||
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("Capitalize", comment: ""), tag: Token.Case.capitalize.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("lowercase", comment: ""), tag: Token.Case.lower.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("UPPERCASE", comment: ""), tag: Token.Case.upper.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("CamelCase", comment: ""), tag: Token.Case.camel.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("snake_case", comment: ""), tag: Token.Case.snake.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("train-case", comment: ""), tag: Token.Case.train.rawValue, token: token))
|
||||
menu.addItem(caseMenuItem(title: NSLocalizedString("dot.case", comment: ""), tag: Token.Case.dot.rawValue, token: token))
|
||||
|
||||
menu.addItem(paddingMenuItem(title: NSLocalizedString("Leading zero", comment: ""), tag: Token.Padding.leadingzero.rawValue, token: token))
|
||||
|
||||
return menu
|
||||
}
|
||||
|
||||
private func caseMenuItem(title: String, tag: Int, token: Token) -> NSMenuItem {
|
||||
let item = NSMenuItem(title: title, action: #selector(setTokenCase(_:)), keyEquivalent: "")
|
||||
item.isEnabled = true
|
||||
item.representedObject = token
|
||||
item.tag = tag
|
||||
if tag == token.textCase.rawValue {
|
||||
item.state = .on
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
private func paddingMenuItem(title: String, tag: Int, token: Token) -> NSMenuItem {
|
||||
let item = NSMenuItem(title: title, action: #selector(setTokenPadding(_:)), keyEquivalent: "")
|
||||
item.isEnabled = true
|
||||
item.representedObject = token
|
||||
item.tag = tag
|
||||
if tag == token.textPadding.rawValue {
|
||||
item.state = .on
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,11 +9,16 @@ import Cocoa
|
||||
|
||||
@objc(SBPrefsWindowController) class PrefsWindowController: NSWindowController {
|
||||
|
||||
@objc public class func registerUserDefaults() {
|
||||
public class func registerUserDefaults() {
|
||||
let defaults = UserDefaults.standard
|
||||
let movieDefaultMap = NSKeyedArchiver.archivedData(withRootObject: MetadataResultMap.movieDefaultMap)
|
||||
let tvShowDefaultMap = NSKeyedArchiver.archivedData(withRootObject: MetadataResultMap.tvShowDefaultMap)
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
|
||||
let movieFormat = try? encoder.encode([Token(text: "{Name}")])
|
||||
let tvShowFormat = try? encoder.encode([Token(text: "{TV Show}"), Token(text: " s", isPlaceholder: false), Token(text: "{TV Season}"), Token(text: "e", isPlaceholder: false), Token(text: "{TV Episode #}")])
|
||||
|
||||
// Migrate 1.2.9 DTS setting
|
||||
if defaults.object(forKey: "SBAudioKeepDts") != nil {
|
||||
if defaults.bool(forKey: "SBAudioKeepDts") {
|
||||
@@ -38,8 +43,8 @@ import Cocoa
|
||||
"chaptersPreviewTrack": true,
|
||||
"SBChaptersPreviewPosition": 0.5,
|
||||
|
||||
"SBMovieFormat": ["{Name}"],
|
||||
"SBTVShowFormat": ["{TV Show}"," s", "{TV Season}", "e", "{TV Episode #}"],
|
||||
"SBMovieFormat": movieFormat ?? "",
|
||||
"SBTVShowFormat": tvShowFormat ?? "",
|
||||
"SBSetMovieFormat": false,
|
||||
"SBSetTVShowFormat": false,
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// Token.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 06/03/2018.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Token: Codable {
|
||||
|
||||
enum Case: Int, Codable {
|
||||
case none
|
||||
case capitalize
|
||||
case lower
|
||||
case upper
|
||||
case camel
|
||||
case snake
|
||||
case train
|
||||
case dot
|
||||
}
|
||||
|
||||
enum Padding: Int, Codable {
|
||||
case none
|
||||
case leadingzero
|
||||
}
|
||||
|
||||
let text: String
|
||||
let isPlaceholder: Bool
|
||||
|
||||
var textCase: Case
|
||||
var textPadding: Padding
|
||||
|
||||
init(text: String, isPlaceholder: Bool = true) {
|
||||
self.text = text
|
||||
self.textCase = .none
|
||||
self.textPadding = .none
|
||||
self.isPlaceholder = isPlaceholder
|
||||
}
|
||||
|
||||
func format(text: String) -> String {
|
||||
var formattedText = text
|
||||
|
||||
switch textCase {
|
||||
case .none:
|
||||
break
|
||||
case .capitalize:
|
||||
formattedText = text.capitalized
|
||||
case .lower:
|
||||
formattedText = text.lowercased()
|
||||
case .upper:
|
||||
formattedText = text.uppercased()
|
||||
case .camel:
|
||||
formattedText = text.camelCased()
|
||||
case .snake:
|
||||
formattedText = text.snakeCased()
|
||||
case .train:
|
||||
formattedText = text.trainCased()
|
||||
case .dot:
|
||||
formattedText = text.dotCased()
|
||||
}
|
||||
|
||||
switch textPadding {
|
||||
case .none:
|
||||
break
|
||||
case .leadingzero:
|
||||
let formatter = NumberFormatter()
|
||||
formatter.minimumIntegerDigits = 2
|
||||
formatter.paddingPosition = .beforePrefix
|
||||
formatter.paddingCharacter = "0"
|
||||
if let number = Int(formattedText) {
|
||||
formattedText = formatter.string(from: number as NSNumber) ?? formattedText
|
||||
}
|
||||
}
|
||||
|
||||
return formattedText
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate extension String {
|
||||
func capitalizingFirstLetter() -> String {
|
||||
return prefix(1).uppercased() + dropFirst()
|
||||
}
|
||||
|
||||
func camelCased() -> String {
|
||||
return self.components(separatedBy: " ")
|
||||
.map { return $0.lowercased().capitalizingFirstLetter() }
|
||||
.joined()
|
||||
}
|
||||
|
||||
func snakeCased() -> String {
|
||||
return self.components(separatedBy: " ")
|
||||
.map { return $0.lowercased() }
|
||||
.joined(separator: "_")
|
||||
}
|
||||
|
||||
func trainCased() -> String {
|
||||
return self.components(separatedBy: " ")
|
||||
.map { return $0.lowercased() }
|
||||
.joined(separator: "-")
|
||||
}
|
||||
|
||||
func dotCased() -> String {
|
||||
return self.components(separatedBy: " ")
|
||||
.map { return $0.lowercased() }
|
||||
.joined(separator: ".")
|
||||
}
|
||||
}
|
||||
|
||||
extension UserDefaults {
|
||||
|
||||
func tokenArray(forKey defaultName: String) -> [Token] {
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
guard let jsonData = self.data(forKey: defaultName),
|
||||
let decoded = try? decoder.decode([Token].self, from: jsonData) else { return [] }
|
||||
|
||||
return decoded
|
||||
}
|
||||
|
||||
func set(_ tokenArray: [Token], forKey defaultName: String) {
|
||||
let encoder = JSONEncoder()
|
||||
let jsonData = try? encoder.encode(tokenArray)
|
||||
self.set(jsonData, forKey: defaultName)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import Cocoa
|
||||
class TokensViewController: NSViewController, NSTokenFieldDelegate {
|
||||
|
||||
@IBOutlet var tokenField: NSTokenField!
|
||||
let tokens: [String]
|
||||
let tokens: [Token]
|
||||
let separators: CharacterSet
|
||||
|
||||
override var nibName: NSNib.Name? {
|
||||
@@ -18,7 +18,7 @@ class TokensViewController: NSViewController, NSTokenFieldDelegate {
|
||||
}
|
||||
|
||||
init(tokens: [String]) {
|
||||
self.tokens = tokens
|
||||
self.tokens = tokens.map { Token(text: "{\($0)}") }
|
||||
self.separators = CharacterSet(charactersIn: "{}")
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
}
|
||||
@@ -30,31 +30,35 @@ class TokensViewController: NSViewController, NSTokenFieldDelegate {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
tokenField.tokenizingCharacterSet = CharacterSet(charactersIn: "%")
|
||||
tokenField.stringValue = tokens.reduce("", { "\($0)%{\($1)}" })
|
||||
tokenField.tokenizingCharacterSet = CharacterSet(charactersIn: "/")
|
||||
tokenField.objectValue = tokens
|
||||
}
|
||||
|
||||
// MARK: Format Token Field Delegate
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, displayStringForRepresentedObject representedObject: Any) -> String? {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return localizedMetadataKeyName(stringValue.trimmingCharacters(in: separators))
|
||||
if let token = representedObject as? Token {
|
||||
return localizedMetadataKeyName(token.text.trimmingCharacters(in: separators))
|
||||
}
|
||||
return representedObject as? String
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, styleForRepresentedObject representedObject: Any) -> NSTokenField.TokenStyle {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return .rounded
|
||||
if let token = representedObject as? Token {
|
||||
return token.isPlaceholder ? .rounded : .none
|
||||
}
|
||||
else {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, representedObjectForEditing editingString: String) -> Any? {
|
||||
return Token(text: editingString)
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, editingStringForRepresentedObject representedObject: Any) -> String? {
|
||||
if let stringValue = representedObject as? String, stringValue.hasPrefix("{") {
|
||||
return "%\(stringValue)%"
|
||||
if let token = representedObject as? Token {
|
||||
return "/\(token.text)/"
|
||||
}
|
||||
return representedObject as? String
|
||||
}
|
||||
@@ -64,8 +68,8 @@ class TokensViewController: NSViewController, NSTokenFieldDelegate {
|
||||
}
|
||||
|
||||
func tokenField(_ tokenField: NSTokenField, writeRepresentedObjects objects: [Any], to pboard: NSPasteboard) -> Bool {
|
||||
if let strings = objects as? [String] {
|
||||
let string = strings.reduce("", { "\($0)%\($1)" })
|
||||
if let tokens = objects as? [Token] {
|
||||
let string = tokens.reduce("", { "\($0)/\($1.text)" })
|
||||
pboard.setString(string, forType: NSPasteboard.PasteboardType.string)
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user