My configuration files for Debian/Ubuntu applications
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

98 行
3.7 KiB

  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2014, anticlockwise <http://github.com/anticlockwise>
  4. --]]
  5. local helpers = require("lain.helpers")
  6. local shell = require("awful.util").shell
  7. local focused = require("awful.screen").focused
  8. local escape_f = require("awful.util").escape
  9. local naughty = require("naughty")
  10. local wibox = require("wibox")
  11. local os = os
  12. local string = string
  13. -- MOC audio player
  14. -- lain.widget.contrib.moc
  15. local function factory(args)
  16. args = args or {}
  17. local moc = { widget = args.widget or wibox.widget.textbox() }
  18. local timeout = args.timeout or 2
  19. local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
  20. local cover_pattern = args.cover_pattern or "*\\.(jpg|jpeg|png|gif)$"
  21. local cover_size = args.cover_size or 100
  22. local default_art = args.default_art or ""
  23. local followtag = args.followtag or false
  24. local settings = args.settings or function() end
  25. moc_notification_preset = { title = "Now playing", timeout = 6 }
  26. helpers.set_map("current moc track", nil)
  27. function moc.update()
  28. helpers.async("mocp -i", function(f)
  29. moc_now = {
  30. state = "N/A",
  31. file = "N/A",
  32. artist = "N/A",
  33. title = "N/A",
  34. album = "N/A",
  35. elapsed = "N/A",
  36. total = "N/A"
  37. }
  38. for line in string.gmatch(f, "[^\n]+") do
  39. for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
  40. if k == "State" then moc_now.state = v
  41. elseif k == "File" then moc_now.file = v
  42. elseif k == "Artist" then moc_now.artist = escape_f(v)
  43. elseif k == "SongTitle" then moc_now.title = escape_f(v)
  44. elseif k == "Album" then moc_now.album = escape_f(v)
  45. elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
  46. elseif k == "TotalTime" then moc_now.total = escape_f(v)
  47. end
  48. end
  49. end
  50. moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
  51. moc_now.album, moc_now.total, moc_now.title)
  52. widget = moc.widget
  53. settings()
  54. if moc_now.state == "PLAY" then
  55. if moc_now.title ~= helpers.get_map("current moc track") then
  56. helpers.set_map("current moc track", moc_now.title)
  57. if followtag then moc_notification_preset.screen = focused() end
  58. local common = {
  59. preset = moc_notification_preset,
  60. icon = default_art,
  61. icon_size = cover_size,
  62. replaces_id = moc.id,
  63. }
  64. local path = string.format("%s/%s", music_dir, string.match(moc_now.file, ".*/"))
  65. local cover = string.format("find '%s' -maxdepth 1 -type f | egrep -i -m1 '%s'", path, cover_pattern)
  66. helpers.async({ shell, "-c", cover }, function(current_icon)
  67. common.icon = current_icon:gsub("\n", "")
  68. moc.id = naughty.notify(common).id
  69. end)
  70. end
  71. elseif moc_now.state ~= "PAUSE" then
  72. helpers.set_map("current moc track", nil)
  73. end
  74. end)
  75. end
  76. moc.timer = helpers.newtimer("moc", timeout, moc.update, true, true)
  77. return moc
  78. end
  79. return factory