Module: Muzak::Cmd
- Included in:
- Instance
- Defined in:
- lib/muzak/cmd.rb,
lib/muzak/cmd/meta.rb,
lib/muzak/cmd/index.rb,
lib/muzak/cmd/config.rb,
lib/muzak/cmd/player.rb,
lib/muzak/cmd/playlist.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_index_available? ⇒ Boolean
- #_index_loaded? ⇒ Boolean
- #_index_outdated? ⇒ Boolean
- #_index_sync ⇒ Object
- #_playlists_loaded? ⇒ Boolean
- #albums_by_artist(*args) ⇒ Object
- #clear_queue ⇒ Object
- #config_get(*args) ⇒ Object
- #enqueue_album(*args) ⇒ Object
- #enqueue_artist(*args) ⇒ Object
- #enqueue_playlist(*args) ⇒ Object
- #help(*args) ⇒ Object
- #index_build(*args) ⇒ Object
- #index_load ⇒ Object
- #jukebox(*args) ⇒ Object
- #list_albums(*args) ⇒ Object
- #list_artists(*args) ⇒ Object
- #list_playlists(*args) ⇒ Object
- #list_plugins ⇒ Object
- #list_queue ⇒ Object
- #next ⇒ Object
- #now_playing ⇒ Object
- #pause ⇒ Object
- #play ⇒ Object
- #player_activate ⇒ Object
- #player_deactivate ⇒ Object
- #playlist_add_album(*args) ⇒ Object
- #playlist_add_artist(*args) ⇒ Object
- #playlist_add_current(*args) ⇒ Object
- #playlist_del_current(*args) ⇒ Object
- #playlist_delete(*args) ⇒ Object
- #playlist_shuffle(*args) ⇒ Object
- #playlists_load(*args) ⇒ Object
- #previous ⇒ Object
- #quit ⇒ Object
- #shuffle_queue ⇒ Object
- #songs_by_artist(*args) ⇒ Object
- #toggle ⇒ Object
Class Method Details
.commands ⇒ Object
8 9 10 11 |
# File 'lib/muzak/cmd.rb', line 8 def self.commands commands = instance_methods.map(&:to_s).reject { |m| m.start_with?("_") } commands.map { |c| Utils.resolve_method c } end |
Instance Method Details
#_index_available? ⇒ Boolean
3 4 5 |
# File 'lib/muzak/cmd/index.rb', line 3 def _index_available? File.file?(INDEX_FILE) end |
#_index_loaded? ⇒ Boolean
7 8 9 |
# File 'lib/muzak/cmd/index.rb', line 7 def _index_loaded? !!@index end |
#_index_outdated? ⇒ Boolean
11 12 13 |
# File 'lib/muzak/cmd/index.rb', line 11 def _index_outdated? Time.now.to_i - @index. >= Config.index_autobuild end |
#_index_sync ⇒ Object
15 16 17 18 |
# File 'lib/muzak/cmd/index.rb', line 15 def _index_sync debug "syncing index hash with #{INDEX_FILE}" File.open(INDEX_FILE, "w") { |io| io.write @index.hash.to_yaml } end |
#_playlists_loaded? ⇒ Boolean
3 4 5 |
# File 'lib/muzak/cmd/playlist.rb', line 3 def _playlists_loaded? !!@playlists end |
#albums_by_artist(*args) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/muzak/cmd/index.rb', line 58 def albums_by_artist(*args) return unless _index_loaded? artist = args.join(" ") return if artist.nil? puts @index.albums_by(artist).map(&:title) end |
#clear_queue ⇒ Object
83 84 85 |
# File 'lib/muzak/cmd/player.rb', line 83 def clear_queue @player.clear_queue end |
#config_get(*args) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/muzak/cmd/config.rb', line 5 def config_get(*args) fail_arity(args, 1) key = args.shift return if key.nil? info "#{key}: #{Config.send Utils.resolve_method(key)}" end |
#enqueue_album(*args) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/muzak/cmd/player.rb', line 55 def enqueue_album(*args) album_name = args.join(" ") return if album_name.nil? debug album_name album = @index.albums[album_name] debug album.to_s return if album.nil? @player.enqueue_album album end |
#enqueue_artist(*args) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/muzak/cmd/player.rb', line 43 def enqueue_artist(*args) artist = args.join(" ") return if artist.nil? albums = @index.albums_by(artist) return if albums.empty? albums.each do |album| @player.enqueue_album album end end |
#enqueue_playlist(*args) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/muzak/cmd/playlist.rb', line 37 def enqueue_playlist(*args) return unless _playlists_loaded? fail_arity(args, 1) pname = args.shift @player.enqueue_playlist(@playlists[pname]) event :playlist_enqueued, @playlists[pname] end |
#help(*args) ⇒ Object
3 4 5 6 |
# File 'lib/muzak/cmd/meta.rb', line 3 def help(*args) commands = Muzak::Cmd.commands.join(", ") info "available commands: #{commands}" end |
#index_build(*args) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/muzak/cmd/index.rb', line 33 def index_build(*args) warn_arity(args, 0) verbose "building a new index, this may take a while" @index = Index.new(Config.music, deep: Config.deep_index) _index_sync end |
#index_load ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/muzak/cmd/index.rb', line 20 def index_load verbose "loading index from #{INDEX_FILE}" @index = Index.load_index(INDEX_FILE) # the order is important here, since Config.index_autobuild # will short-circuit if index-autobuild isn't set if Config.index_autobuild && _index_outdated? verbose "rebuilding outdated index" index_build end end |
#jukebox(*args) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/muzak/cmd/player.rb', line 67 def jukebox(*args) count = args.shift || Config.jukebox_size songs = @index.jukebox(count.to_i) songs.each { |s| @player.enqueue_song s } end |
#list_albums(*args) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/muzak/cmd/index.rb', line 50 def list_albums(*args) return unless _index_loaded? warn_arity(args, 0) puts @index.album_names.join("\n") end |
#list_artists(*args) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/muzak/cmd/index.rb', line 42 def list_artists(*args) return unless _index_loaded? warn_arity(args, 0) puts @index.artists.join("\n") end |
#list_playlists(*args) ⇒ Object
7 8 9 10 11 |
# File 'lib/muzak/cmd/playlist.rb', line 7 def list_playlists(*args) Playlist.playlist_names.each do |playlist| info playlist end end |
#list_plugins ⇒ Object
8 9 10 11 |
# File 'lib/muzak/cmd/meta.rb', line 8 def list_plugins plugins = Plugin.plugin_names.join(", ") puts "available plugins: #{plugins}" end |
#list_queue ⇒ Object
75 76 77 |
# File 'lib/muzak/cmd/player.rb', line 75 def list_queue puts @player.list_queue.map(&:title) end |
#next ⇒ Object
35 36 37 |
# File 'lib/muzak/cmd/player.rb', line 35 def next @player.next_song end |
#now_playing ⇒ Object
87 88 89 90 91 |
# File 'lib/muzak/cmd/player.rb', line 87 def return unless @player. info @player..full_title end |
#pause ⇒ Object
23 24 25 |
# File 'lib/muzak/cmd/player.rb', line 23 def pause @player.pause end |
#play ⇒ Object
19 20 21 |
# File 'lib/muzak/cmd/player.rb', line 19 def play @player.play end |
#player_activate ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/muzak/cmd/player.rb', line 3 def player_activate if @player.running? warn "player is already running" return end @player.activate! end |
#player_deactivate ⇒ Object
12 13 14 15 16 17 |
# File 'lib/muzak/cmd/player.rb', line 12 def player_deactivate warn "player is not running" unless @player.running? # do cleanup even if the player isn't running, just in case @player.deactivate! end |
#playlist_add_album(*args) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/muzak/cmd/playlist.rb', line 46 def playlist_add_album(*args) return unless _playlists_loaded? pname = args.shift return if pname.nil? album_name = args.join(" ") return if album_name.nil? album = @index.albums[album_name] return if album.nil? @playlists[pname].add(album.songs) end |
#playlist_add_artist(*args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/muzak/cmd/playlist.rb', line 61 def playlist_add_artist(*args) return unless _playlists_loaded? pname = args.shift return if pname.nil? artist = args.join(" ") return if artist.nil? @playlists[pname].add(@index.songs_by(artist)) end |
#playlist_add_current(*args) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/muzak/cmd/playlist.rb', line 73 def playlist_add_current(*args) return unless @player.running? && _playlists_loaded? pname = args.shift return if pname.nil? @playlists[pname].add @player. end |
#playlist_del_current(*args) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/muzak/cmd/playlist.rb', line 82 def playlist_del_current(*args) return unless @player.running? && _playlists_loaded? pname = args.shift return if pname.nil? @playlists[pname].delete @player. end |
#playlist_delete(*args) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/muzak/cmd/playlist.rb', line 27 def playlist_delete(*args) fail_arity(args, 1) pname = args.shift debug "deleting playist '#{pname}'" Playlist.delete!(pname) @playlist[pname] = nil end |
#playlist_shuffle(*args) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/muzak/cmd/playlist.rb', line 91 def playlist_shuffle(*args) return unless _playlists_loaded? pname = args.shift return if pname.nil? @playlists[pname].shuffle! end |
#playlists_load(*args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/muzak/cmd/playlist.rb', line 13 def playlists_load(*args) # fail_arity(args, 1) # pname = args.shift @playlists = {} @playlists.default_proc = proc { |h, k| h[k] = Playlist.new(k) } Playlist.playlist_names.each do |pname| debug "loading playlist '#{pname}'" @playlists[pname] = Playlist.new(pname) end event :playlists_loaded, @playlist end |
#previous ⇒ Object
39 40 41 |
# File 'lib/muzak/cmd/player.rb', line 39 def previous @player.previous_song end |
#quit ⇒ Object
13 14 15 16 |
# File 'lib/muzak/cmd/meta.rb', line 13 def quit debug "muzak is quitting..." @player.deactivate! end |
#shuffle_queue ⇒ Object
79 80 81 |
# File 'lib/muzak/cmd/player.rb', line 79 def shuffle_queue @player.shuffle_queue end |
#songs_by_artist(*args) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/muzak/cmd/index.rb', line 67 def songs_by_artist(*args) return unless _index_loaded? artist = args.join(" ") return if artist.nil? puts @index.songs_by(artist).map(&:title) end |
#toggle ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/muzak/cmd/player.rb', line 27 def toggle if @player. @player.pause else @player.play end end |