Skip to content

Commit 6809e40

Browse files
authored
refactor: use libuv directly, drop dependency on fwatch.nvim (#13)
Use `vim.uv` wrapper of `libuv`
1 parent e05cd4a commit 6809e40

File tree

4 files changed

+55
-75
lines changed

4 files changed

+55
-75
lines changed

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,6 @@ tinted.setup(nil, {
167167
})
168168
```
169169

170-
The live-reload feature depends on [fwatch.nvim]. Add this dependency in whatever manner your plugin manager allows you.
171-
For example, with **lazy.nvim**:
172-
173-
```diff
174-
return {
175-
"tinted-theming/tinted-nvim",
176-
+ dependencies = {
177-
+ { "rktjmp/fwatch.nvim" },
178-
+ }
179-
}
180-
```
181-
182170
> [!NOTE]
183171
> If you don't see colours, try adding `vim.opt.termguicolors = true` to
184172
> your init.lua
@@ -195,7 +183,7 @@ require('tinted-colorscheme').with_config({
195183
supports = {
196184
tinty = true,
197185
tinted_shell = false,
198-
live_reload = false -- If set to true, requires rktjump/fwatch.nvim as a dependency
186+
live_reload = true,
199187
},
200188
highlights = {
201189
telescope = true,
@@ -209,15 +197,22 @@ require('tinted-colorscheme').with_config({
209197
})
210198
```
211199

212-
### Autocmd
200+
### Live Reload
213201

214-
```lua
202+
If you have both `tinty` and `live_reload` enabled, the colorscheme will automatically update whenever you change
203+
your theme system-wide with `tinty apply <SCHEME>`.
215204

205+
The user event `TintedColorsPost` is triggered whenever the colorscheme changes. This is useful for when you want to
206+
do something whenever the colorscheme live-reloads for example, such as applying some highlights yourself. You're able
207+
to access the color-table from the `tinted-colorscheme` module:
208+
209+
```lua
216210
vim.api.nvim_create_autocmd("User", {
217211
pattern = "TintedColorsPost",
218212
callback = function()
219213
-- Do things whenever the theme changes.
220214
local colors = require("tinted-colorscheme").colors
215+
-- e.g. colors.base00, colors.base01, ... colors.base0F
221216
end,
222217
})
223218
```
@@ -685,4 +680,3 @@ contributors for work they've done.
685680
[Tinty]: https://github.com/tinted-theming/tinty
686681
[Tinted Gallery]: https://tinted-theming.github.io/tinted-gallery/
687682
[base16-nvim]: https://github.com/RRethy/base16-nvim
688-
[fwatch.nvim]: https://github.com/rktjump/fwatch.nvim

lua/tinted-colorscheme.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
local default_config = {
3333
supports = {
3434
tinty = true,
35-
live_reload = false,
35+
live_reload = true,
3636
tinted_shell = false,
3737
},
3838
highlights = {

lua/tinted-live-reload.lua

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
local uv = require("luv")
2-
31
local M = {}
42

5-
local function check_prerequisites()
6-
local ok = pcall(require, "fwatch")
7-
if not ok then
8-
vim.notify("Required plugin rktjmp/fwatch.nvim is missing.", vim.log.levels.ERROR)
9-
return false
10-
end
11-
return true
12-
end
13-
14-
local function schedule_trigger(callback)
15-
vim.schedule(callback)
16-
end
17-
183
local function start_watcher(callback)
194
if vim.g.tinted_live_reload_registered then
205
return
216
end
227
vim.g.tinted_live_reload_registered = true
238

24-
local fwatch = require("fwatch")
25-
269
-- The first line of the output is the path to the data directory.
2710
-- We need to watch the "current_scheme" file in that directory.
2811
-- The output is a list of lines, so we take the first line.
2912
-- The path is trimmed to remove any leading or trailing whitespace.
3013
-- We check if the path is not empty.
31-
local job = vim.fn.jobstart(
14+
return vim.fn.jobstart(
3215
{ "tinty", "config", "--data-dir-path" },
3316
{
3417
on_stdout = function(_, data)
@@ -44,36 +27,45 @@ local function start_watcher(callback)
4427
vim.notify(string.format("File %s is not readable.", file_path), vim.log.levels.ERROR)
4528
return
4629
end
47-
fwatch.watch(file_path, {
48-
on_event = function(_, events, unwatch)
49-
schedule_trigger(callback)
50-
-- Tinty 0.28+ will no longer update the current_scheme file in place. It will replace it
51-
-- with a new file which breaks this file watcher. We'll check whether the file moves,
52-
-- at which point we'll start a new watcher.
53-
if events.rename == true then
54-
-- Trigger the change
55-
vim.schedule(function()
56-
-- Stop the existing watcher.
57-
unwatch()
58-
-- Reset the idempotency tracker
59-
vim.g.tinted_live_reload_registered = false
60-
-- Start a new watcher
61-
start_watcher(callback)
62-
end)
63-
end
64-
end,
65-
})
30+
31+
32+
local handle = vim.uv.new_fs_event()
33+
34+
if handle == nil then
35+
vim.notify("Unable to start watcher.", vim.log.levels.ERROR)
36+
return
37+
end
38+
39+
--- @link https://docs.libuv.org/en/v1.x/fs_event.html
40+
local flags = {
41+
watch_entry = false,
42+
stat = false,
43+
recursive = false,
44+
}
45+
46+
-- attach handler
47+
vim.uv.fs_event_start(handle, file_path, flags, function(_, _, events)
48+
vim.schedule(callback)
49+
-- Tinty 0.28+ will no longer update the current_scheme file in place. It will replace it
50+
-- with a new file which breaks this file watcher. We'll check whether the file moves,
51+
-- at which point we'll start a new watcher.
52+
if events.rename == true then
53+
-- Stop the existing watcher.
54+
vim.uv.fs_event_stop(handle)
55+
vim.schedule(function()
56+
-- Reset the idempotency tracker
57+
vim.g.tinted_live_reload_registered = false
58+
-- Start a new watcher
59+
start_watcher(callback)
60+
end)
61+
end
62+
end)
6663
end,
6764
}
6865
)
6966
end
7067

7168
M.setup_live_reload = function(callback)
72-
local ok = check_prerequisites()
73-
if not ok then
74-
-- Required plugins aren't present. Stop.
75-
return
76-
end
7769
start_watcher(callback)
7870
end
7971

templates/README.md.mustache

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,6 @@ tinted.setup(nil, {
167167
})
168168
```
169169

170-
The live-reload feature depends on [fwatch.nvim]. Add this dependency in whatever manner your plugin manager allows you.
171-
For example, with **lazy.nvim**:
172-
173-
```diff
174-
return {
175-
"tinted-theming/tinted-nvim",
176-
+ dependencies = {
177-
+ { "rktjmp/fwatch.nvim" },
178-
+ }
179-
}
180-
```
181-
182170
> [!NOTE]
183171
> If you don't see colours, try adding `vim.opt.termguicolors = true` to
184172
> your init.lua
@@ -195,7 +183,7 @@ require('tinted-colorscheme').with_config({
195183
supports = {
196184
tinty = true,
197185
tinted_shell = false,
198-
live_reload = false -- If set to true, requires rktjump/fwatch.nvim as a dependency
186+
live_reload = true,
199187
},
200188
highlights = {
201189
telescope = true,
@@ -209,15 +197,22 @@ require('tinted-colorscheme').with_config({
209197
})
210198
```
211199

212-
### Autocmd
200+
### Live Reload
213201

214-
```lua
202+
If you have both `tinty` and `live_reload` enabled, the colorscheme will automatically update whenever you change
203+
your theme system-wide with `tinty apply <SCHEME>`.
215204

205+
The user event `TintedColorsPost` is triggered whenever the colorscheme changes. This is useful for when you want to
206+
do something whenever the colorscheme live-reloads for example, such as applying some highlights yourself. You're able
207+
to access the color-table from the `tinted-colorscheme` module:
208+
209+
```lua
216210
vim.api.nvim_create_autocmd("User", {
217211
pattern = "TintedColorsPost",
218212
callback = function()
219213
-- Do things whenever the theme changes.
220214
local colors = require("tinted-colorscheme").colors
215+
-- e.g. colors.base00, colors.base01, ... colors.base0F
221216
end,
222217
})
223218
```
@@ -241,4 +236,3 @@ contributors for work they've done.
241236
[Tinty]: https://github.com/tinted-theming/tinty
242237
[Tinted Gallery]: https://tinted-theming.github.io/tinted-gallery/
243238
[base16-nvim]: https://github.com/RRethy/base16-nvim
244-
[fwatch.nvim]: https://github.com/rktjump/fwatch.nvim

0 commit comments

Comments
 (0)