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
- #_config_available? ⇒ Boolean
- #_config_init ⇒ Object
- #_config_loaded? ⇒ Boolean
- #_config_plugin?(name) ⇒ Boolean
- #_config_sync ⇒ Object
- #_index_available? ⇒ Boolean
- #_index_loaded? ⇒ Boolean
- #_index_outdated? ⇒ Boolean
- #_index_sync ⇒ Object
- #_playlist_available?(pname) ⇒ Boolean
- #_playlist_file(pname) ⇒ Object
- #_playlist_loaded? ⇒ Boolean
- #albums_by_artist(*args) ⇒ Object
- #clear_queue ⇒ Object
- #config_get(*args) ⇒ Object
- #config_load ⇒ Object
- #enqueue_album(*args) ⇒ Object
- #enqueue_artist(*args) ⇒ Object
- #enqueue_playlist ⇒ Object
- #help(*args) ⇒ Object
- #index_build(*args) ⇒ Object
- #index_load ⇒ Object
- #list_albums(*args) ⇒ Object
- #list_artists(*args) ⇒ Object
- #list_playlists ⇒ 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_current ⇒ Object
- #playlist_del_current ⇒ Object
- #playlist_delete(*args) ⇒ Object
- #playlist_load(*args) ⇒ Object
- #playlist_shuffle ⇒ Object
- #playlist_sync(*args) ⇒ Object
- #previous ⇒ Object
- #quit ⇒ Object
- #shuffle_queue ⇒ Object
- #songs_by_artist(*args) ⇒ Object
Class Method Details
.commands ⇒ Object
13 14 15 16 |
# File 'lib/muzak/cmd.rb', line 13 def self.commands commands = instance_methods.map(&:to_s).reject { |m| m.start_with?("_") } commands.map { |c| c.tr "_", "-" } end |
.resolve_command(cmd) ⇒ Object
5 6 7 |
# File 'lib/muzak/cmd.rb', line 5 def self.resolve_command(cmd) cmd.tr "-", "_" end |
.resolve_method(meth) ⇒ Object
9 10 11 |
# File 'lib/muzak/cmd.rb', line 9 def self.resolve_method(meth) meth.to_s.tr "_", "-" end |
Instance Method Details
#_config_available? ⇒ Boolean
5 6 7 |
# File 'lib/muzak/cmd/config.rb', line 5 def _config_available? File.file?(CONFIG_FILE) end |
#_config_init ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/muzak/cmd/config.rb', line 18 def _config_init debug "creating a config file in #{CONFIG_FILE}" @config = { "music" => File.("~/music"), "player" => "mpv", "index-autobuild" => 86400 } Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR) _config_sync end |
#_config_loaded? ⇒ Boolean
9 10 11 |
# File 'lib/muzak/cmd/config.rb', line 9 def _config_loaded? !!@config end |
#_config_plugin?(name) ⇒ Boolean
31 32 33 |
# File 'lib/muzak/cmd/config.rb', line 31 def _config_plugin?(name) @config.key?("plugin-#{name}") end |
#_config_sync ⇒ Object
13 14 15 16 |
# File 'lib/muzak/cmd/config.rb', line 13 def _config_sync debug "syncing config hash with #{CONFIG_FILE}" File.open(CONFIG_FILE, "w") { |io| io.write @config.to_yaml } end |
#_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["timestamp"] >= @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 |
#_playlist_available?(pname) ⇒ Boolean
7 8 9 |
# File 'lib/muzak/cmd/playlist.rb', line 7 def _playlist_available?(pname) File.exist?(_playlist_file(pname)) end |
#_playlist_file(pname) ⇒ Object
3 4 5 |
# File 'lib/muzak/cmd/playlist.rb', line 3 def _playlist_file(pname) File.join(PLAYLIST_DIR, pname) + ".yml" end |
#_playlist_loaded? ⇒ Boolean
11 12 13 |
# File 'lib/muzak/cmd/playlist.rb', line 11 def _playlist_loaded? !!@playlist end |
#albums_by_artist(*args) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/muzak/cmd/index.rb', line 56 def albums_by_artist(*args) return unless _index_loaded? artist = args.join(" ") return if artist.nil? puts @index.albums_by(artist).keys end |
#clear_queue ⇒ Object
71 72 73 |
# File 'lib/muzak/cmd/player.rb', line 71 def clear_queue @player.clear_queue end |
#config_get(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/muzak/cmd/config.rb', line 41 def config_get(*args) return unless _config_loaded? fail_arity(args, 1) key = args.shift return if key.nil? info "#{key}: #{@config[key]}" end |
#config_load ⇒ Object
35 36 37 38 39 |
# File 'lib/muzak/cmd/config.rb', line 35 def config_load verbose "loading config from #{CONFIG_FILE}" @config = YAML::load_file(CONFIG_FILE) end |
#enqueue_album(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/muzak/cmd/player.rb', line 51 def enqueue_album(*args) album_name = args.join(" ") return if album_name.nil? album_hash = @index.albums[album_name] return if album_hash.nil? album = Album.new(album_name, album_hash) @player.enqueue_album album end |
#enqueue_artist(*args) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/muzak/cmd/player.rb', line 35 def enqueue_artist(*args) artist = args.join(" ") return if artist.nil? album_hashes = @index.albums_by(artist) return if album_hashes.empty? albums = album_hashes.map do |album_name, album_hash| Album.new(album_name, album_hash) end albums.each do |album| @player.enqueue_album album end end |
#enqueue_playlist ⇒ Object
57 58 59 60 61 62 |
# File 'lib/muzak/cmd/playlist.rb', line 57 def enqueue_playlist return unless _playlist_loaded? @player.enqueue_playlist(@playlist) event :playlist_enqueued, @playlist 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 |
# File 'lib/muzak/cmd/index.rb', line 33 def index_build(*args) warn_arity(args, 0) @index = Index.new(@config["music"]) _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 debug "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 |
#list_albums(*args) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/muzak/cmd/index.rb', line 48 def list_albums(*args) return unless _index_loaded? warn_arity(args, 0) puts @index.album_names.join("\n") end |
#list_artists(*args) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/muzak/cmd/index.rb', line 40 def list_artists(*args) return unless _index_loaded? warn_arity(args, 0) puts @index.artists.join("\n") end |
#list_playlists ⇒ Object
15 16 17 18 19 |
# File 'lib/muzak/cmd/playlist.rb', line 15 def list_playlists 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
63 64 65 |
# File 'lib/muzak/cmd/player.rb', line 63 def list_queue puts @player.list_queue.map(&:title) end |
#next ⇒ Object
27 28 29 |
# File 'lib/muzak/cmd/player.rb', line 27 def next @player.next_song end |
#now_playing ⇒ Object
75 76 77 |
# File 'lib/muzak/cmd/player.rb', line 75 def 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/muzak/cmd/playlist.rb', line 64 def playlist_add_album(*args) return unless _playlist_loaded? album_name = args.join(" ") return if album_name.nil? album_hash = @index.albums[album_name] return if album_hash.nil? album = Album.new(album_name, album_hash) album.songs.each { |song| @playlist.add song } playlist_sync end |
#playlist_add_current ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/muzak/cmd/playlist.rb', line 80 def playlist_add_current return unless @player.running? && _playlist_loaded? @playlist.add @player. playlist_sync end |
#playlist_del_current ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/muzak/cmd/playlist.rb', line 88 def playlist_del_current return unless @player.running? && _playlist_loaded? @playlist.delete(@player.) playlist_sync end |
#playlist_delete(*args) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/muzak/cmd/playlist.rb', line 37 def playlist_delete(*args) fail_arity(args, 1) pname = args.shift debug "deleting playist '#{pname}'" File.delete(_playlist_file(pname)) if _playlist_available?(pname) @playlist = nil end |
#playlist_load(*args) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/muzak/cmd/playlist.rb', line 21 def playlist_load(*args) fail_arity(args, 1) pname = args.shift if _playlist_available?(pname) info "loading playlist '#{pname}'" @playlist = Playlist.load_playlist(_playlist_file(pname)) else info "creating playlist '#{pname}'" @playlist = Playlist.new(pname, []) playlist_sync end event :playlist_loaded, @playlist end |
#playlist_shuffle ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/muzak/cmd/playlist.rb', line 96 def playlist_shuffle return unless _playlist_loaded? @playlist.shuffle! playlist_sync end |
#playlist_sync(*args) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/muzak/cmd/playlist.rb', line 47 def playlist_sync(*args) return unless _playlist_loaded? fail_arity(args, 0) debug "syncing playlist '#{@playlist.name}'" Dir.mkdir(PLAYLIST_DIR) unless Dir.exist?(PLAYLIST_DIR) File.open(_playlist_file(@playlist.name), "w") { |io| io.write @playlist.to_hash.to_yaml } end |
#previous ⇒ Object
31 32 33 |
# File 'lib/muzak/cmd/player.rb', line 31 def previous @player.previous_song end |
#quit ⇒ Object
13 14 15 16 17 |
# File 'lib/muzak/cmd/meta.rb', line 13 def quit debug "muzak is quitting..." @player.deactivate! exit end |
#shuffle_queue ⇒ Object
67 68 69 |
# File 'lib/muzak/cmd/player.rb', line 67 def shuffle_queue @player.shuffle_queue end |
#songs_by_artist(*args) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/muzak/cmd/index.rb', line 65 def songs_by_artist(*args) return unless _index_loaded? artist = args.join(" ") return if artist.nil? puts @index.songs_by(artist) end |