summaryrefslogtreecommitdiffstats
path: root/modules/desktop/awesome/widgets/mpd.lua
blob: d588089d283fd5e8da8dd366233c8bdb8806db6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local awful = require("awful")
local spawn = require("awful.spawn")
local watch = require("awful.widget.watch")
local beautiful = require("beautiful")
local wibox = require("wibox")
local gears = require("gears")
local naughty = require("naughty")

local mpd_widget = {}

local MUSIC_ICON = ' '
local OFF_ICON = '󰝛 '

local PLAY_ICON = ' '
local PAUSE_ICON = ' '

local PREV_ICON = ' '
local NEXT_ICON = ' '

local function worker(user_args)
    local mpd_bar = wibox.widget {
        {
            id = "progress",
            widget = wibox.widget.progressbar,
            max_value = 1,
            forced_height = 12,
            forced_width = 150,
            border_width = 0,
            color = beautiful.bg_focus,
            background_color = beautiful.bg_normal,
        },
        {
            id = "title",
            widget = wibox.widget.textbox,
            text = "NO SONG",
            valign = "center",
            halign = "center",
        },
        layout = wibox.layout.stack
    }

    local status_icon = wibox.widget {
        id = "status",
        widget = wibox.widget.textbox,
        text = PLAY_ICON
    }

    local update = function(widget, stdout, _, _, _)
        local current_song = string.gmatch(stdout, "[^\r\n]+")()
        stdout = string.gsub(stdout, "\n", "")
        local mpd_percent = string.match(stdout, "(%d%d)%%")
        local mpd_status = string.match(stdout, "%[(%a+)%]")

        widget:get_children_by_id("title")[1].text = current_song
        widget:get_children_by_id("progress")[1].value = tonumber(mpd_percent)/100

        if mpd_status == "playing" then
            status_icon.text = PLAY_ICON
        elseif mpd_status == "paused" then
            status_icon.text = PAUSE_ICON
        end
    end

    mpd_bar:connect_signal("button::press", function(_, _, _, button)
        if button == 1 then awful.spawn("mpc toggle", false)
        elseif button == 3 then awful.spawn("kitty -e ncmpcpp", false)
        elseif button == 4 then awful.spawn("mpc next", false)
        elseif button == 5 then awful.spawn("mpc prev", false)
        end
    end)

    watch("mpc status", 1, update, mpd_bar)

    mpd_widget = wibox.widget {
        status_icon,
        mpd_bar,
        layout = wibox.layout.align.horizontal,
        spacing = 8,
    }

    return mpd_widget
end

return setmetatable(mpd_widget, { __call = function(_, ...)
    return worker(...)
end})