Skip to content

Commit

Permalink
feat: make query_command shell-agnostic (#19)
Browse files Browse the repository at this point in the history
* fix #18 by making `query_command` shell-agnostic

`query_command` is now of type `table` so that `vim.fn.jobstart` does
not use `shell`.

* style: stylua formatting

* fix: linux `query_command`

* Removes shell syntax when dropping priv

* removes unnecessary quoting from dbus-send command

* fix: handle `$SUDO_USER` being `nil`

this should help with escalation methods that aren't sudo, we don't have
to add support for them right now, but at least alert the user that an
error occurred.

* style: spaces -> tabs

* style: didn't quite get all of them

---------

Co-authored-by: winston <[email protected]>
  • Loading branch information
bugabinga and nekowinston authored Oct 9, 2024
1 parent 2190e4c commit d365bec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
57 changes: 37 additions & 20 deletions lua/auto-dark-mode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local set_light_mode
---@type number
local update_interval

---@type string
---@type table
local query_command
---@type "Linux" | "Darwin" | "Windows_NT" | "WSL"
local system
Expand All @@ -22,27 +22,27 @@ local system
local fallback

-- Parses the query response for each system
---@param res string
---@param res table
---@return boolean
local function parse_query_response(res)
if system == "Linux" then
-- https://github.com/flatpak/xdg-desktop-portal/blob/c0f0eb103effdcf3701a1bf53f12fe953fbf0b75/data/org.freedesktop.impl.portal.Settings.xml#L32-L46
-- 0: no preference
-- 1: dark
-- 2: light
if string.match(res, "uint32 1") ~= nil then
if string.match(res[1], "uint32 1") ~= nil then
return true
elseif string.match(res, "uint32 2") ~= nil then
elseif string.match(res[1], "uint32 2") ~= nil then
return false
else
return fallback == "dark"
end
elseif system == "Darwin" then
return res == "Dark"
return res[1] == "Dark"
elseif system == "Windows_NT" or system == "WSL" then
-- AppsUseLightTheme REG_DWORD 0x0 : dark
-- AppsUseLightTheme REG_DWORD 0x1 : light
return string.match(res, "1") == nil
-- AppsUseLightTheme REG_DWORD 0x0 : dark
-- AppsUseLightTheme REG_DWORD 0x1 : light
return string.match(res[3], "0x1") == nil
end
return false
end
Expand All @@ -51,8 +51,7 @@ end
local function check_is_dark_mode(callback)
utils.start_job(query_command, {
on_stdout = function(data)
-- we only care about the first line of the response
local is_dark_mode = parse_query_response(data[1])
local is_dark_mode = parse_query_response(data)
callback(is_dark_mode)
end,
})
Expand Down Expand Up @@ -86,34 +85,52 @@ local function init()
end

if system == "Darwin" then
query_command = "defaults read -g AppleInterfaceStyle"
query_command = { "defaults", "read", "-g", "AppleInterfaceStyle" }
elseif system == "Linux" then
if not vim.fn.executable("dbus-send") then
error([[
`dbus-send` is not available. The Linux implementation of
auto-dark-mode.nvim relies on `dbus-send` being on the `$PATH`.
]])
]])
end

query_command = table.concat({
"dbus-send --session --print-reply=literal --reply-timeout=1000",
query_command = {
"dbus-send",
"--session",
"--print-reply=literal",
"--reply-timeout=1000",
"--dest=org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings.Read",
"string:'org.freedesktop.appearance'",
"string:'color-scheme'",
}, " ")
"string:org.freedesktop.appearance",
"string:color-scheme",
}
elseif system == "Windows_NT" or system == "WSL" then
-- Don't swap the quotes; it breaks the code
query_command =
'reg.exe Query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize" /v AppsUseLightTheme | findstr.exe "AppsUseLightTheme"'
query_command = {
"reg.exe",
"Query",
"HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
"/v",
"AppsUseLightTheme",
}
else
return
end

if vim.fn.has("unix") ~= 0 then
if vim.loop.getuid() == 0 then
query_command = "su - $SUDO_USER -c " .. query_command
local sudo_user = vim.env.SUDO_USER

if sudo_user ~= nil then
query_command = vim.tbl_extend("keep", { "su", "-", sudo_user, "-c" }, query_command)
else
error([[
auto-dark-mode.nvim:
Running as `root`, but `$SUDO_USER` is not set.
Please open an issue to add support for your system.
]])
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/auto-dark-mode/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

---@param cmd string
---@param cmd table
---@param opts {input?: string, on_stdout?: function, on_exit?: function}
---@return number | 'the job id'
function M.start_job(cmd, opts)
Expand Down

0 comments on commit d365bec

Please sign in to comment.