Skip to content

Commit 18c9240

Browse files
authored
feat: use new neovim apis, add :checkhealth, fix sudo query_command, WSL fixes (#35)
Uses `vim.uv` over `vim.loop`, `vim.system` over `vim.fn.jobstart`, adds a `:checkhealth` hook with a query_command benchmark, and fixes the `SUDO_USER` query_command. * feat: better `:checkhealth`, add default WSL `reg.exe` location, other various refactor * fix: vim.fn booleans & format error strings * fix: better healthcheck code, other improvements * test: add busted tests * test: remove neovim 0.8 * chore: clean up the rockspec * build: add `shell.nix` * test: run healthcheck directly * ci: rename unittests * test: use plenary to also cover `vim.fn` in neovim < 0.10 * test: don't call `check()` ourselves Apparently needs to be run inside :checkhealth to work before nvim 0.10 williamboman/nvim-lsp-installer#769 * test: rename for plenary, add query parsing test * fix: concat strings for `vim.fn.jobstart` stdout/stderr * ci(unit_tests): also run on macos/windows * ci: try to fix windows tests, p1 * ci: add neovim v0.7.2 to matrix * revert: "ci: add neovim v0.7.2 to matrix" This reverts commit 543b409. * ci: use plenary that still supports nvim 0.7.2 & add it back to matrix * refactor: use `Appearance?` instead of `bool` for current appearance * feat: handle more WSL errors * docs: add WSL-specific docs * ci(panvimdoc): clean up output a bit * docs: auto-generate vimdoc * fix(sudo): use `sudo` instead of `su` to resolve executable paths We're already relying on `sudo` through `$SUDO_USER` anyways, this is cleaner than using both `su` and `sudo`. To use `su`, we'd have to re-implement shell argument escaping and resolving the executable path (or inheriting parts of ENV manually) in Lua. * fix(interval): instantly sync theme in `start()` * fix(wsl): use new interop binfmt path * fix(wsl): check for Windows dir instead of always-existing `/mnt/c`
1 parent 02ef955 commit 18c9240

File tree

13 files changed

+552
-218
lines changed

13 files changed

+552
-218
lines changed

.github/workflows/panvimdoc.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ jobs:
1212
runs-on: ubuntu-latest
1313
name: README to vimdoc
1414
steps:
15-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1616

17-
- uses: kdheepak/panvimdoc@main
17+
- uses: kdheepak/panvimdoc@v4.0.1
1818
with:
19-
vimdoc: auto-dark-mode
20-
description: Plugin that automatically changes the editor appearance based on system settings.
19+
vimdoc: "auto-dark-mode"
20+
version: "NVIM v0.7.0 -> v0.10.4"
21+
description: "Plugin that automatically changes the editor appearance based on system settings."
22+
demojify: true
2123

2224
- uses: stefanzweifel/git-auto-commit-action@v4
2325
with:

.github/workflows/unit_tests.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Run Tests
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches: [master]
7+
8+
jobs:
9+
unit_tests:
10+
name: Run unit tests
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
os:
16+
- macos-latest
17+
- ubuntu-latest
18+
- windows-latest
19+
neovim_version:
20+
- v0.7.2
21+
- v0.8.3
22+
- v0.9.5
23+
- v0.10.4
24+
- nightly
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
- run: date +%F > todays-date
29+
- name: Restore cache for today's nightly.
30+
uses: actions/cache@v4
31+
with:
32+
path: ~/nvim-nightly
33+
key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }}
34+
35+
- name: Prepare plenary
36+
uses: actions/checkout@v4
37+
with:
38+
repository: nvim-lua/plenary.nvim
39+
# FIXME: update this once we drop support for neovim 0.7.*
40+
ref: 37604d95d8a212478063c40df4b7c48313453d52
41+
path: _runtime/plenary.nvim
42+
43+
- uses: rhysd/action-setup-vim@v1
44+
with:
45+
neovim: true
46+
version: ${{ matrix.neovim_version }}
47+
48+
- name: Run tests
49+
env:
50+
PLENARY: _runtime/plenary.nvim
51+
run: |
52+
nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}"

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
<!-- panvimdoc-ignore-start -->
12
# auto-dark-mode.nvim
3+
<!-- panvimdoc-ignore-end -->
24

35
A Neovim plugin for macOS, Linux, and Windows that automatically changes the
46
editor appearance based on system settings.
@@ -32,15 +34,32 @@ editor appearance based on system settings.
3234
## 📋 Requirements
3335
Your operating system needs to be:
3436

35-
- a Linux desktop environment that implements
36-
[`org.freedesktop.appearance.color-scheme`](https://github.com/flatpak/xdg-desktop-portal/issues/629),
37-
such as
37+
- a Linux desktop environment that implements [`org.freedesktop.appearance.color-scheme`](https://github.com/flatpak/xdg-desktop-portal/issues/629), such as
3838
- [Gnome](https://gnome.org)
3939
- [KDE](https://kde.org)
4040
- [darkman](https://gitlab.com/WhyNotHugo/darkman) for window managers
4141
- macOS Mojave or newer
4242
- Windows 10 or newer (or WSL)
4343

44+
<details>
45+
<summary>Notes for WSL</summary>
46+
47+
To query the appearance of the Windows host system, two options **must be**
48+
enabled in `/etc/wsl.conf`:
49+
50+
```ini
51+
[automount]
52+
enabled = true
53+
54+
[interop]
55+
enabled = true
56+
```
57+
58+
See [the Microsoft docs](https://learn.microsoft.com/en-us/windows/wsl/wsl-config)
59+
for more information about WSL settings.
60+
61+
</details>
62+
4463
## 📦 Installation
4564

4665
Install the plugin with your preferred package manager:
@@ -107,4 +126,4 @@ You can disable `auto-dark-mode.nvim` at runtime via `lua require('auto-dark-mod
107126

108127

109128
👩‍🎤 Special thanks to [@nekowinston](https://github.com/nekowinston) for
110-
maintaining the plugin 👩‍🎤
129+
maintaining the plugin 👩‍🎤

doc/auto-dark-mode.txt

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,46 @@
33
==============================================================================
44
Table of Contents *auto-dark-mode-table-of-contents*
55

6-
1. auto-dark-mode.nvim |auto-dark-mode-auto-dark-mode.nvim|
7-
- 📋 Requirements |auto-dark-mode-auto-dark-mode.nvim-📋-requirements|
8-
- 📦 Installation |auto-dark-mode-auto-dark-mode.nvim-📦-installation|
9-
- ⚙️ Configuration|auto-dark-mode-auto-dark-mode.nvim-⚙️-configuration|
10-
- 🚀 Usage |auto-dark-mode-auto-dark-mode.nvim-🚀-usage|
11-
- 💖 Contributors |auto-dark-mode-auto-dark-mode.nvim-💖-contributors|
12-
2. Links |auto-dark-mode-links|
13-
14-
==============================================================================
15-
1. auto-dark-mode.nvim *auto-dark-mode-auto-dark-mode.nvim*
16-
6+
- Requirements |auto-dark-mode-requirements|
7+
- Installation |auto-dark-mode-installation|
8+
- Configuration |auto-dark-mode-configuration|
9+
- Usage |auto-dark-mode-usage|
10+
- Contributors |auto-dark-mode-contributors|
11+
1. Links |auto-dark-mode-links|
1712
A Neovim plugin for macOS, Linux, and Windows that automatically changes the
1813
editor appearance based on system settings.
1914

2015

21-
📋 REQUIREMENTS *auto-dark-mode-auto-dark-mode.nvim-📋-requirements*
16+
REQUIREMENTS *auto-dark-mode-requirements*
2217

2318
Your operating system needs to be:
2419

25-
- a Linux desktop environment that implements
26-
`org.freedesktop.appearance.color-scheme` <https://github.com/flatpak/xdg-desktop-portal/issues/629>,
27-
such as
20+
- a Linux desktop environment that implements `org.freedesktop.appearance.color-scheme` <https://github.com/flatpak/xdg-desktop-portal/issues/629>, such as
2821
- Gnome <https://gnome.org>
2922
- KDE <https://kde.org>
3023
- darkman <https://gitlab.com/WhyNotHugo/darkman> for window managers
3124
- macOS Mojave or newer
3225
- Windows 10 or newer (or WSL)
3326

27+
Notes for WSL ~
28+
29+
To query the appearance of the Windows host system, two options **must be**
30+
enabled in `/etc/wsl.conf`
31+
32+
>ini
33+
[automount]
34+
enabled = true
35+
36+
[interop]
37+
enabled = true
38+
<
39+
40+
Seethe Microsoft docs
41+
<https://learn.microsoft.com/en-us/windows/wsl/wsl-config> for more information
42+
about WSL settings.
43+
3444

35-
📦 INSTALLATION *auto-dark-mode-auto-dark-mode.nvim-📦-installation*
45+
INSTALLATION *auto-dark-mode-installation*
3646

3747
Install the plugin with your preferred package manager:
3848

@@ -59,7 +69,7 @@ VIM-PLUG ~
5969
<
6070

6171

62-
⚙️ CONFIGURATION *auto-dark-mode-auto-dark-mode.nvim-⚙️-configuration*
72+
CONFIGURATION *auto-dark-mode-configuration*
6373

6474
**auto-dark-mode** comes with the following defaults:
6575

@@ -90,7 +100,7 @@ be particularly useful to specify a default version when remotely connecting
90100
via SSH, or when using neovim on a tty.
91101

92102

93-
🚀 USAGE *auto-dark-mode-auto-dark-mode.nvim-🚀-usage*
103+
USAGE *auto-dark-mode-usage*
94104

95105

96106
DISABLING AT RUNTIME ~
@@ -99,15 +109,15 @@ You can disable `auto-dark-mode.nvim` at runtime via `lua
99109
require('auto-dark-mode').disable()`.
100110

101111

102-
💖 CONTRIBUTORS *auto-dark-mode-auto-dark-mode.nvim-💖-contributors*
112+
CONTRIBUTORS *auto-dark-mode-contributors*
103113

104114
<https://github.com/f-person/auto-dark-mode.nvim/graphs/contributors>
105115

106-
👩‍🎤 Special thanks to @nekowinston <https://github.com/nekowinston> for
107-
maintaining the plugin 👩‍🎤
116+
Special thanks to @nekowinston <https://github.com/nekowinston> for
117+
maintaining the plugin
108118

109119
==============================================================================
110-
2. Links *auto-dark-mode-links*
120+
1. Links *auto-dark-mode-links*
111121

112122
1. **: https://contrib.rocks/image?repo=f-person/auto-dark-mode.nvim
113123
2. *@nekowinston*:

lua/auto-dark-mode/health.lua

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
local M = {}
2+
3+
local uv = vim.uv or vim.loop
4+
5+
local adm = require("auto-dark-mode")
6+
local interval = require("auto-dark-mode.interval")
7+
8+
M.benchmark = function(iterations)
9+
local result = {
10+
avg = 0,
11+
max = 0,
12+
min = math.huge,
13+
all_results = {},
14+
stdout = nil,
15+
stderr = nil,
16+
parsed = nil,
17+
}
18+
19+
for _ = 1, iterations do
20+
local _start = uv.hrtime()
21+
-- parsing the response is measured, but actually syncing the vim theme isn't performed
22+
local function callback(_stdout, _stderr)
23+
result.stdout = _stdout
24+
result.stderr = _stderr
25+
result.parsed = interval.parse_callback(_stdout, _stderr)
26+
vim.schedule_wrap(function()
27+
vim.print(result)
28+
end)
29+
end
30+
31+
local success, _ = pcall(interval.poll_dark_mode, callback)
32+
-- bail early if polling fails
33+
if not success then
34+
break
35+
end
36+
local _end = uv.hrtime()
37+
38+
table.insert(result.all_results, (_end - _start) / 1e6)
39+
end
40+
41+
local sum = 0
42+
for _, v in pairs(result.all_results) do
43+
result.max = result.max > v and result.max or v
44+
result.min = result.min < v and result.min or v
45+
sum = sum + v
46+
end
47+
result.avg = sum / iterations
48+
49+
return result
50+
end
51+
52+
-- support for neovim < 0.9.0
53+
local H = vim.health
54+
local health = {}
55+
health.start = H.start or H.report_start
56+
health.ok = H.ok or H.report_ok
57+
health.info = H.info or H.report_info
58+
health.error = H.error or H.report_error
59+
60+
M.check = function()
61+
health.start("auto-dark-mode.nvim")
62+
63+
if adm.state.setup_correct then
64+
health.ok("Setup is correct")
65+
else
66+
health.error("Setup is incorrect")
67+
end
68+
69+
health.info(string.format("Detected operating system: %s", adm.state.system))
70+
health.info(string.format("Using query command: `%s`", table.concat(adm.state.query_command, " ")))
71+
72+
interval.poll_dark_mode(function(stdout, stderr)
73+
vim.schedule(function()
74+
health.info(string.format("Query response:\nstdout: %s\nstderr: %s\n", stdout, stderr))
75+
end)
76+
end)
77+
78+
local benchmark = M.benchmark(30)
79+
health.info(
80+
string.format("Benchmark: %.2fms avg / %.2fms min / %.2fms max", benchmark.avg, benchmark.min, benchmark.max)
81+
)
82+
83+
local update_interval = adm.options.update_interval
84+
local ratio = update_interval / benchmark.avg
85+
local msg = {
86+
info = string.format("Update interval (%dms) is %.2fx the average query time", update_interval, ratio),
87+
error = string.format(
88+
"Update interval (%dms) seems too short compared to current benchmarks, consider increasing it",
89+
update_interval
90+
),
91+
}
92+
93+
if ratio > 30 then
94+
health.ok(msg.info)
95+
elseif ratio > 5 then
96+
health.warn(msg.info)
97+
else
98+
health.error(msg.error)
99+
end
100+
end
101+
102+
return M

0 commit comments

Comments
 (0)