nirish

config files for niri window manager
Log | Files | Refs | README | LICENSE

ls.lua (1829B)


      1 --[[
      2     An addon for mpv-file-browser which uses the Linux ls command to parse native directories
      3     This behaves near identically to the native parser, but IO is done asynchronously.
      4 
      5     Available at: https://github.com/CogentRedTester/mpv-file-browser/tree/master/addons
      6 ]]--
      7 
      8 local mp = require "mp"
      9 local msg = require "mp.msg"
     10 local fb = require "file-browser"
     11 
     12 local PLATFORM = fb.get_platform()
     13 
     14 ---@type ParserConfig
     15 local ls = {
     16     priority = 109,
     17     api_version = "1.9.0",
     18     name = "ls",
     19     keybind_name = "file"
     20 }
     21 
     22 ---@async
     23 ---@param args string[]
     24 ---@param parse_state ParseState
     25 ---@return string|nil
     26 local function command(args, parse_state)
     27     local async = mp.command_native_async({
     28             name = "subprocess",
     29             playback_only = false,
     30             capture_stdout = true,
     31             capture_stderr = true,
     32             args = args
     33         }, fb.coroutine.callback(30))
     34 
     35     ---@type boolean, boolean, MPVSubprocessResult
     36     local completed, _, cmd = parse_state:yield()
     37     if not completed then
     38         msg.warn('read timed out for:', table.unpack(args))
     39         mp.abort_async_command(async)
     40         return nil
     41     end
     42 
     43     return cmd.status == 0 and cmd.stdout or nil
     44 end
     45 
     46 function ls:can_parse(directory)
     47     if not fb.get_opt('ls_parser') then return false end
     48     return PLATFORM ~= 'windows' and directory ~= '' and not fb.get_protocol(directory)
     49 end
     50 
     51 ---@async
     52 function ls:parse(directory, parse_state)
     53     local list = {}
     54     local files = command({"ls", "-1", "-p", "-A", "-N", "--zero", "-L", directory}, parse_state)
     55 
     56     if not files then return nil end
     57 
     58     for str in files:gmatch("%Z+") do
     59         local is_dir = str:sub(-1) == "/"
     60         msg.trace(str)
     61 
     62         table.insert(list, {name = str, type = is_dir and "dir" or "file"})
     63     end
     64 
     65     return list
     66 end
     67 
     68 return ls