]>
Commit | Line | Data |
---|---|---|
d852b84e BB |
1 | // The main class, handles all the actions that the executable will call |
2 | class Lyricli { | |
0b3e11a8 | 3 | |
d852b84e | 4 | // Version of the application |
4adf8d51 | 5 | static var version = "2.0.1" |
a968bff7 | 6 | |
d852b84e BB |
7 | // Flag that controls whether we should show the track artist and name before |
8 | // the lyrics | |
9 | static var showTitle = false | |
10 | ||
11 | // Obtains the name of the current track from a source, fetches the lyrics | |
12 | // from an engine and prints them | |
13 | static func printLyrics() { | |
4425e900 BB |
14 | |
15 | let sourceManager = SourceManager() | |
16 | ||
17 | if let currentTrack = sourceManager.currentTrack { | |
fdafe0d4 BB |
18 | printLyrics(currentTrack) |
19 | } else { | |
20 | print("No Artist/Song could be found :(") | |
21 | } | |
22 | } | |
a968bff7 | 23 | |
fdafe0d4 BB |
24 | // fetches the lyrics from an engine and prints them |
25 | static func printLyrics(_ currentTrack: Track) { | |
26 | let engine = LyricsEngine(withTrack: currentTrack) | |
a968bff7 | 27 | |
c53df649 RBR |
28 | if showTitle { |
29 | printTitle(currentTrack) | |
30 | } | |
fdafe0d4 | 31 | if let lyrics = engine.lyrics { |
fdafe0d4 | 32 | print(lyrics) |
1263f62c | 33 | } else { |
fdafe0d4 | 34 | print("Lyrics not found :(") |
4425e900 | 35 | } |
0b3e11a8 BB |
36 | } |
37 | ||
d852b84e BB |
38 | // Print the currently available sources |
39 | static func printSources() { | |
c53df649 RBR |
40 | let sourceManager = SourceManager() |
41 | for (sourceName, _) in sourceManager.availableSources { | |
42 | if (Configuration.shared["enabled_sources"] as? [String] ?? []).contains(sourceName) { | |
43 | print("\(sourceName) (enabled)") | |
44 | } else { | |
45 | print(sourceName) | |
46 | } | |
47 | } | |
0b3e11a8 BB |
48 | } |
49 | ||
d852b84e BB |
50 | // Runs the enable method of a source and writes the configuration to set it |
51 | // as enabled | |
c53df649 RBR |
52 | static func enableSource(_ sourceName: String) throws { |
53 | let sourceManager = SourceManager() | |
54 | if let source = sourceManager.availableSources[sourceName] { | |
55 | if let enabledSources = Configuration.shared["enabled_sources"] as? [String] { | |
56 | if source.enable() == false { | |
57 | throw SourceCouldNotBeEnabled() | |
58 | } | |
59 | if !enabledSources.contains(sourceName) { | |
60 | Configuration.shared["enabled_sources"] = enabledSources + [sourceName] | |
61 | } | |
62 | return | |
63 | } | |
64 | throw ConfigurationCouldNotBeRead() | |
65 | } | |
66 | throw SourceNotAvailable() | |
0b3e11a8 BB |
67 | } |
68 | ||
d852b84e | 69 | // Remove a source from the enabled sources configuration |
c53df649 RBR |
70 | static func disableSource(_ sourceName: String) throws { |
71 | let sourceManager = SourceManager() | |
72 | if let source = sourceManager.availableSources[sourceName] { | |
73 | if let enabledSources = Configuration.shared["enabled_sources"] as? [String] { | |
74 | if source.disable() == false { | |
75 | throw SourceCouldNotBeDisabled() | |
76 | } | |
77 | Configuration.shared["enabled_sources"] = enabledSources.filter { $0 != sourceName } | |
78 | return | |
79 | } | |
80 | throw ConfigurationCouldNotBeRead() | |
81 | } | |
82 | throw SourceNotAvailable() | |
0b3e11a8 BB |
83 | } |
84 | ||
d852b84e | 85 | // Removes any configuration for a source, and disables it |
c53df649 RBR |
86 | static func resetSource(_ sourceName: String) throws { |
87 | let sourceManager = SourceManager() | |
88 | if let source = sourceManager.availableSources[sourceName] { | |
89 | if source.reset() == false { | |
90 | throw SourceCouldNotBeReset() | |
91 | } | |
92 | try disableSource(sourceName) | |
93 | return | |
94 | } | |
95 | throw SourceNotAvailable() | |
0b3e11a8 | 96 | } |
a968bff7 | 97 | |
d852b84e | 98 | // Prints the track artist and name |
a968bff7 BB |
99 | private static func printTitle(_ track: Track) { |
100 | print("\(track.artist) - \(track.name)") | |
101 | } | |
194a3581 | 102 | } |