Module: Muzak::Utils
- Included in:
- Index, Instance, Player, Player::StubPlayer, Plugin::StubPlugin, Song
- Defined in:
- lib/muzak/utils.rb
Overview
A collection of convenience utilities for use throughout muzak.
Class Method Summary collapse
-
.album_art?(filename) ⇒ Boolean
Tests whether the given filename is likely to be album art.
-
.music?(filename) ⇒ Boolean
Tests whether the given filename is likely to be music.
-
.which?(util) ⇒ Boolean
Tests whether the given utility is available in the system path.
Instance Method Summary collapse
-
#build_response(error: nil, data: nil) ⇒ Object
Returns a response hash containing the given data and error.
-
#danger(*args) ⇒ void
Outputs a boxed warning message.
-
#debug(*args) ⇒ void
Outputs a boxed debugging message.
-
#debug? ⇒ Boolean
Whether or not muzak is running in debug mode.
-
#error(*args) ⇒ void
Outputs a boxed error message.
-
#error!(*args) ⇒ void
Outputs a boxed error message and then exits.
-
#output(box, *args) ⇒ void
Outputs a boxed message and arguments.
-
#pretty(str, color = :none) ⇒ String
Formats a string with ANSI colors.
-
#verbose(*args) ⇒ void
Outputs a boxed verbose message.
-
#verbose? ⇒ Boolean
Whether or not muzak is running in verbose mode.
Class Method Details
.album_art?(filename) ⇒ Boolean
Tests whether the given filename is likely to be album art.
16 17 18 |
# File 'lib/muzak/utils.rb', line 16 def self.album_art?(filename) File.basename(filename) =~ Config::ALBUM_ART_REGEX end |
.music?(filename) ⇒ Boolean
Tests whether the given filename is likely to be music.
9 10 11 |
# File 'lib/muzak/utils.rb', line 9 def self.music?(filename) Config::MUSIC_SUFFIXES.include?(File.extname(filename.downcase)) end |
.which?(util) ⇒ Boolean
Tests whether the given utility is available in the system path.
23 24 25 26 27 |
# File 'lib/muzak/utils.rb', line 23 def self.which?(util) ENV["PATH"].split(File::PATH_SEPARATOR).any? do |path| File.executable?(File.join(path, util)) end end |
Instance Method Details
#build_response(error: nil, data: nil) ⇒ Object
Returns a response hash containing the given data and error.
108 109 110 111 112 113 114 115 116 |
# File 'lib/muzak/utils.rb', line 108 def build_response(error: nil, data: nil) { response: { error: error, data: data, method: caller_locations.first.label, }, } end |
#danger(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed warning message.
67 68 69 |
# File 'lib/muzak/utils.rb', line 67 def danger(*args) output pretty("warn", :yellow), args end |
#debug(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed debugging message.
90 91 92 93 94 |
# File 'lib/muzak/utils.rb', line 90 def debug(*args) return unless debug? context = is_a?(Module) ? name : self.class.name output pretty("debug", :yellow), "[#{context}]", args end |
#debug? ⇒ Boolean
Returns whether or not muzak is running in debug mode.
30 31 32 |
# File 'lib/muzak/utils.rb', line 30 def debug? Config.debug end |
#error(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed error message.
74 75 76 77 |
# File 'lib/muzak/utils.rb', line 74 def error(*args) context = is_a?(Module) ? name : self.class.name output pretty("error", :red), "[#{context}]", args end |
#error!(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed error message and then exits.
82 83 84 85 |
# File 'lib/muzak/utils.rb', line 82 def error!(*args) error(*args) exit 1 end |
#output(box, *args) ⇒ void
This method returns an undefined value.
Outputs a boxed message and arguments.
59 60 61 62 |
# File 'lib/muzak/utils.rb', line 59 def output(box, *args) msg = args.join(" ") puts "[#{box}] #{msg}" end |
#pretty(str, color = :none) ⇒ String
Formats a string with ANSI colors.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/muzak/utils.rb', line 43 def pretty(str, color = :none) colors = { none: 0, red: 31, green: 32, yellow: 33, blue: 34, } "\e[#{colors[color]}m#{str}\e[0m" end |
#verbose(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed verbose message.
99 100 101 102 103 |
# File 'lib/muzak/utils.rb', line 99 def verbose(*args) return unless verbose? output pretty("verbose", :blue), args end |
#verbose? ⇒ Boolean
Returns whether or not muzak is running in verbose mode.
35 36 37 |
# File 'lib/muzak/utils.rb', line 35 def verbose? Config.verbose end |