robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
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 ) 18local info =vim . fn . swapinfo ( path ) 19local mtime =info . mtime and vim . fn . strftime ( '%a %b %d %H:%M:%S %Y' , info . mtime ) or '?' 20local lines ={ 21vim . fs . basename ( path ), 22( ' dated: %s' ): format ( mtime ), 23} 24if info . error then 25lines [ # lines + 1 ] =( ' [%s]' ): format ( info . error ) 26else 27lines [ # lines + 1 ] =( ' file name: %s' ): format ( 28info . fname == '' and '[No Name]' or info . fname 29) 30lines [ # lines + 1 ] =( ' modified: %s' ): format ( info . dirty == 1 and 'YES' or 'no' ) 31if info . user ~= '' or info . host ~= '' then 32local parts ={} ---@type string[] 33if info . user ~= '' then 34parts [ # parts + 1 ] =( 'user name: %s' ): format ( info . user ) 35end 36if info . host ~= '' then 37parts [ # parts + 1 ] =( 'host name: %s' ): format ( info . host ) 38end 39lines [ # lines + 1 ] =( ' %s' ): format ( table . concat ( parts , ' ' )) 40end 41if info . pid > 0 then 42lines [ # lines + 1 ] =( ' process ID: %d (STILL RUNNING)' ): format ( info . pid ) 43end 44end 45return 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 ) 54vim . ui . select ( items , { 55prompt =N_ ( 'Select a swapfile:' ), 56kind ='swap' , 57format_item =format_swap , 58} , function ( _ , idx ) 59if not idx then 60return 61end 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". 64vim . fn . feedkeys ( 65vim . keycode (( '<Cmd>recover! %s<CR>' ): format ( vim . fn . fnameescape ( assert ( items [ idx ])))), 66'in' 67) 68end ) 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 ) 75local lines ={ { N_ ( 'Swap files found:' ) .. '\n' } } ---@type [string][] 76if # items == 0 then 77lines [ # lines + 1 ] ={ ' ' .. N_ ( '-- none --' ) } 78else 79for i , path in ipairs ( items ) do 80lines [ # lines + 1 ] ={ ( '%d. %s\n' ): format ( i , format_swap ( path )) } 81end 82end 83api . nvim_echo ( lines , false ) 84end 85 86return M