winroot.lua (1628B)
1 --[[ 2 This file is an internal file-browser addon. 3 It should not be imported like a normal module. 4 5 Automatically populates the root with windows drives on startup. 6 Ctrl+r will add new drives mounted since startup. 7 8 Drives will only be added if they are not already present in the root. 9 ]] 10 11 local mp = require 'mp' 12 local msg = require 'mp.msg' 13 local fb = require 'file-browser' 14 15 local PLATFORM = fb.get_platform() 16 17 ---returns a list of windows drives 18 ---@return string[]? 19 local function get_drives() 20 ---@type MPVSubprocessResult?, string? 21 local result, err = mp.command_native({ 22 name = 'subprocess', 23 playback_only = false, 24 capture_stdout = true, 25 args = {'fsutil', 'fsinfo', 'drives'} 26 }) 27 if not result then return msg.error(err) end 28 if result.status ~= 0 then return msg.error('could not read windows root') end 29 30 local root = {} 31 for drive in result.stdout:gmatch("(%a:)\\") do 32 table.insert(root, drive..'/') 33 end 34 return root 35 end 36 37 -- adds windows drives to the root if they are not already present 38 local function import_drives() 39 if fb.get_opt('auto_detect_windows_drives') and PLATFORM ~= 'windows' then return end 40 41 local drives = get_drives() 42 if not drives then return end 43 44 for _, drive in ipairs(drives) do 45 fb.register_root_item(drive) 46 end 47 end 48 49 local keybind = { 50 key = 'Ctrl+r', 51 name = 'import_root_drives', 52 command = import_drives, 53 parser = 'root', 54 passthrough = true 55 } 56 57 ---@type ParserConfig 58 return { 59 api_version = '1.9.0', 60 setup = import_drives, 61 keybinds = { keybind } 62 }