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



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

Returns:

  • (Boolean)


5
6
7
# File 'lib/muzak/cmd/config.rb', line 5

def _config_available?
  File.file?(CONFIG_FILE)
end

#_config_initObject



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.expand_path("~/music"),
    "player" => "mpv",
    "index-autobuild" => 86400
  }

  Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
  _config_sync
end

#_config_loaded?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/muzak/cmd/config.rb', line 9

def _config_loaded?
  !!@config
end

#_config_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/muzak/cmd/config.rb', line 31

def _config_plugin?(name)
  @config.key?("plugin-#{name}")
end

#_config_syncObject



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

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

#_playlist_available?(pname) ⇒ Boolean

Returns:

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

Returns:

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



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_loadObject



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_playlistObject



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_loadObject



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_playlistsObject



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_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



63
64
65
# File 'lib/muzak/cmd/player.rb', line 63

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

#nextObject



27
28
29
# File 'lib/muzak/cmd/player.rb', line 27

def next
  @player.next_song
end

#now_playingObject



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

def now_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



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_currentObject



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

  playlist_sync
end

#playlist_del_currentObject



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

  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_shuffleObject



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

#previousObject



31
32
33
# File 'lib/muzak/cmd/player.rb', line 31

def previous
  @player.previous_song
end

#quitObject



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

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

#shuffle_queueObject



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