-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_jukebox.rb
More file actions
executable file
·137 lines (104 loc) · 3.15 KB
/
console_jukebox.rb
File metadata and controls
executable file
·137 lines (104 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby
require "formatador"
require "highline/import"
require "yaml"
require "./lib/app"
###########################
# Some setup...
###########################
SONGS_FILE_PATH = "./data/songs.yml"
raw_songs_collection = YAML.load(File.open(SONGS_FILE_PATH) )
songs = raw_songs_collection.map { |attributes| Song.build_from_hash(attributes) }
jukebox = Jukebox.new(songs)
###########################
# Utility methods
###########################
def safe_operation
begin
yield
rescue RuntimeError => error
say("<%= color( \"Oops! #{error.message}\", :on_red) %>")
end
end
def print_custom_header(jukebox)
@songs_table ||= jukebox.library.as_hash
Formatador.display_table(@songs_table, ["id", "name", "artist", "album", "price"])
say("\tCREDIT: #{jukebox.credit} €")
end
def print_playing_song(song, jukebox)
say("\n")
say("<%= color( \"Added song: #{song.name} by #{song.artist}\", :green) %>")
say("\n")
print_now_playing(jukebox)
end
def print_now_playing(jukebox)
unless jukebox.now_playing.nil?
say("\n")
say("<%= color( \"Now playing... #{jukebox.now_playing.name} by #{jukebox.now_playing.artist}\", :yellow) %>")
say("\n")
end
end
###########################
# Jukebox CLI starts here
###########################
loop do
print_custom_header(jukebox)
choose do |menu|
menu.prompt = "Select an option:"
menu.layout = '<%= "\n" %>' +
"<%= list( @menu, #{@flow.inspect}, #{@list_option.inspect} ) %>" +
'<%= "\n" %>' +
'<%= @prompt %>'
menu.select_by = :index
menu.shell = true
menu.choice("Add money") do
money = ask("How much money? ", Float)
safe_operation { jukebox.add_money(money) }
end
menu.choice("Now playing") do
print_now_playing(jukebox)
end
menu.choice("Play a song selected by ID") do
song_id = ask("What song do you want to play?", String)
safe_operation do
song = jukebox.play_selected_song_by_id(song_id)
print_playing_song(song, jukebox)
end
end
menu.choice("Play random song by artist") do
artist = ask("What artist? ", String)
safe_operation do
song = jukebox.play_random_song_by_artist(artist)
print_playing_song(song, jukebox)
end
end
menu.choice("Play random song by genre") do
genre = ask("What genre? ", String)
safe_operation do
song = jukebox.play_random_song_by_genre(genre)
print_playing_song(song, jukebox)
end
end
menu.choice("Play random song by year") do
year = ask("What year? ", Integer)
safe_operation do
song = jukebox.play_random_song_by_year(year)
print_playing_song(song, jukebox)
end
end
menu.choice("Play random song") do
safe_operation do
song = jukebox.play_random_song
print_playing_song(song, jukebox)
end
end
menu.choice("Exit program") do
say("Bye!")
exit
end
menu.choice("<%= color( \"Maintenance menu\", :white, :on_white) %>") do
command = ask("What do you wanna do?", String)
eval(command)
end
end
end