Commit | Line | Data |
---|---|---|
4411059d SK |
1 | #! /usr/bin/awk -f |
2 | ||
3 | # Msg separator | |
4 | /^OK/ {msg_count++; next} | |
5 | ||
6 | # Msg content | |
7 | /^[a-zA-Z-]+: / { | |
8 | key = $1 | |
9 | val = $0 | |
10 | sub(".*" key " *", "", val) | |
11 | sub(":$", "", key) | |
12 | key = tolower(key) | |
13 | # Note that we expect a particular order of response messages (also | |
14 | # reflected in the name of this script file): "status" THEN "currentsong" | |
15 | if (msg_count == 1) {status[key] = val} | |
16 | else if (msg_count == 2) {currentsong[key] = val} | |
17 | else { | |
18 | printf("Unexpected msg_count in mpd response: %d\n", msg_count) \ | |
19 | > "/dev/stderr" | |
20 | exit 1 | |
21 | } | |
22 | next | |
23 | } | |
24 | ||
25 | END { | |
26 | name = currentsong["name"] | |
27 | title = currentsong["title"] | |
28 | file = currentsong["file"] | |
29 | ||
30 | if (name) { | |
31 | song = name | |
32 | } else if (title) { | |
33 | song = title | |
34 | } else if (file) { | |
35 | last = split(file, parts, "/") | |
36 | song = parts[last] | |
37 | } else { | |
38 | song = "?" | |
39 | } | |
40 | ||
41 | format_time(status["time"], time) | |
81f81623 SK |
42 | state_symbol = symbol_of_state(status["state"]) |
43 | print("status", sprintf("%s %s %s", state_symbol, time["minimal_units"], time["percentage"])) | |
44 | } | |
45 | ||
46 | function symbol_of_state(state) { | |
47 | if (state = "play") { | |
48 | return "▶" | |
49 | } else if (state = "pause") { | |
50 | return "❚❚" | |
51 | } else if (state = "stop") { | |
52 | return "⬛" | |
53 | } else { | |
54 | print("Error: unexpected value for state: ", state) > "/dev/stderr" | |
55 | exit 1 | |
4411059d SK |
56 | } |
57 | } | |
58 | ||
59 | function format_time(time_str, time_arr, \ | |
60 | \ | |
61 | time_str_parts, | |
62 | seconds_current, | |
63 | seconds_total, | |
64 | hours, | |
65 | secs_beyond_hours, | |
66 | mins, | |
67 | secs, | |
68 | time_percentage \ | |
69 | ) { | |
70 | split(time_str, time_str_parts, ":") | |
71 | seconds_current = time_str_parts[1] | |
72 | seconds_total = time_str_parts[2] | |
73 | ||
74 | hours = int(seconds_current / 60 / 60); | |
75 | secs_beyond_hours = seconds_current - (hours * 60 * 60); | |
76 | mins = int(secs_beyond_hours / 60); | |
77 | secs = secs_beyond_hours - (mins * 60); | |
78 | ||
79 | if (hours > 0) { | |
80 | time_arr["minimal_units"] = sprintf("%d:%.2d:%.2d", hours, mins, secs) | |
81 | } else { | |
82 | time_arr["minimal_units"] = sprintf("%.2d:%.2d", mins, secs) | |
83 | } | |
84 | ||
85 | if (seconds_total > 0) { | |
86 | time_percentage = (seconds_current / seconds_total) * 100 | |
87 | time_arr["percentage"] = sprintf("%d%%", time_percentage) | |
88 | } else { | |
89 | time_arr["percentage"] = "~" | |
90 | } | |
91 | } |