robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
2.7 KiB86 linesraw
1local api = vim.api
2local N_ = vim.fn.gettext
3
4local M = {}
5
6--- Renders a swap file as a multi-line block:
7--- ```
8--- %home%foo%bar%README.md.swl
9---              dated: Thu Apr 23 17:25:52 2026
10---          file name: ~foo/bar/README.md
11---           modified: no
12---          user name: justin   host name: minime
13---         process ID: 10521 (STILL RUNNING)
14--- ```
15--- @param path string
16--- @return string
17local function format_swap(path)
18  local info = vim.fn.swapinfo(path)
19  local mtime = info.mtime and vim.fn.strftime('%a %b %d %H:%M:%S %Y', info.mtime) or '?'
20  local lines = {
21    vim.fs.basename(path),
22    ('             dated: %s'):format(mtime),
23  }
24  if info.error then
25    lines[#lines + 1] = ('         [%s]'):format(info.error)
26  else
27    lines[#lines + 1] = ('         file name: %s'):format(
28      info.fname == '' and '[No Name]' or info.fname
29    )
30    lines[#lines + 1] = ('          modified: %s'):format(info.dirty == 1 and 'YES' or 'no')
31    if info.user ~= '' or info.host ~= '' then
32      local parts = {} ---@type string[]
33      if info.user ~= '' then
34        parts[#parts + 1] = ('user name: %s'):format(info.user)
35      end
36      if info.host ~= '' then
37        parts[#parts + 1] = ('host name: %s'):format(info.host)
38      end
39      lines[#lines + 1] = ('         %s'):format(table.concat(parts, '   '))
40    end
41    if info.pid > 0 then
42      lines[#lines + 1] = ('        process ID: %d (STILL RUNNING)'):format(info.pid)
43    end
44  end
45  return table.concat(lines, '\n')
46end
47
48--- Implements `:recover` (when there are multiple swap files): let the user pick via vim.ui.select().
49---
50--- async: returns immediately, then schedules `:recover {path}` on the chosen swapfile.
51---
52--- @param items string[] List of swapfile paths.
53function M.select_swap(items)
54  vim.ui.select(items, {
55    prompt = N_('Select a swapfile:'),
56    kind = 'swap',
57    format_item = format_swap,
58  }, function(_, idx)
59    if not idx then
60      return
61    end
62    -- Queue ":recover! <swapfile>" as user input, so the recursive recovery runs via the normal
63    -- input-dispatch loop. Using vim.schedule + vim.cmd can hang bc of "Press ENTER".
64    vim.fn.feedkeys(
65      vim.keycode(('<Cmd>recover! %s<CR>'):format(vim.fn.fnameescape(assert(items[idx])))),
66      'in'
67    )
68  end)
69end
70
71--- Implements `nvim -r` (no arg): list every swapfile found in 'directory'.
72---
73--- @param items string[] List of swapfile paths.
74function M.list_swaps(items)
75  local lines = { { N_('Swap files found:') .. '\n' } } ---@type [string][]
76  if #items == 0 then
77    lines[#lines + 1] = { '   ' .. N_('-- none --') }
78  else
79    for i, path in ipairs(items) do
80      lines[#lines + 1] = { ('%d. %s\n'):format(i, format_swap(path)) }
81    end
82  end
83  api.nvim_echo(lines, false)
84end
85
86return M