Skip to content

Instantly share code, notes, and snippets.

@mobily
Last active February 24, 2024 13:57
Show Gist options
  • Select an option

  • Save mobily/4e5a11bfeefa2fd7f77e8d5681de5c44 to your computer and use it in GitHub Desktop.

Select an option

Save mobily/4e5a11bfeefa2fd7f77e8d5681de5c44 to your computer and use it in GitHub Desktop.
msgpack: https://github.com/kieselsteini/msgpack
local utils = require("utils")
local hhann = require("modules.hhann")
local msgpack = require("utils.msgpack")
local switch = utils.switch
local server = hs.httpserver.new()
local client = hs.socket.new()
local annotate = hs.hotkey.modal.new()
annotate:bind(
{},
"escape",
nil,
function()
hhann:stop()
annotate:exit()
end
)
local M = {}
local sockets = {}
local id = 0
local each_connected_socket = function(callback)
for i, pair in ipairs(sockets) do
local servername, socket = table.unpack(pair)
if not socket ~= nil and socket:connected() then
callback(socket, servername)
end
end
end
local request = function(req, params)
id = id + 1
return msgpack.encode({0, id, req, params})
end
local not_found = function()
return {
hs.json.encode({success = false, message = "not found"}),
404,
{
["Content-Type"] = "application/json"
}
}
end
local success = function(data)
return {
hs.json.encode({success = true, data = data}),
200,
{
["Content-Type"] = "application/json"
}
}
end
local get = function(path, data)
return function()
return switch(
path,
{
["/annotate"] = function()
hhann:start()
hhann:startAnnotating()
annotate:enter()
return success("ok")
end,
["/spotify/get_current_track"] = function()
return success(hs.spotify.getCurrentArtist() .. " - " .. hs.spotify.getCurrentTrack())
end,
["default"] = not_found
}
)
end
end
local post = function(path, data)
return function()
return switch(
path,
{
["/notification"] = function()
hs.notify.show("Neovim", "", data.value)
return success("ok")
end,
["/connect"] = function()
local socket = hs.socket.new():connect(data.servername)
table.insert(sockets, {data.servername, socket})
return success("ok")
end,
["/disconnect"] = function()
each_connected_socket(
function(socket)
if servername == data.servername then
socket:disconnect()
end
end
)
return success("ok")
end,
["default"] = not_found
}
)
end
end
local callback = function(type, path, headers, body)
local data = hs.json.decode(body == "" and "{}" or body)
return table.unpack(
switch(
type,
{
["GET"] = get(path, data),
["POST"] = post(path, data),
["default"] = not_found
}
)
)
end
local timer =
hs.timer.new(
1,
function()
each_connected_socket(
function(socket)
if (hs.spotify.getCurrentArtist() ~= nil and hs.spotify.getCurrentTrack() ~= nil) then
local current_song = hs.spotify.getCurrentArtist() .. " - " .. hs.spotify.getCurrentTrack()
socket:write(
request(
"nvim_exec_lua",
{
"require('plugins.hammerspoon'):set_state({ current_song = '" ..
current_song .. "', is_playing = " .. tostring(hs.spotify.isPlaying()) .. " })",
{}
}
)
)
else
socket:write(
request(
"nvim_exec_lua",
{
"require('plugins.hammerspoon'):set_state({ current_song = nil, is_playing = false })",
{}
}
)
)
end
end
)
end
)
M.init = function()
server:setPort(1122)
server:setCallback(callback)
server:start()
timer:start()
end
return M
local M = {}
M.switch = function(param, t)
local case = t[param]
if case then
return case()
end
local defaultFn = t["default"]
return defaultFn and defaultFn() or nil
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment