public static var version = "0.0.0-feature/option-parsing"
public static func printLyrics() {
- print("Getting Lyrics: Not yet implemented")
+
+ let sourceManager = SourceManager()
+
+ if let currentTrack = sourceManager.currentTrack {
+ print(currentTrack.artist)
+ print(currentTrack.name)
+ }
+ else {
+ print("Current track not found")
+ }
}
public static func printTitle() {
--- /dev/null
+/// Source that deals with command line
+class ArgumentsSource: Source {
+ public var currentTrack: Track? {
+ get {
+ if CommandLine.arguments.count >= 3 {
+
+ // expected usage: $ ./lyricli <artist> <name>
+
+ let trackName: String = CommandLine.arguments[2]
+ let trackArtist: String = CommandLine.arguments[1]
+
+ return Track(withName: trackName, andArtist: trackArtist)
+ }
+ return nil
+ }
+ }
+}
--- /dev/null
+import Foundation
+
+/// Handles reading and writing the configuration
+public class Configuration {
+
+ let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
+
+ // The defaults are added here
+
+ private var configuration: [String: Any] = [
+ "enabled_sources": ["arguments"],
+ "default": true
+ ]
+
+ static let sharedInstance: Configuration = Configuration()
+
+ private init() {
+
+ // Read the config file and attempt toset any of the values. Otherwise
+ // Don't do anything.
+ // IMPROVEMENT: Enable a debug mode
+
+ if let data = try? NSData(contentsOfFile: configurationPath) as Data {
+ if let parsedConfig = try? JSONSerialization.jsonObject(with: data) as! [String:Any] {
+ for (key, value) in parsedConfig {
+
+ if key == "enabled_sources" {
+ configuration[key] = value as! [String]
+ }
+ else {
+ configuration[key] = value as! String
+ }
+ }
+ }
+ }
+
+ writeConfiguration()
+ }
+
+ private func writeConfiguration() {
+
+ var error: NSError?
+
+ if let outputStream = OutputStream(toFileAtPath: configurationPath, append: false) {
+ outputStream.open()
+ JSONSerialization.writeJSONObject(configuration, to: outputStream, options: [JSONSerialization.WritingOptions.prettyPrinted], error: &error)
+ outputStream.close()
+ }
+ }
+
+ // Allow access as an object
+ subscript(index: String) -> Any? {
+ get {
+ return configuration[index]
+ }
+
+ set(newValue) {
+ configuration[index] = newValue
+ writeConfiguration()
+ }
+ }
+}
}
}
+ // Remove any flags so anyone after this gets the unprocessed values
+ let programName: [String] = [CommandLine.arguments[0]]
+ CommandLine.arguments = programName + parser.unparsedArguments
+
Lyricli.printLyrics()
}
--- /dev/null
+/// Manages the different sources. Keeps track of them, lists and toggles
+public class SourceManager {
+
+ private var availableSources: [String: Source] = [
+ "arguments": ArgumentsSource()
+ ]
+
+ var currentTrack: Track? {
+ get {
+
+ for source in enabledSources {
+ if let currentTrack = source.currentTrack {
+ return currentTrack
+ }
+ }
+
+ return nil
+ }
+ }
+
+ var enabledSources: [Source] {
+
+ // Checks the config and returns an array of sources based on the
+ // enabled and available ones
+
+ get {
+ var sources = [Source]()
+
+ if let sourceNames = Configuration.sharedInstance["enabled_sources"] as? [String]{
+ for sourceName in sourceNames {
+ if let source = availableSources[sourceName] {
+ sources.append(source)
+ }
+ }
+ }
+
+ return sources
+ }
+ }
+
+ func enable(sourceName: String) {
+ }
+
+ func disable(sourceName: String) {
+ }
+
+ func reset(sourceName: String) {
+ }
+
+ func getSources(sourceName: String) {
+ }
+}
--- /dev/null
+protocol Source {
+ var currentTrack: Track? { get }
+}
--- /dev/null
+/// Contains the artist and name of a track
+public class Track {
+ public let name: String
+ public let artist: String
+
+ init(withName trackName: String, andArtist trackArtist: String) {
+
+ name = trackName
+ artist = trackArtist
+ }
+}