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

Overview

The namespace for all commands exposed by muzak.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandsArray<String>

Returns all valid muzak commands.

Returns:

  • (Array<String>)

    all valid muzak commands



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| Config.resolve_method c }
end

Instance Method Details

#albums_by_artist(*args) ⇒ Object

List all albums by the given artist in the index.

muzak command:

  • albums-by-artist <artist name>

example invocation:

  • muzak> albums-by-artist Your Favorite Artist



49
50
51
52
53
54
55
56
57
# 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_queueObject

Note:

This does not (usually) stop the current song.

Tell the player to clear its internal queue.

muzak command:

  • clear-queue

example invocation:

  • muzak> clear-queue



152
153
154
155
156
# File 'lib/muzak/cmd/player.rb', line 152

def clear_queue
  player.clear_queue

  build_response
end

#config_get(key) ⇒ Object

Query the Muzak::Config for a given key.

muzak command:

  • config-get <key>

example invocation:

  • muzak> config-get player



10
11
12
13
14
# 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_indexObject

Dump a hash representation of the index.

muzak command:

  • dump-index

example invocation:

  • muzak> dump-index



22
23
24
25
26
# File 'lib/muzak/cmd/index.rb', line 22

def dump_index
  build_response data: {
    index: index.hash,
  }
end

#enqueue_album(*args) ⇒ Object

Tell the player to enqueue the given album.

muzak command:

  • enqueue-album <album name>

example invocation:

  • muzak> enqueue-album Your Favorite Album



105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object

Tell the player to enqueue all songs by the given artist.

muzak command:

  • enqueue-artist <artist name>

example invocation:

  • muzak> enqueue-artist Your Favorite Artist



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

Add the given playlist to the player's queue.

muzak command:

  • enqueue-playlist <playlist>

example invocation:

  • muzak> enqueue-playlist favorites



29
30
31
32
33
34
# 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) ⇒ Object

Return a "helpful" listing of commands.

muzak command:

  • help

example invocation:

  • muzak> help



20
21
22
23
24
# File 'lib/muzak/cmd/meta.rb', line 20

def help(*_args)
  build_response data: {
    commands: Muzak::Cmd.commands,
  }
end

#jukebox(count = Config.jukebox_size) ⇒ Object

Tell the player to load the given number of random songs.

muzak command:

  • jukebox [count]

example invocation:

  • muzak> jukebox 150



120
121
122
123
124
125
126
127
128
# 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_albumsObject

List all albums in the index.

muzak command:

  • list-albums

example invocation:

  • muzak> list-albums



40
41
42
43
44
# File 'lib/muzak/cmd/index.rb', line 40

def list_albums
  build_response data: {
    albums: index.album_names,
  }
end

#list_artistsObject

List all artists in the index.

muzak command:

  • list-artists

example invocation:

  • muzak> list-artists



31
32
33
34
35
# File 'lib/muzak/cmd/index.rb', line 31

def list_artists
  build_response data: {
    artists: index.artists,
  }
end

#list_playlistsObject

List all currently available playlists.

muzak command:

  • list-playlists

example invocation:

  • muzak> list-playlists



8
9
10
11
12
# File 'lib/muzak/cmd/playlist.rb', line 8

def list_playlists
  build_response data: {
    playlists: Playlist.playlist_names,
  }
end

#list_pluginsObject

Note:

This list will differ from loaded plugins, if not all available plugins are configured.

List all available plugins.

muzak command:

  • list-plugins

example invocation:

  • muzak> list-plugins



31
32
33
34
35
# File 'lib/muzak/cmd/meta.rb', line 31

def list_plugins
  build_response data: {
    plugins: Plugin.plugin_names,
  }
end

#list_queueObject

Tell the player to list its internal queue.

muzak command:

  • list-queue

example invocation:

  • muzak> list-queue



133
134
135
136
137
# File 'lib/muzak/cmd/player.rb', line 133

def list_queue
  build_response data: {
    queue: player.list_queue.map(&:title),
  }
end

#nextObject

Tell the player to load the next song.

muzak command:

  • next

example invocation:

  • muzak> next



70
71
72
73
74
# File 'lib/muzak/cmd/player.rb', line 70

def next
  player.next_song

  build_response
end

#now_playingObject

Retrieve the currently playing song from the player and print it.

muzak command:

  • now-playing

example invocation:

  • muzak> now-playing



161
162
163
164
165
166
167
168
169
# 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

#pauseObject

Tell the player to pause.

muzak command:

  • pause

example invocation:

  • muzak> pause



48
49
50
51
52
# File 'lib/muzak/cmd/player.rb', line 48

def pause
  player.pause

  build_response
end

#pingObject

Return a simple heartbeat message.

muzak command:

  • ping

example invocation:

  • muzak> ping



8
9
10
11
12
13
14
15
# File 'lib/muzak/cmd/meta.rb', line 8

def ping
  timestamp = Time.now.to_i
  debug "pong: #{timestamp}"

  build_response data: {
    pong: timestamp,
  }
end

#playObject

Tell the player to begin playback.

muzak command:

  • play

example invocation:

  • muzak> play



39
40
41
42
43
# File 'lib/muzak/cmd/player.rb', line 39

def play
  player.play

  build_response
end

#player_activateObject

Note:

Many playback commands will automatically activate the player.

Activate the configured player.

muzak command:

  • player-activate

example invocation:

  • muzak> player-activate



9
10
11
12
13
14
15
16
17
18
19
# 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_deactivateObject

Note:

Deactivating the player (usually) ends playback immediately.

Deactivate the configured player.

muzak command:

  • player-deactivate

example invocation:

  • muzak> player-deactivate



25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object

Add the given album to the given playlist.

muzak command:

  • playlist-add-album <playlist> <album name>

example invocation:

  • muzak> playlist-add-album favorites Your Favorite Album



39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

Add the given artist to the given playlist.

muzak command:

  • playlist-add-artist <playlist> <artist name>

example invocation:

  • muzak> playlist-add-artist dad-rock The Rolling Stones



54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Add the currently playing song to the given playlist.

See Also:

muzak command:

  • playlist-add-current <playlist>

example invocation:

  • muzak> playlist-add-current favorites



70
71
72
73
74
75
76
77
# 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) ⇒ Object

Deletes the currently playing song from the given playlist.

See Also:

muzak command:

  • playlist-del-current <playlist>

example invocation:

  • muzak> playlist-del-current favorites



83
84
85
86
87
88
89
90
# 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) ⇒ Object

Delete the given playlist.

muzak command:

  • playlist-delete <playlist>

example invocation:

  • muzak> playlist-delete favorites



17
18
19
20
21
22
23
24
# 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) ⇒ Object

Shuffle the given playlist.

See Also:

muzak command:

  • playlist-shuffle <playlist>

example invocation:

  • muzak> playlist-shuffle dad-rock



96
97
98
99
# File 'lib/muzak/cmd/playlist.rb', line 96

def playlist_shuffle(pname)
  playlists[pname].shuffle!
  build_response
end

#previousObject

Tell the player to load the previous song.

muzak command:

  • previous

example invocation:

  • muzak> previous



79
80
81
82
83
# File 'lib/muzak/cmd/player.rb', line 79

def previous
  player.previous_song

  build_response
end

#quitObject

Terminates the muzak instance (not just the client).

muzak command:

  • quit

example invocation:

  • muzak> quit



40
41
42
43
44
45
46
# 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_indexObject

Note:

This does not rebuild the index.

Reload the active index from the index file.

muzak command:

  • reload-index

example invocation:

  • muzak> reload-index



9
10
11
12
13
14
15
16
17
# 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_queueObject

Tell the player to shuffle its internal queue.

muzak command:

  • shuffle-queue

example invocation:

  • muzak> shuffle-queue



142
143
144
145
146
# File 'lib/muzak/cmd/player.rb', line 142

def shuffle_queue
  player.shuffle_queue

  build_response
end

#songs_by_artist(*args) ⇒ Object

List all songs by the given artist in the index.

muzak command:

  • songs-by-artist <artist name>

example invocation:

  • muzak> songs-by-artist Your Next Favorite Artist



62
63
64
65
66
67
68
69
70
# 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

#toggleObject

Tell the player to toggle its playback state.

muzak command:

  • toggle

example invocation:

  • muzak> toggle



57
58
59
60
61
62
63
64
65
# File 'lib/muzak/cmd/player.rb', line 57

def toggle
  if player.playing?
    player.pause
  else
    player.play
  end

  build_response
end