From: Ben Beltran Date: Sun, 14 Apr 2019 14:08:54 +0000 (+0200) Subject: Update swift files to use Bariloche X-Git-Tag: 1.0.0^2^2~24 X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli/commitdiff_plain/fdafe0d4012af00e0d9cb613a0146924b8fd8eaf?hp=38d5d6de414ad69d6a7dc744e4aed39c488ba30f Update swift files to use Bariloche --- diff --git a/Sources/arguments_source.swift b/Sources/arguments_source.swift deleted file mode 100644 index 9615318..0000000 --- a/Sources/arguments_source.swift +++ /dev/null @@ -1,18 +0,0 @@ -// Source that reads track artist and name from the command line -class ArgumentsSource: Source { - - // Returns a track based on the arguments. It assumes the track artist - // will be the first argument, and the name will be the second, excluding - // any flags. - var currentTrack: Track? { - - 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/lyricli/configuration.swift similarity index 97% rename from Sources/configuration.swift rename to Sources/lyricli/configuration.swift index 05e2802..1b01034 100644 --- a/Sources/configuration.swift +++ b/Sources/lyricli/configuration.swift @@ -8,7 +8,7 @@ class Configuration { // Default options, will be automatically written to the global config if // not found. private var configuration: [String: Any] = [ - "enabled_sources": ["arguments", "itunes", "spotify"] + "enabled_sources": ["itunes", "spotify"] ] // The shared instance of the object diff --git a/Sources/lyricli.swift b/Sources/lyricli/lyricli.swift similarity index 77% rename from Sources/lyricli.swift rename to Sources/lyricli/lyricli.swift index 8aeb5c0..c4136fb 100644 --- a/Sources/lyricli.swift +++ b/Sources/lyricli/lyricli.swift @@ -2,7 +2,7 @@ class Lyricli { // Version of the application - static var version = "0.3.0" + static var version = "0.4.0" // Flag that controls whether we should show the track artist and name before // the lyrics @@ -15,20 +15,24 @@ class Lyricli { let sourceManager = SourceManager() if let currentTrack = sourceManager.currentTrack { - let engine = LyricsEngine(withTrack: currentTrack) + printLyrics(currentTrack) + } else { + print("No Artist/Song could be found :(") + } + } - if let lyrics = engine.lyrics { - if showTitle { - printTitle(currentTrack) - } + // fetches the lyrics from an engine and prints them + static func printLyrics(_ currentTrack: Track) { + let engine = LyricsEngine(withTrack: currentTrack) - print(lyrics) - } else { - print("Lyrics not found :(") + if let lyrics = engine.lyrics { + if showTitle { + printTitle(currentTrack) } + print(lyrics) } else { - print("No Artist/Song could be found :(") + print("Lyrics not found :(") } } diff --git a/Sources/lyricli/lyricli_command.swift b/Sources/lyricli/lyricli_command.swift new file mode 100644 index 0000000..28ad44a --- /dev/null +++ b/Sources/lyricli/lyricli_command.swift @@ -0,0 +1,38 @@ +import Bariloche + +class LyricliCommand: Command { + let usage: String? = "Fetch the lyrics for current playing track or the one specified via arguments" + + // Flags + let version = Flag(short: "v", long: "version", help: "Prints the version.") + let showTitle = Flag(short: "t", long: "title", help: "Shows title of song if true") + let listSources = Flag(short: "l", long: "listSources", help: "Lists all sources") + + // Named Arguments + let enableSource = Argument(name: "source", + kind: .named(short: "e", long: "enableSource"), + optional: true, + help: "Enables a source") + let disableSource = Argument(name: "source", + kind: .named(short: "d", long: "disableSource"), + optional: true, + help: "Disables a source") + let resetSource = Argument(name: "source", + kind: .named(short: "r", long: "resetSource"), + optional: true, + help: "Resets a source") + + // Positional Arguments + let artist = Argument(name: "artist", + kind: .positional, + optional: true, + help: "The name of the artist") + let trackName = Argument(name: "trackName", + kind: .positional, + optional: true, + help: "The name of the track") + + func run() -> Bool { + return true + } +} diff --git a/Sources/lyrics_engine.swift b/Sources/lyricli/lyrics_engine.swift similarity index 98% rename from Sources/lyrics_engine.swift rename to Sources/lyricli/lyrics_engine.swift index 27e0e11..085a61c 100644 --- a/Sources/lyrics_engine.swift +++ b/Sources/lyricli/lyrics_engine.swift @@ -119,12 +119,12 @@ class LyricsEngine { // Look for the lyrics lightbox if let regex = try? NSRegularExpression(pattern: lyricsMatcher) { - let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.characters.count)) + let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.count)) for match in matches { let nsBody = body as NSString - let range = match.rangeAt(1) + let range = match.range(at: 1) let encodedLyrics = nsBody.substring(with: range) let decodedLyrics = decodeLyrics(encodedLyrics) diff --git a/Sources/lyricli/main.swift b/Sources/lyricli/main.swift new file mode 100644 index 0000000..be1c933 --- /dev/null +++ b/Sources/lyricli/main.swift @@ -0,0 +1,107 @@ +import Foundation +import Bariloche + +// Entry point of the application. This is the main executable +private func main() { + + // Bariloche assumes at least one argument, so bypass + // if that's the case. + if CommandLine.arguments.count > 1 { + let parser = Bariloche(command: LyricliCommand()) + let result = parser.parse() + + if result.count == 0 { + exit(EX_USAGE) + } + + if let lyricliCommand = result[0] as? LyricliCommand { + // Flags + checkVersionFlag(lyricliCommand) + checkListSourcesFlag(lyricliCommand) + checkTitleFlag(lyricliCommand) + + // String Options + + checkEnableSourceFlag(lyricliCommand) + checkDisableSourceFlag(lyricliCommand) + checkResetSourceFlag(lyricliCommand) + + checkPositionalArguments(lyricliCommand) + + } + } + + // Run Lyricli + Lyricli.printLyrics() +} + +// Handle the version flag + +private func checkVersionFlag(_ command: LyricliCommand) { + if command.version.value { + print(Lyricli.version) + exit(0) + } +} + +// Handle the list sources flag + +private func checkListSourcesFlag(_ command: LyricliCommand) { + if command.listSources.value { + Lyricli.printSources() + exit(0) + } +} + +// Handle the title flag + +private func checkTitleFlag(_ command: LyricliCommand) { + Lyricli.showTitle = command.showTitle.value +} + +// Handle the enable source flag + +private func checkEnableSourceFlag(_ command: LyricliCommand) { + if let source = command.enableSource.value { + Lyricli.enableSource(source) + exit(0) + } +} + +// Handle the disable source flag + +private func checkDisableSourceFlag(_ command: LyricliCommand) { + if let source = command.disableSource.value { + Lyricli.disableSource(source) + exit(0) + } +} + +// Handle the reset source flag + +private func checkResetSourceFlag(_ command: LyricliCommand) { + if let source = command.resetSource.value { + Lyricli.resetSource(source) + exit(0) + } +} + +// Handle the positional arguments + +private func checkPositionalArguments(_ command: LyricliCommand) { + if let artist = command.artist.value { + + let currentTrack: Track + + if let trackName = command.trackName.value { + currentTrack = Track(withName: trackName, andArtist: artist) + } else { + currentTrack = Track(withName: "", andArtist: artist) + } + + Lyricli.printLyrics(currentTrack) + exit(0) + } +} + +main() diff --git a/Sources/source_manager.swift b/Sources/lyricli/source_manager.swift similarity index 97% rename from Sources/source_manager.swift rename to Sources/lyricli/source_manager.swift index e6c42da..2f0b8f4 100644 --- a/Sources/source_manager.swift +++ b/Sources/lyricli/source_manager.swift @@ -3,7 +3,6 @@ class SourceManager { // List of sources enabled for the crurent platform private var availableSources: [String: Source] = [ - "arguments": ArgumentsSource(), "itunes": ItunesSource(), "spotify": SpotifySource() ] diff --git a/Sources/itunes_source.swift b/Sources/lyricli/sources/itunes_source.swift similarity index 89% rename from Sources/itunes_source.swift rename to Sources/lyricli/sources/itunes_source.swift index 6f4aef9..4e175c1 100644 --- a/Sources/itunes_source.swift +++ b/Sources/lyricli/sources/itunes_source.swift @@ -12,7 +12,7 @@ import ScriptingBridge @objc optional var currentStreamTitle: String? {get} } -extension SBApplication : iTunesApplication {} +extension SBApplication: iTunesApplication {} // Source that reads track artist and name from current itunes track class ItunesSource: Source { @@ -26,7 +26,7 @@ class ItunesSource: Source { if let currentStreamTitle = iTunes.currentStreamTitle { if let track = currentStreamTitle { - let trackComponents = track.characters.split(separator: "-").map(String.init) + let trackComponents = track.split(separator: "-").map(String.init) if trackComponents.count == 2 { let artist = trackComponents[0].trimmingCharacters(in: .whitespaces) @@ -45,7 +45,7 @@ class ItunesSource: Source { if let artist = track.artist { // track properties are empty strings if itunes is closed - if (!(name != "" && artist != "")) { + if name == "" || artist == "" { return nil } return Track(withName: name, andArtist: artist) diff --git a/Sources/source_protocol.swift b/Sources/lyricli/sources/source_protocol.swift similarity index 100% rename from Sources/source_protocol.swift rename to Sources/lyricli/sources/source_protocol.swift diff --git a/Sources/spotify_source.swift b/Sources/lyricli/sources/spotify_source.swift similarity index 100% rename from Sources/spotify_source.swift rename to Sources/lyricli/sources/spotify_source.swift diff --git a/Sources/track.swift b/Sources/lyricli/track.swift similarity index 100% rename from Sources/track.swift rename to Sources/lyricli/track.swift diff --git a/Sources/main.swift b/Sources/main.swift deleted file mode 100644 index 9d46e92..0000000 --- a/Sources/main.swift +++ /dev/null @@ -1,151 +0,0 @@ -import CommandLineKit -import Foundation - -// Entry point of the application. This is the main executable -private func main() { - let (flags, parser) = createParser() - - do { - try parser.parse() - } catch { - parser.printUsage(error) - exit(EX_USAGE) - } - - // Boolean Options - - checkHelpFlag(flags["help"], withParser: parser) - checkVersionFlag(flags["version"], withParser: parser) - checkListSourcesFlag(flags["listSources"], withParser: parser) - checkTitleFlag(flags["title"], withParser: parser) - - // String Options - - checkEnableSourceFlag(flags["enableSource"], withParser: parser) - checkDisableSourceFlag(flags["disableSource"], withParser: parser) - checkResetSourceFlag(flags["resetSource"], withParser: parser) - - // Remove any flags so anyone after this gets the unprocessed values - - let programName: [String] = [CommandLine.arguments[0]] - CommandLine.arguments = programName + parser.unparsedArguments - - // Run Lyricli - - Lyricli.printLyrics() -} - -/// Sets up and returns a new options parser -/// -/// - Returns: A Dictionary of Options, and a new CommandLineKit instance -private func createParser() -> ([String:Option], CommandLineKit) { - let parser = CommandLineKit() - var flags: [String:Option] = [:] - - flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.") - flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.") - - flags["enableSource"] = StringOption(shortFlag: "e", longFlag: "enable-source", helpMessage: "Enables a source") - flags["disableSource"] = StringOption(shortFlag: "d", longFlag: "disable-source", helpMessage: "Disables a source") - flags["resetSource"] = StringOption(shortFlag: "r", longFlag: "reset-source", helpMessage: "Resets a source") - flags["listSources"] = BoolOption(shortFlag: "l", longFlag: "list-sources", helpMessage: "Lists all sources") - - flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true") - - parser.addOptions(Array(flags.values)) - - parser.formatOutput = {parseString, type in - - var formattedString: String - - switch type { - case .About: - formattedString = "\(parseString) [ ]" - break - default: - formattedString = parseString - } - - return parser.defaultFormat(formattedString, type: type) - } - - return (flags, parser) -} - -// Handle the Help flag - -private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let helpFlag = flag as? BoolOption { - if helpFlag.value { - parser.printUsage() - exit(0) - } - } -} - -// Handle the version flag - -private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let versionFlag = flag as? BoolOption { - if versionFlag.value { - print(Lyricli.version) - exit(0) - } - } -} - -// Handle the list sources flag - -private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let listSourcesFlag = flag as? BoolOption { - if listSourcesFlag.value { - Lyricli.printSources() - exit(0) - } - } -} - -// Handle the title flag - -private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let titleFlag = flag as? BoolOption { - if titleFlag.value { - Lyricli.showTitle = true - } - } -} - -// Handle the enable source flag - -private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let enableSourceFlag = flag as? StringOption { - if let source = enableSourceFlag.value { - Lyricli.enableSource(source) - exit(0) - } - } -} - -// Handle the disable source flag - -private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let disableSourceFlag = flag as? StringOption { - if let source = disableSourceFlag.value { - Lyricli.disableSource(source) - exit(0) - } - } -} - -// Handle the reset source flag - -private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { - if let resetSourceFlag = flag as? StringOption { - if let source = resetSourceFlag.value { - Lyricli.resetSource(source) - exit(0) - } - } -} - -main()