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
30
31
# 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,
    "deep-index" => false,
    "jukebox-size" => 100
  }

  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)


33
34
35
# File 'lib/muzak/cmd/config.rb', line 33

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_loaded?Boolean

Returns:

  • (Boolean)


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

def _playlist_loaded?
  !!@playlist
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



43
44
45
46
47
48
49
50
51
# File 'lib/muzak/cmd/config.rb', line 43

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



37
38
39
40
41
# File 'lib/muzak/cmd/config.rb', line 37

def config_load
  verbose "loading config from #{CONFIG_FILE}"

  @config = YAML::load_file(CONFIG_FILE)
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



33
34
35
36
37
38
# File 'lib/muzak/cmd/playlist.rb', line 33

def enqueue_playlist(*args)
  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
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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/muzak/cmd/playlist.rb', line 40

def playlist_add_album(*args)
  return unless _playlist_loaded?

  album_name = args.join(" ")
  return if album_name.nil?

  album = @index.albums[album_name]
  return if album.nil?

  @playlist.add(album.songs)
end

#playlist_add_artist(*args) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/muzak/cmd/playlist.rb', line 52

def playlist_add_artist(*args)
  return unless _playlist_loaded?

  artist = args.join(" ")
  return if artist.nil?

  @playlist.add(@index.songs_by(artist))
end

#playlist_add_current(*args) ⇒ Object



61
62
63
64
65
# File 'lib/muzak/cmd/playlist.rb', line 61

def playlist_add_current(*args)
  return unless @player.running? && _playlist_loaded?

  @playlist.add @player.now_playing
end

#playlist_del_current(*args) ⇒ Object



67
68
69
70
71
# File 'lib/muzak/cmd/playlist.rb', line 67

def playlist_del_current(*args)
  return unless @player.running? && _playlist_loaded?

  @playlist.delete @player.now_playing
end

#playlist_delete(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/muzak/cmd/playlist.rb', line 23

def playlist_delete(*args)
  fail_arity(args, 1)
  pname = args.shift

  debug "deleting playist '#{pname}'"

  Playlist.delete!(pname)
  @playlist = nil
end

#playlist_load(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/muzak/cmd/playlist.rb', line 13

def playlist_load(*args)
  fail_arity(args, 1)
  pname = args.shift

  info "loading playlist '#{pname}'"
  @playlist = Playlist.new(pname)

  event :playlist_loaded, @playlist
end

#playlist_shuffle(*args) ⇒ Object



73
74
75
76
77
# File 'lib/muzak/cmd/playlist.rb', line 73

def playlist_shuffle(*args)
  return unless _playlist_loaded?

  @playlist.shuffle!
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