]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/lyricli/lyricli.swift
d5e5b91d5f08687eef022e2083931431477506cb
[rbdr/lyricli] / Sources / lyricli / lyricli.swift
1 // The main class, handles all the actions that the executable will call
2 class Lyricli {
3
4 // Version of the application
5 static var version = "2.0.1"
6
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() {
14
15 let sourceManager = SourceManager()
16
17 if let currentTrack = sourceManager.currentTrack {
18 printLyrics(currentTrack)
19 } else {
20 print("No Artist/Song could be found :(")
21 }
22 }
23
24 // fetches the lyrics from an engine and prints them
25 static func printLyrics(_ currentTrack: Track) {
26 let engine = LyricsEngine(withTrack: currentTrack)
27
28 if showTitle {
29 printTitle(currentTrack)
30 }
31 if let lyrics = engine.lyrics {
32 print(lyrics)
33 } else {
34 print("Lyrics not found :(")
35 }
36 }
37
38 // Print the currently available sources
39 static func printSources() {
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 }
48 }
49
50 // Runs the enable method of a source and writes the configuration to set it
51 // as enabled
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()
67 }
68
69 // Remove a source from the enabled sources configuration
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()
83 }
84
85 // Removes any configuration for a source, and disables it
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()
96 }
97
98 // Prints the track artist and name
99 private static func printTitle(_ track: Track) {
100 print("\(track.artist) - \(track.name)")
101 }
102 }