]> git.r.bdr.sh - rbdr/lyricli/commitdiff
Merge branch 'feature/read-song-from-command-line' into develop
authorBen Beltran <redacted>
Fri, 19 May 2017 00:38:15 +0000 (19:38 -0500)
committerBen Beltran <redacted>
Fri, 19 May 2017 00:38:15 +0000 (19:38 -0500)
Sources/arguments_source.swift [new file with mode: 0644]
Sources/configuration.swift [new file with mode: 0644]
Sources/lyricli.swift [moved from Sources/Lyricli.swift with 50% similarity]
Sources/lyrics_engine.swift [new file with mode: 0644]
Sources/main.swift
Sources/source_manager.swift [new file with mode: 0644]
Sources/source_protocol.swift [new file with mode: 0644]
Sources/track.swift [new file with mode: 0644]

diff --git a/Sources/arguments_source.swift b/Sources/arguments_source.swift
new file mode 100644 (file)
index 0000000..c07c952
--- /dev/null
@@ -0,0 +1,17 @@
+/// 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
+        }
+    }
+}
diff --git a/Sources/configuration.swift b/Sources/configuration.swift
new file mode 100644 (file)
index 0000000..f5e4b0f
--- /dev/null
@@ -0,0 +1,62 @@
+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()
+        }
+    }
+}
similarity index 50%
rename from Sources/Lyricli.swift
rename to Sources/lyricli.swift
index 6c73e028e2ae23a0f8057ee0d5d594b4ba1c00ba..c44a7f401f4e9d3e20af94f58531764700340e70 100644 (file)
@@ -2,12 +2,31 @@
 public class Lyricli {
     public static var version = "0.0.0-feature/option-parsing"
 
+    public static var showTitle = false
+
     public static func printLyrics() {
-        print("Getting Lyrics: Not yet implemented")
-    }
 
-    public static func printTitle() {
-        print("Getting Song Title: Not yet implemented")
+        let sourceManager = SourceManager()
+
+        if let currentTrack = sourceManager.currentTrack {
+
+            let engine = LyricsEngine(withTrack: currentTrack)
+
+            if let lyrics = engine.lyrics {
+                if showTitle {
+                    printTitle(currentTrack)
+                }
+
+                print(lyrics)
+            }
+            else {
+                print("Lyrics not found :(")
+            }
+
+        }
+        else {
+            print("No Artist/Song could be found :(")
+        }
     }
 
     public static func printSources() {
@@ -25,4 +44,8 @@ public class Lyricli {
     public static func resetSource(_ sourceName: String) {
         print("Reset source \(sourceName): Not yet implemented")
     }
+
+    private static func printTitle(_ track: Track) {
+        print("\(track.artist) - \(track.name)")
+    }
 }
diff --git a/Sources/lyrics_engine.swift b/Sources/lyrics_engine.swift
new file mode 100644 (file)
index 0000000..661ce86
--- /dev/null
@@ -0,0 +1,20 @@
+/// Looks for lyrics on the internet
+class LyricsEngine {
+
+    let track: Track
+
+    var lyrics: String? {
+        get {
+            if track.artist == "test" && track.name == "test" {
+                return "Doo doo doo"
+            }
+
+            return nil
+        }
+    }
+
+    init(withTrack targetTrack: Track) {
+
+        track = targetTrack
+    }
+}
index 47720ad3a631b15d7feed0637520a729bf3c8662..e4b2760e873888695e8174b42db9c84b77ca82e3 100644 (file)
@@ -80,10 +80,14 @@ func main() {
 
     if let titleFlag = flags["title"] as? BoolOption {
         if titleFlag.value == true {
-            Lyricli.printTitle()
+            Lyricli.showTitle = true
         }
     }
 
+    // Remove any flags so anyone after this gets the unprocessed values
+    let programName: [String] = [CommandLine.arguments[0]]
+    CommandLine.arguments = programName + parser.unparsedArguments
+
     Lyricli.printLyrics()
 }
 
diff --git a/Sources/source_manager.swift b/Sources/source_manager.swift
new file mode 100644 (file)
index 0000000..dd956ad
--- /dev/null
@@ -0,0 +1,52 @@
+/// Manages the different sources. Keeps track of them, lists and toggles
+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) {
+    }
+}
diff --git a/Sources/source_protocol.swift b/Sources/source_protocol.swift
new file mode 100644 (file)
index 0000000..ee9350c
--- /dev/null
@@ -0,0 +1,3 @@
+protocol Source {
+    var currentTrack: Track? { get }
+}
diff --git a/Sources/track.swift b/Sources/track.swift
new file mode 100644 (file)
index 0000000..d2a9047
--- /dev/null
@@ -0,0 +1,11 @@
+/// Contains the artist and name of a track
+class Track {
+    let name: String
+    let artist: String
+
+    init(withName trackName: String, andArtist trackArtist: String) {
+
+        name = trackName
+        artist = trackArtist
+    }
+}