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