Handle renaming documents

This commit is contained in:
Catalin Constantin Mititiuc 2025-01-20 14:55:24 -08:00
parent f85f240e2b
commit a244265018
2 changed files with 9 additions and 7 deletions

View File

@ -3,6 +3,8 @@ defmodule Pandoc.Watcher do
use GenServer use GenServer
@events MapSet.new([:closed, :deleted, :moved_from, :moved_to])
def start_link(args) do def start_link(args) do
GenServer.start_link(__MODULE__, args) GenServer.start_link(__MODULE__, args)
end end
@ -18,12 +20,8 @@ defmodule Pandoc.Watcher do
@impl true @impl true
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
case {String.match?(path, state[:pattern]), :closed in events or :deleted in events} do if String.match?(path, state[:pattern]) and update?(events) do
{true, true} ->
Pandoc.install_and_run(state[:profile], [Path.basename(path) | state[:extra_args]]) Pandoc.install_and_run(state[:profile], [Path.basename(path) | state[:extra_args]])
_ ->
nil
end end
{:noreply, state} {:noreply, state}
@ -32,4 +30,8 @@ defmodule Pandoc.Watcher do
def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do
{:noreply, state} {:noreply, state}
end end
defp update?(events) do
events |> MapSet.new() |> MapSet.intersection(@events) |> MapSet.size() > 0
end
end end

View File

@ -1,7 +1,7 @@
defmodule Pandoc.MixProject do defmodule Pandoc.MixProject do
use Mix.Project use Mix.Project
@version "0.3.1" @version "0.3.2"
@source_url "https://webdevcat.me/git/pandoc/" @source_url "https://webdevcat.me/git/pandoc/"
def project do def project do