-
Notifications
You must be signed in to change notification settings - Fork 8
/
powershell.lua
81 lines (66 loc) · 2.35 KB
/
powershell.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
--[[
An addon for mpv-file-browser which uses powershell commands to parse native directories
This is slower than the default parser for local drives, but faster for network drives
The drive_letters array below is used to list the drives to use this parser for
]]--
--list the drive letters to use here (case sensitive)
local drive_letters = {
"Y", "Z"
}
local mp = require "mp"
local msg = require "mp.msg"
local fb = require "file-browser"
local wn = {
priority = 109,
version = "1.1.0",
name = "powershell",
keybind_name = "file"
}
local drives = {}
for _, letter in ipairs(drive_letters) do
drives[letter] = true
end
local function command(args, parse_state)
local _, cmd = parse_state:yield(
mp.command_native_async({
name = "subprocess",
playback_only = false,
capture_stdout = true,
capture_stderr = true,
args = args
}, fb.coroutine.callback())
)
return cmd.status == 0 and cmd.stdout or nil, cmd.stderr
end
function wn:can_parse(directory)
return not self.get_protocol(directory) and drives[ directory:sub(1,1) ]
end
function wn:parse(directory, parse_state)
local list = {}
local files, err = command({"powershell", "-noprofile", "-command", [[
$dirs = Get-ChildItem -LiteralPath ]]..string.format("%q", directory)..[[ -Directory
$files = Get-ChildItem -LiteralPath ]]..string.format("%q", directory)..[[ -File
foreach ($n in $dirs.Name) {
$n += "/"
$u8clip = [System.Text.Encoding]::UTF8.GetBytes($n)
[Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
Write-Host ""
}
foreach ($n in $files.Name) {
$u8clip = [System.Text.Encoding]::UTF8.GetBytes($n)
[Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
Write-Host ""
}
]]}, parse_state)
if not files then msg.debug(err) ; return nil end
for str in files:gmatch("[^\n\r]+") do
local is_dir = str:sub(-1) == "/"
if is_dir and self.valid_dir(str) then
table.insert(list, {name = str, type = "dir"})
elseif self.valid_file(str) then
table.insert(list, {name = str, type = "file"})
end
end
return self.sort(list), {filtered = true, sorted = true}
end
return wn