1# This is free and unencumbered software released into the public domain.
2
3# Anyone is free to copy, modify, publish, use, compile, sell, or
4# distribute this software, either in source code form or as a compiled
5# binary, for any purpose, commercial or non-commercial, and by any
6# means.
7
8# In jurisdictions that recognize copyright laws, the author or authors
9# of this software dedicate any and all copyright interest in the
10# software to the public domain. We make this dedication for the benefit
11# of the public at large and to the detriment of our heirs and
12# successors. We intend this dedication to be an overt act of
13# relinquishment in perpetuity of all present and future rights to this
14# software under copyright law.
15
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22# OTHER DEALINGS IN THE SOFTWARE.
23
24provide-module discord-rpc %~
25 ## begin public options
26 declare-option bool discord_rpc_autostart true
27 declare-option str discord_rpc_description "i just be writing text and editing it"
28 declare-option str discord_rpc_image_description "i just be editing text!!!"
29 ## end public options
30
31 define-command -docstring %{
32 Begins a discord-rpc-cli process if none exists.
33 } start-discord-rpc %{
34 nop %sh{
35 # only one process should exist globally
36 lock="/tmp/kak-discord"
37 if [ ! -f "$lock" ]; then
38 # spawn in detached session
39 {
40 discord-rpc-cli -c '1397337509393989713' -N kak \
41 -d "$kak_opt_discord_rpc_description" \
42 -I "$kak_opt_discord_rpc_image_description"
43 } >/dev/null 2>&1 </dev/null &
44 # save the pid for killing later
45 pid="$!"
46 echo "$pid">"$lock"
47 fi
48 }
49 }
50 define-command -docstring %{
51 Stop the discord-rpc-cli process if it exists.
52 } stop-discord-rpc %{
53 nop %sh{
54 # only one process globally
55 lock="/tmp/kak-discord"
56 # if it exists, kill it
57 if [ -f "$lock" ]; then
58 kill "$(cat "$lock")"
59 rm "$lock"
60 fi
61 }
62 }
63 define-command -docstring %{
64 Restart the discord-rpc-cli process.
65 } restart-discord-rpc %{
66 stop-discord-rpc
67 start-discord-rpc
68 }
69 hook -once global KakBegin .* %{
70 evaluate-commands %sh{
71 if [ "$kak_opt_discord_rpc_autostart" = "true" ]; then
72 echo "start-discord-rpc"
73 fi
74 }
75 }
76 hook -once -always global KakEnd .* %{
77 evaluate-commands %sh{
78 # kill if this is the last kak session
79 count="$(kak -l | grep -v '(dead)' | wc -l)"
80 if [ ${count} -le 1 ]; then
81 echo "stop-discord-rpc"
82 fi
83 }
84 }
85~
86