From 050dfa3505da5fe6c64cbe740f7097cd8784fd90 Mon Sep 17 00:00:00 2001 From: Alexander Yakushev Date: Fri, 19 Aug 2011 10:30:25 +0300 Subject: coverart.sh simply rewritten in Lua --- README | 2 +- awesompd.lua | 70 +++++++++++++++++++++++++++++++++++++++--- icons/default_album_cover.png | Bin 0 -> 18940 bytes jamendo.lua | 6 ++++ rcsample.lua | 4 +-- 5 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 icons/default_album_cover.png diff --git a/README b/README index a5e52c6..2a2e3bc 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ This is a MPD widget for AwesomeWM. -For the detailed installation guide see https://awesome.naquadah.org/wiki/Awesompd_widget . +For the detailed installation guide see http://awesome.naquadah.org/wiki/Awesompd_widget . diff --git a/awesompd.lua b/awesompd.lua index 4e76258..2731f39 100644 --- a/awesompd.lua +++ b/awesompd.lua @@ -1,7 +1,7 @@ --------------------------------------------------------------------------- -- @author Alexander Yakushev <yakushev.alex@gmail.com> -- @copyright 2010-2011 Alexander Yakushev --- @release v1.0 +-- @release v1.0.2 --------------------------------------------------------------------------- require('utf8') @@ -66,6 +66,8 @@ function awesompd.load_icons(path) awesompd.ICONS.PREV = awesompd.try_load(path .. "/prev_icon.png") awesompd.ICONS.CHECK = awesompd.try_load(path .. "/check_icon.png") awesompd.ICONS.RADIO = awesompd.try_load(path .. "/radio_icon.png") + awesompd.ICONS.DEFAULT_ALBUM_COVER = + awesompd.try_load(path .. "/default_album_cover.png") end -- Function that returns a new awesompd object. @@ -93,7 +95,7 @@ function awesompd:create() instance.recreate_jamendo_formats = true instance.recreate_jamendo_order = true instance.current_number = 0 - instance.menu_shown = false + instance.menu_shown = false -- Default user options instance.servers = { { server = "localhost", port = 6600 } } @@ -106,7 +108,8 @@ function awesompd:create() instance.rdecorator = " " instance.show_jamendo_album_covers = true instance.album_cover_size = 50 - + instance.show_local_album_covers = true + -- Widget configuration instance.widget:add_signal("mouse::enter", function(c) instance:notify_track() @@ -174,6 +177,8 @@ end -- /// Group of mpc command functions /// +-- Takes a command to mpc and a hook that is provided with awesompd +-- instance and the result of command execution. function awesompd:command(com,hook) local file = io.popen(self:mpcquery() .. com) if hook then @@ -182,6 +187,14 @@ function awesompd:command(com,hook) file:close() end +-- Takes a command to mpc and read mode and returns the result. +function awesompd:command_read(com, mode) + self:command(com, function(_, f) + result = f:read(mode) + end) + return result +end + function awesompd:command_toggle() return function() self:command("toggle",self.update_track) @@ -586,7 +599,8 @@ end function awesompd:add_hint(hint_title, hint_text, hint_image) self:remove_hint() - hint_image = self.show_jamendo_album_covers and hint_image or nil + hint_image = self.show_jamendo_album_covers and hint_image or + self.show_local_album_covers and self:try_get_local_cover() or nil self.notification = naughty.notify({ title = hint_title , text = awesompd.protect_string(hint_text) , timeout = 5 @@ -813,7 +827,7 @@ function awesompd.protect_string(str, for_menu) end end --- Displays a inputbox on the screen (looks like naughty with prompt). +-- Displays an inputbox on the screen (looks like naughty with prompt). -- title_text - bold text on the first line -- prompt_text - preceding text on the second line -- hook - function that will be called with input data @@ -853,3 +867,49 @@ function awesompd:display_inputbox(title_text, prompt_text, hook) awful.prompt.run( { prompt = " " .. prompt_text .. ": " }, wprompt.widget, exe_callback, nil, nil, nil, done_callback) end + +-- Tries to find an album cover for the track that is currently +-- playing. +function awesompd:try_get_local_cover() + local result = self.ICONS.DEFAULT_ALBUM_COVER + if self.show_local_album_covers and self.mpd_config then + -- First find the music directory in MPD configuration file + local _, _, music_folder = string.find( + io.popen('cat ' .. self.mpd_config .. ' | grep -v "#" | grep music_directory'):read("*line"), + 'music_directory%s+"(.+)"') + music_folder = music_folder .. "/" + dbg("musfolder", music_folder) + + -- Get the path to the file currently playing. + local _, _, current_file_folder = + string.find(self:command_read('current -f "%file%"', "*line"), '(.+%/).*') + + local folder = music_folder .. current_file_folder + dbg("folder", folder) + -- Get all images in the folder + local covers_bus = io.popen('ls "' .. folder .. '" | grep -P "\.jpg\|\.png\|\.gif"') + dbg('wtf', 'ls "' .. folder .. '" | grep "\.jpg\|\.png\|\.gif"') + local covers = {} + for l in covers_bus:lines() do + dbg('here') + table.insert(covers, l) + end + dbg('first', covers[1]) + + if table.getn(covers) > 0 then + result = folder .. covers[1] + for i = 2, table.getn(covers) do + -- Searching for front cover with grep because Lua regular + -- expressions suck:[ + local front_cover = + io.popen('echo "' .. covers[i] .. + '" | grep -i "cover\|front\|folder\|albumart" | head -n 1') + if front_cover then + result = folder .. front_cover + break + end + end + end + end + return result +end diff --git a/icons/default_album_cover.png b/icons/default_album_cover.png new file mode 100644 index 0000000..b952ba4 Binary files /dev/null and b/icons/default_album_cover.png differ diff --git a/jamendo.lua b/jamendo.lua index f805f2f..1dd421c 100644 --- a/jamendo.lua +++ b/jamendo.lua @@ -1,3 +1,9 @@ +--------------------------------------------------------------------------- +-- @author Alexander Yakushev <yakushev.alex@gmail.com> +-- @copyright 2011 Alexander Yakushev +-- @release v1.0.2 +--------------------------------------------------------------------------- + module('jamendo', package.seeall) -- Grab environment diff --git a/rcsample.lua b/rcsample.lua index 71d9349..2cf5938 100644 --- a/rcsample.lua +++ b/rcsample.lua @@ -99,11 +99,11 @@ mysystray = widget({ type = "systray" }) -- If true, song notifications for Jamendo tracks will also contain -- album cover image. - instance.show_jamendo_album_covers = true + musicwidget.show_jamendo_album_covers = true -- Specify how big in pixels should an album cover be. Maximum value -- is 100. - instance.album_cover_size = 50 + musicwidget.album_cover_size = 50 -- Specify decorators on the left and the right side of the -- widget. Or just leave empty strings if you decorate the widget -- cgit v1.2.3