From 4425e9001e20e891dab7711644f83a1628788b47 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Wed, 17 May 2017 23:06:25 -0500 Subject: Add the arguments source --- Sources/Lyricli.swift | 11 +++++++- Sources/arguments_source.swift | 17 ++++++++++++ Sources/configuration.swift | 62 ++++++++++++++++++++++++++++++++++++++++++ Sources/main.swift | 4 +++ Sources/source_manager.swift | 52 +++++++++++++++++++++++++++++++++++ Sources/source_protocol.swift | 3 ++ Sources/track.swift | 11 ++++++++ 7 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 Sources/arguments_source.swift create mode 100644 Sources/configuration.swift create mode 100644 Sources/source_manager.swift create mode 100644 Sources/source_protocol.swift create mode 100644 Sources/track.swift (limited to 'Sources') diff --git a/Sources/Lyricli.swift b/Sources/Lyricli.swift index 6c73e02..d55b500 100644 --- a/Sources/Lyricli.swift +++ b/Sources/Lyricli.swift @@ -3,7 +3,16 @@ public class Lyricli { 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() { diff --git a/Sources/arguments_source.swift b/Sources/arguments_source.swift new file mode 100644 index 0000000..c07c952 --- /dev/null +++ b/Sources/arguments_source.swift @@ -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 + + 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 index 0000000..f5e4b0f --- /dev/null +++ b/Sources/configuration.swift @@ -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() + } + } +} diff --git a/Sources/main.swift b/Sources/main.swift index 47720ad..0ae48bc 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -84,6 +84,10 @@ func main() { } } + // 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 index 0000000..d0af817 --- /dev/null +++ b/Sources/source_manager.swift @@ -0,0 +1,52 @@ +/// 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) { + } +} diff --git a/Sources/source_protocol.swift b/Sources/source_protocol.swift new file mode 100644 index 0000000..ee9350c --- /dev/null +++ b/Sources/source_protocol.swift @@ -0,0 +1,3 @@ +protocol Source { + var currentTrack: Track? { get } +} diff --git a/Sources/track.swift b/Sources/track.swift new file mode 100644 index 0000000..efc09f4 --- /dev/null +++ b/Sources/track.swift @@ -0,0 +1,11 @@ +/// 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 + } +} -- cgit From a02972c1c6bfcc3248276ce43a9b0e0b6a9ba05e Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Wed, 17 May 2017 23:09:14 -0500 Subject: Rename because of case --- Sources/Lyricli.swift | 37 ------------------------------------- Sources/lor.swift | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 Sources/Lyricli.swift create mode 100644 Sources/lor.swift (limited to 'Sources') diff --git a/Sources/Lyricli.swift b/Sources/Lyricli.swift deleted file mode 100644 index d55b500..0000000 --- a/Sources/Lyricli.swift +++ /dev/null @@ -1,37 +0,0 @@ -/// The main Lyricli interface -public class Lyricli { - public static var version = "0.0.0-feature/option-parsing" - - public static func printLyrics() { - - let sourceManager = SourceManager() - - if let currentTrack = sourceManager.currentTrack { - print(currentTrack.artist) - print(currentTrack.name) - } - else { - print("Current track not found") - } - } - - public static func printTitle() { - print("Getting Song Title: Not yet implemented") - } - - public static func printSources() { - print("Listing Sources: Not yet implemented") - } - - public static func enableSource(_ sourceName: String) { - print("Enable source \(sourceName): Not yet implemented") - } - - public static func disableSource(_ sourceName: String) { - print("Disable source \(sourceName): Not yet implemented") - } - - public static func resetSource(_ sourceName: String) { - print("Reset source \(sourceName): Not yet implemented") - } -} diff --git a/Sources/lor.swift b/Sources/lor.swift new file mode 100644 index 0000000..d55b500 --- /dev/null +++ b/Sources/lor.swift @@ -0,0 +1,37 @@ +/// The main Lyricli interface +public class Lyricli { + public static var version = "0.0.0-feature/option-parsing" + + public static func printLyrics() { + + let sourceManager = SourceManager() + + if let currentTrack = sourceManager.currentTrack { + print(currentTrack.artist) + print(currentTrack.name) + } + else { + print("Current track not found") + } + } + + public static func printTitle() { + print("Getting Song Title: Not yet implemented") + } + + public static func printSources() { + print("Listing Sources: Not yet implemented") + } + + public static func enableSource(_ sourceName: String) { + print("Enable source \(sourceName): Not yet implemented") + } + + public static func disableSource(_ sourceName: String) { + print("Disable source \(sourceName): Not yet implemented") + } + + public static func resetSource(_ sourceName: String) { + print("Reset source \(sourceName): Not yet implemented") + } +} -- cgit From cadf28c5831b225abf9da650127659c0b0bf6e39 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Wed, 17 May 2017 23:09:32 -0500 Subject: It's lowercase now! --- Sources/lor.swift | 37 ------------------------------------- Sources/lyricli.swift | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 Sources/lor.swift create mode 100644 Sources/lyricli.swift (limited to 'Sources') diff --git a/Sources/lor.swift b/Sources/lor.swift deleted file mode 100644 index d55b500..0000000 --- a/Sources/lor.swift +++ /dev/null @@ -1,37 +0,0 @@ -/// The main Lyricli interface -public class Lyricli { - public static var version = "0.0.0-feature/option-parsing" - - public static func printLyrics() { - - let sourceManager = SourceManager() - - if let currentTrack = sourceManager.currentTrack { - print(currentTrack.artist) - print(currentTrack.name) - } - else { - print("Current track not found") - } - } - - public static func printTitle() { - print("Getting Song Title: Not yet implemented") - } - - public static func printSources() { - print("Listing Sources: Not yet implemented") - } - - public static func enableSource(_ sourceName: String) { - print("Enable source \(sourceName): Not yet implemented") - } - - public static func disableSource(_ sourceName: String) { - print("Disable source \(sourceName): Not yet implemented") - } - - public static func resetSource(_ sourceName: String) { - print("Reset source \(sourceName): Not yet implemented") - } -} diff --git a/Sources/lyricli.swift b/Sources/lyricli.swift new file mode 100644 index 0000000..d55b500 --- /dev/null +++ b/Sources/lyricli.swift @@ -0,0 +1,37 @@ +/// The main Lyricli interface +public class Lyricli { + public static var version = "0.0.0-feature/option-parsing" + + public static func printLyrics() { + + let sourceManager = SourceManager() + + if let currentTrack = sourceManager.currentTrack { + print(currentTrack.artist) + print(currentTrack.name) + } + else { + print("Current track not found") + } + } + + public static func printTitle() { + print("Getting Song Title: Not yet implemented") + } + + public static func printSources() { + print("Listing Sources: Not yet implemented") + } + + public static func enableSource(_ sourceName: String) { + print("Enable source \(sourceName): Not yet implemented") + } + + public static func disableSource(_ sourceName: String) { + print("Disable source \(sourceName): Not yet implemented") + } + + public static func resetSource(_ sourceName: String) { + print("Reset source \(sourceName): Not yet implemented") + } +} -- cgit From a968bff7e21c9372deca5fce5921999b45d5d348 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Wed, 17 May 2017 23:23:18 -0500 Subject: Simulate lyrics engine --- Sources/lyricli.swift | 28 +++++++++++++++++++++------- Sources/lyrics_engine.swift | 20 ++++++++++++++++++++ Sources/main.swift | 2 +- Sources/source_manager.swift | 2 +- Sources/track.swift | 6 +++--- 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 Sources/lyrics_engine.swift (limited to 'Sources') diff --git a/Sources/lyricli.swift b/Sources/lyricli.swift index d55b500..c44a7f4 100644 --- a/Sources/lyricli.swift +++ b/Sources/lyricli.swift @@ -2,23 +2,33 @@ public class Lyricli { public static var version = "0.0.0-feature/option-parsing" + public static var showTitle = false + public static func printLyrics() { let sourceManager = SourceManager() if let currentTrack = sourceManager.currentTrack { - print(currentTrack.artist) - print(currentTrack.name) + + let engine = LyricsEngine(withTrack: currentTrack) + + if let lyrics = engine.lyrics { + if showTitle { + printTitle(currentTrack) + } + + print(lyrics) + } + else { + print("Lyrics not found :(") + } + } else { - print("Current track not found") + print("No Artist/Song could be found :(") } } - public static func printTitle() { - print("Getting Song Title: Not yet implemented") - } - public static func printSources() { print("Listing Sources: Not yet implemented") } @@ -34,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 index 0000000..661ce86 --- /dev/null +++ b/Sources/lyrics_engine.swift @@ -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 + } +} diff --git a/Sources/main.swift b/Sources/main.swift index 0ae48bc..e4b2760 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -80,7 +80,7 @@ func main() { if let titleFlag = flags["title"] as? BoolOption { if titleFlag.value == true { - Lyricli.printTitle() + Lyricli.showTitle = true } } diff --git a/Sources/source_manager.swift b/Sources/source_manager.swift index d0af817..dd956ad 100644 --- a/Sources/source_manager.swift +++ b/Sources/source_manager.swift @@ -1,5 +1,5 @@ /// Manages the different sources. Keeps track of them, lists and toggles -public class SourceManager { +class SourceManager { private var availableSources: [String: Source] = [ "arguments": ArgumentsSource() diff --git a/Sources/track.swift b/Sources/track.swift index efc09f4..d2a9047 100644 --- a/Sources/track.swift +++ b/Sources/track.swift @@ -1,7 +1,7 @@ /// Contains the artist and name of a track -public class Track { - public let name: String - public let artist: String +class Track { + let name: String + let artist: String init(withName trackName: String, andArtist trackArtist: String) { -- cgit