module Muzak::Cmd

The namespace for all commands exposed by muzak. @see file:COMMANDS.md User Commands

Public Class Methods

commands() click to toggle source

@return [Array<String>] all valid muzak commands

# File lib/muzak/cmd.rb, line 13
def self.commands
  commands = instance_methods.map(&:to_s).reject { |m| m.start_with?("_") }
  commands.map { |c| Config.resolve_method c }
end

Public Instance Methods

albums_by_artist(*args) click to toggle source

List all albums by the given artist in the index. @command `albums-by-artist <artist name>` @cmdexample `muzak> albums-by-artist Your Favorite Artist`

# File lib/muzak/cmd/index.rb, line 49
def albums_by_artist(*args)
  artist = args.join(" ")

  albums = index.albums_by(artist).map(&:title)

  build_response data: {
    albums: albums,
  }
end
clear_queue() click to toggle source

Tell the player to clear its internal queue. @command `clear-queue` @cmdexample `muzak> clear-queue` @note This does not (usually) stop the current song.

# File lib/muzak/cmd/player.rb, line 152
def clear_queue
  player.clear_queue

  build_response
end
config_get(key) click to toggle source

Query the {Muzak::Config} for a given key. @command `config-get <key>` @cmdexample `muzak> config-get player`

# File lib/muzak/cmd/config.rb, line 10
def config_get(key)
  value = Config.send Config.resolve_command(key)

  build_response data: { key => value }
end
dump_index() click to toggle source

Dump a hash representation of the index. @command `dump-index` @cmdexample `muzak> dump-index`

# File lib/muzak/cmd/index.rb, line 22
def dump_index
  build_response data: {
    index: index.hash,
  }
end
enqueue_album(*args) click to toggle source

Tell the player to enqueue the given album. @command `enqueue-album <album name>` @cmdexample `muzak> enqueue-album Your Favorite Album`

# File lib/muzak/cmd/player.rb, line 105
def enqueue_album(*args)
  album_name = args.join(" ")
  album      = index.albums[album_name]

  if album
    player.enqueue_album album
    build_response
  else
    build_response error: "no such album: '#{album_name}'"
  end
end
enqueue_artist(*args) click to toggle source

Tell the player to enqueue all songs by the given artist. @command `enqueue-artist <artist name>` @cmdexample `muzak> enqueue-artist Your Favorite Artist`

# File lib/muzak/cmd/player.rb, line 88
def enqueue_artist(*args)
  artist = args.join(" ")
  albums = index.albums_by(artist)

  if albums.any?
    albums.each do |album|
      player.enqueue_album album
    end
    build_response
  else
    build_response error: "no albums by: '#{artist}'"
  end
end
enqueue_playlist(pname) click to toggle source

Add the given playlist to the player's queue. @command `enqueue-playlist <playlist>` @cmdexample `muzak> enqueue-playlist favorites`

# File lib/muzak/cmd/playlist.rb, line 29
def enqueue_playlist(pname)
  player.enqueue_playlist(playlists[pname])
  event :playlist_enqueued, playlists[pname]

  build_response
end
help(*_args) click to toggle source

Return a “helpful” listing of commands. @command `help` @cmdexample `muzak> help`

# File lib/muzak/cmd/meta.rb, line 20
def help(*_args)
  build_response data: {
    commands: Muzak::Cmd.commands,
  }
end
jukebox(count = Config.jukebox_size) click to toggle source

Tell the player to load the given number of random songs. @command `jukebox [count]` @cmdexample `muzak> jukebox 150`

# File lib/muzak/cmd/player.rb, line 120
def jukebox(count = Config.jukebox_size)
  songs = index.jukebox(count.to_i)

  songs.each { |s| player.enqueue_song s }

  build_response data: {
    jukebox: songs.map(&:full_title),
  }
end
list_albums() click to toggle source

List all albums in the index. @command `list-albums` @cmdexample `muzak> list-albums`

# File lib/muzak/cmd/index.rb, line 40
def list_albums
  build_response data: {
    albums: index.album_names,
  }
end
list_artists() click to toggle source

List all artists in the index. @command `list-artists` @cmdexample `muzak> list-artists`

# File lib/muzak/cmd/index.rb, line 31
def list_artists
  build_response data: {
    artists: index.artists,
  }
end
list_playlists() click to toggle source

List all currently available playlists. @command `list-playlists` @cmdexample `muzak> list-playlists`

# File lib/muzak/cmd/playlist.rb, line 8
def list_playlists
  build_response data: {
    playlists: Playlist.playlist_names,
  }
end
list_plugins() click to toggle source

List all available plugins. @command `list-plugins` @cmdexample `muzak> list-plugins` @note This list will differ from loaded plugins, if not all available

plugins are configured.
# File lib/muzak/cmd/meta.rb, line 31
def list_plugins
  build_response data: {
    plugins: Plugin.plugin_names,
  }
end
list_queue() click to toggle source

Tell the player to list its internal queue. @command `list-queue` @cmdexample `muzak> list-queue`

# File lib/muzak/cmd/player.rb, line 133
def list_queue
  build_response data: {
    queue: player.list_queue.map(&:title),
  }
end
next() click to toggle source

Tell the player to load the next song. @command `next` @cmdexample `muzak> next`

# File lib/muzak/cmd/player.rb, line 70
def next
  player.next_song

  build_response
end
now_playing() click to toggle source

Retrieve the currently playing song from the player and print it. @command `now-playing` @cmdexample `muzak> now-playing`

# File lib/muzak/cmd/player.rb, line 161
def now_playing
  if player.playing?
    build_response data: {
      playing: player.now_playing&.full_title,
    }
  else
    build_response error: "no currently playing song"
  end
end
pause() click to toggle source

Tell the player to pause. @command `pause` @cmdexample `muzak> pause`

# File lib/muzak/cmd/player.rb, line 48
def pause
  player.pause

  build_response
end
ping() click to toggle source

Return a simple heartbeat message. @command `ping` @cmdexample `muzak> ping`

# File lib/muzak/cmd/meta.rb, line 8
def ping
  timestamp = Time.now.to_i
  debug "pong: #{timestamp}"

  build_response data: {
    pong: timestamp,
  }
end
play() click to toggle source

Tell the player to begin playback. @command `play` @cmdexample `muzak> play`

# File lib/muzak/cmd/player.rb, line 39
def play
  player.play

  build_response
end
player_activate() click to toggle source

Activate the configured player. @command `player-activate` @cmdexample `muzak> player-activate` @note Many playback commands will automatically activate the player.

# File lib/muzak/cmd/player.rb, line 9
def player_activate
  if player.running?
    danger "player is already running"
  else
    player.activate!
  end

  build_response data: {
    player: player.class.name,
  }
end
player_deactivate() click to toggle source

Deactivate the configured player. @command `player-deactivate` @cmdexample `muzak> player-deactivate` @note Deactivating the player (usually) ends playback immediately.

# File lib/muzak/cmd/player.rb, line 25
def player_deactivate
  danger "player is not running" unless player.running?

  # do cleanup even if the player isn't running, just in case
  player.deactivate!

  build_response data: {
    player: player.class.name,
  }
end
playlist_add_album(pname, *args) click to toggle source

Add the given album to the given playlist. @command `playlist-add-album <playlist> <album name>` @cmdexample `muzak> playlist-add-album favorites Your Favorite Album`

# File lib/muzak/cmd/playlist.rb, line 39
def playlist_add_album(pname, *args)
  album_name = args.join(" ")
  album = index.albums[album_name]

  if album
    playlists[pname].add(album.songs)
    build_response
  else
    build_response error: "no such album: '#{album_name}'"
  end
end
playlist_add_artist(pname, *args) click to toggle source

Add the given artist to the given playlist. @command `playlist-add-artist <playlist> <artist name>` @cmdexample `muzak> playlist-add-artist dad-rock The Rolling Stones`

# File lib/muzak/cmd/playlist.rb, line 54
def playlist_add_artist(pname, *args)
  artist = args.join(" ")
  songs = index.songs_by(artist)

  if songs.any?
    playlists[pname].add(songs)
    build_response
  else
    build_response error: "no songs by artist: '#{artist}'"
  end
end
playlist_add_current(pname) click to toggle source

Add the currently playing song to the given playlist. @see Muzak::Player::StubPlayer#now_playing @command `playlist-add-current <playlist>` @cmdexample `muzak> playlist-add-current favorites`

# File lib/muzak/cmd/playlist.rb, line 70
def playlist_add_current(pname)
  if player.running?
    playlists[pname].add player.now_playing
    build_response
  else
    build_response error: "the player is not running"
  end
end
playlist_del_current(pname) click to toggle source

Deletes the currently playing song from the given playlist. @see Muzak::Player::StubPlayer#now_playing @command `playlist-del-current <playlist>` @cmdexample `muzak> playlist-del-current favorites`

# File lib/muzak/cmd/playlist.rb, line 83
def playlist_del_current(pname)
  if player.running?
    playlists[pname].delete player.now_playing
    build_response
  else
    build_response error: "the player is not running"
  end
end
playlist_delete(pname) click to toggle source

Delete the given playlist. @command `playlist-delete <playlist>` @cmdexample `muzak> playlist-delete favorites`

# File lib/muzak/cmd/playlist.rb, line 17
def playlist_delete(pname)
  debug "deleting playist '#{pname}'"

  Playlist.delete!(pname)
  playlists[pname] = nil

  build_response
end
playlist_shuffle(pname) click to toggle source

Shuffle the given playlist. @see Muzak::Playlist#shuffle! @command `playlist-shuffle <playlist>` @cmdexample `muzak> playlist-shuffle dad-rock`

# File lib/muzak/cmd/playlist.rb, line 96
def playlist_shuffle(pname)
  playlists[pname].shuffle!
  build_response
end
previous() click to toggle source

Tell the player to load the previous song. @command `previous` @cmdexample `muzak> previous`

# File lib/muzak/cmd/player.rb, line 79
def previous
  player.previous_song

  build_response
end
quit() click to toggle source

Terminates the muzak instance (not just the client). @command `quit` @cmdexample `muzak> quit`

# File lib/muzak/cmd/meta.rb, line 40
def quit
  verbose "muzak is quitting..."
  player.deactivate!

  event :instance_quitting
  build_response data: "quitting"
end
reload_index() click to toggle source

Reload the active index from the index file. @note This does not rebuild the index. @command `reload-index` @cmdexample `muzak> reload-index`

# File lib/muzak/cmd/index.rb, line 9
def reload_index
  index.reload!

  build_response data: {
    artist_count: index.artists.size,
    album_count: index.albums.size,
    deep: index.deep?,
  }
end
shuffle_queue() click to toggle source

Tell the player to shuffle its internal queue. @command `shuffle-queue` @cmdexample `muzak> shuffle-queue`

# File lib/muzak/cmd/player.rb, line 142
def shuffle_queue
  player.shuffle_queue

  build_response
end
songs_by_artist(*args) click to toggle source

List all songs by the given artist in the index. @command `songs-by-artist <artist name>` @cmdexample `muzak> songs-by-artist Your Next Favorite Artist`

# File lib/muzak/cmd/index.rb, line 62
def songs_by_artist(*args)
  artist = args.join(" ")

  songs = index.songs_by(artist).map(&:title)

  build_response data: {
    songs: songs,
  }
end
toggle() click to toggle source

Tell the player to toggle its playback state. @command `toggle` @cmdexample `muzak> toggle`

# File lib/muzak/cmd/player.rb, line 57
def toggle
  if player.playing?
    player.pause
  else
    player.play
  end

  build_response
end