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

Class Method Details

.commandsObject



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

Returns:

  • (Boolean)


3
4
5
# File 'lib/muzak/cmd/index.rb', line 3

def _index_available?
  File.file?(INDEX_FILE)
end

#_index_loaded?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/muzak/cmd/index.rb', line 7

def _index_loaded?
  !!@index
end

#_index_outdated?Boolean

Returns:

  • (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_syncObject



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

Returns:

  • (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_queueObject



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_loadObject



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_pluginsObject



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_queueObject



75
76
77
# File 'lib/muzak/cmd/player.rb', line 75

def list_queue
  puts @player.list_queue.map(&:title)
end

#nextObject



35
36
37
# File 'lib/muzak/cmd/player.rb', line 35

def next
  @player.next_song
end

#now_playingObject



87
88
89
90
91
# File 'lib/muzak/cmd/player.rb', line 87

def now_playing
  return unless @player.playing?

  info @player.now_playing.full_title
end

#pauseObject



23
24
25
# File 'lib/muzak/cmd/player.rb', line 23

def pause
  @player.pause
end

#playObject



19
20
21
# File 'lib/muzak/cmd/player.rb', line 19

def play
  @player.play
end

#player_activateObject



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_deactivateObject



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.now_playing
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.now_playing
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

#previousObject



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

def previous
  @player.previous_song
end

#quitObject



13
14
15
16
# File 'lib/muzak/cmd/meta.rb', line 13

def quit
  debug "muzak is quitting..."
  @player.deactivate!
end

#shuffle_queueObject



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

#toggleObject



27
28
29
30
31
32
33
# File 'lib/muzak/cmd/player.rb', line 27

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