|
| 1 | +-- Check if `vfox` command exists |
| 2 | +local function is_vfox_available() |
| 3 | + local check = os.execute("vfox --version >nul 2>&1") |
| 4 | + return check == 0 |
| 5 | +end |
| 6 | + |
| 7 | +if not is_vfox_available() then |
| 8 | + return |
| 9 | +end |
| 10 | + |
1 | 11 | -- https://chrisant996.github.io/clink/clink.html#extending-clink
|
| 12 | +-- https://github.com/version-fox/vfox/blob/main/internal/shell/clink_vfox.lua |
2 | 13 | local vfox_sdk_table = {}
|
| 14 | + |
3 | 15 | clink.argmatcher('vfox'):nofiles():setdelayinit(function(vfox)
|
4 |
| - if #vfox_sdk_table ~= 0 then |
5 |
| - return |
6 |
| - end |
| 16 | + if #vfox_sdk_table ~= 0 then |
| 17 | + return |
| 18 | + end |
7 | 19 |
|
8 |
| - local current_timestamp = os.time() |
9 |
| - local file_name = os.getenv('USERPROFILE') .. '\\vfox_available.txt' |
10 |
| - local file_available = io.open(file_name, 'r') |
11 |
| - if file_available then |
12 |
| - local file_timestamp = tonumber(file_available:read('*l')) |
13 |
| - if current_timestamp - file_timestamp <= 24 * 60 * 60 then |
14 |
| - for line in file_available:lines() do |
15 |
| - table.insert(vfox_sdk_table, line) |
16 |
| - end |
17 |
| - end |
18 |
| - file_available:close() |
19 |
| - end |
| 20 | + local current_timestamp = os.time() |
| 21 | + local file_name = os.getenv('USERPROFILE') .. '\\vfox_available.txt' |
| 22 | + local file_available = io.open(file_name, 'r') |
| 23 | + if file_available then |
| 24 | + local file_timestamp = tonumber(file_available:read('*l')) |
| 25 | + if current_timestamp - file_timestamp <= 24 * 60 * 60 then |
| 26 | + for line in file_available:lines() do |
| 27 | + table.insert(vfox_sdk_table, line) |
| 28 | + end |
| 29 | + end |
| 30 | + file_available:close() |
| 31 | + end |
20 | 32 |
|
21 |
| - if #vfox_sdk_table == 0 then |
22 |
| - file_available = io.open(file_name, 'w') |
23 |
| - file_available:write(current_timestamp .. '\n') |
24 |
| - local vfox_available = io.popen('vfox available') |
25 |
| - for line in vfox_available:lines() do |
26 |
| - local trim = line:gsub('%c%[%d+m', '') |
27 |
| - local name = trim:match('^(%S+)') |
28 |
| - if name and (trim:find('YES') or trim:find('NO')) then |
29 |
| - table.insert(vfox_sdk_table, name) |
30 |
| - file_available:write(name .. '\n') |
31 |
| - end |
32 |
| - end |
33 |
| - vfox_available:close() |
34 |
| - file_available:close() |
35 |
| - end |
| 33 | + if #vfox_sdk_table == 0 then |
| 34 | + file_available = io.open(file_name, 'w') |
| 35 | + file_available:write(current_timestamp .. '\n') |
| 36 | + local vfox_available = io.popen('vfox available') |
| 37 | + for line in vfox_available:lines() do |
| 38 | + local trim = line:gsub('%c%[%d+m', '') |
| 39 | + local name = trim:match('^(%S+)') |
| 40 | + if name and (trim:find('YES') or trim:find('NO')) then |
| 41 | + table.insert(vfox_sdk_table, name) |
| 42 | + file_available:write(name .. '\n') |
| 43 | + end |
| 44 | + end |
| 45 | + vfox_available:close() |
| 46 | + file_available:close() |
| 47 | + end |
36 | 48 |
|
37 |
| - local function vfox_ls_func() |
38 |
| - local pre, ls = '', {} |
39 |
| - local vfox_ls = io.popen('vfox ls') |
40 |
| - for line in vfox_ls:lines() do |
41 |
| - local txt = line:gsub('%c%[%d+m', ''):match('^%A+(%a.+)') |
42 |
| - if txt then |
43 |
| - if txt:find('v') == 1 then |
44 |
| - ls[pre] = true |
45 |
| - table.insert(ls, pre .. '@' .. txt:sub(2)) |
46 |
| - else |
47 |
| - pre = txt |
48 |
| - ls[pre] = false |
49 |
| - end |
50 |
| - end |
51 |
| - end |
52 |
| - vfox_ls:close() |
53 |
| - return ls |
54 |
| - end |
55 |
| - local function vfox_sdk_func() |
56 |
| - local ls, res = vfox_ls_func(), {} |
57 |
| - for k, v in pairs(ls) do |
58 |
| - if type(v) == 'boolean' then |
59 |
| - table.insert(res, k) |
60 |
| - end |
61 |
| - end |
62 |
| - return res |
63 |
| - end |
| 49 | + local function vfox_ls_func() |
| 50 | + local pre, ls = '', {} |
| 51 | + local vfox_ls = io.popen('vfox ls') |
| 52 | + for line in vfox_ls:lines() do |
| 53 | + local txt = line:gsub('%c%[%d+m', ''):match('^%A+(%a.+)') |
| 54 | + if txt then |
| 55 | + if txt:find('v') == 1 then |
| 56 | + ls[pre] = true |
| 57 | + table.insert(ls, pre .. '@' .. txt:sub(2)) |
| 58 | + else |
| 59 | + pre = txt |
| 60 | + ls[pre] = false |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + vfox_ls:close() |
| 65 | + return ls |
| 66 | + end |
| 67 | + local function vfox_sdk_func() |
| 68 | + local ls, res = vfox_ls_func(), {} |
| 69 | + for k, v in pairs(ls) do |
| 70 | + if type(v) == 'boolean' then |
| 71 | + table.insert(res, k) |
| 72 | + end |
| 73 | + end |
| 74 | + return res |
| 75 | + end |
64 | 76 |
|
65 |
| - local vfox_sdk = clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--help', '-h') |
66 |
| - local vfox_use = clink.argmatcher():nofiles():addarg(function() |
67 |
| - local ls, res = vfox_ls_func(), {} |
68 |
| - for k, v in pairs(ls) do |
69 |
| - if v then |
70 |
| - table.insert(res, v == true and k or v) |
71 |
| - end |
72 |
| - end |
73 |
| - return res |
74 |
| - end):addflags('--global', '-g', '--session', '-s', '--project', '-p', '--help', '-h') |
75 |
| - local vfox_help = clink.argmatcher():nofiles():addflags('--help', '-h') |
76 |
| - local vfox_shell = clink.argmatcher():nofiles():addarg('bash', 'zsh', 'pwsh', 'fish', 'clink') |
77 |
| - local vfox_uninstall = clink.argmatcher():nofiles():addarg(vfox_ls_func):addflags('--help', '-h') |
78 |
| - local vfox_install = clink.argmatcher():nofiles():addarg({ |
79 |
| - onadvance = function() return 0 end, |
80 |
| - vfox_sdk_func, |
81 |
| - }):addflags('--all', '-a', '--help', '-h') |
| 77 | + local vfox_sdk = clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--help', '-h') |
| 78 | + local vfox_use = clink.argmatcher():nofiles():addarg(function() |
| 79 | + local ls, res = vfox_ls_func(), {} |
| 80 | + for k, v in pairs(ls) do |
| 81 | + if v then |
| 82 | + table.insert(res, v == true and k or v) |
| 83 | + end |
| 84 | + end |
| 85 | + return res |
| 86 | + end):addflags('--global', '-g', '--session', '-s', '--project', '-p', '--help', '-h') |
| 87 | + local vfox_help = clink.argmatcher():nofiles():addflags('--help', '-h') |
| 88 | + local vfox_shell = clink.argmatcher():nofiles():addarg('bash', 'zsh', 'pwsh', 'fish', 'clink') |
| 89 | + local vfox_uninstall = clink.argmatcher():nofiles():addarg(vfox_ls_func):addflags('--help', '-h') |
| 90 | + local vfox_install = clink.argmatcher():nofiles():addarg({ |
| 91 | + onadvance = function() return 0 end, |
| 92 | + vfox_sdk_func, |
| 93 | + }):addflags('--all', '-a', '--help', '-h') |
82 | 94 |
|
83 |
| - vfox:addarg( |
84 |
| - 'add' .. clink.argmatcher():nofiles():addarg({ |
85 |
| - onadvance = function() return 0 end, |
86 |
| - function(word, word_index, line_state) |
87 |
| - local res, line = {}, line_state:getline() |
88 |
| - for _, v in ipairs(vfox_sdk_table) do |
89 |
| - if not line:find(v) then |
90 |
| - table.insert(res, v) |
91 |
| - end |
92 |
| - end |
93 |
| - return res |
94 |
| - end |
95 |
| - }):addflags('--source', '-s', '--alias', '--help', '-h'), |
96 |
| - 'use' .. vfox_use, 'u' .. vfox_use, |
97 |
| - 'info' .. vfox_sdk, |
98 |
| - 'remove' .. vfox_sdk, |
99 |
| - 'search' .. vfox_sdk, |
100 |
| - 'update' .. clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--all', '-a', '--help', '-h'), |
101 |
| - 'available' .. vfox_help, |
102 |
| - 'upgrade' .. vfox_help, |
103 |
| - 'current' .. vfox_sdk, 'c' .. vfox_sdk, |
104 |
| - 'list' .. vfox_sdk, 'ls' .. vfox_sdk, |
105 |
| - 'uninstall' .. vfox_uninstall, 'un' .. vfox_uninstall, |
106 |
| - 'install' .. vfox_install, 'i' .. vfox_install, |
107 |
| - 'env' .. clink.argmatcher():nofiles():addflags( |
108 |
| - '--shell' .. vfox_shell, '-s' .. vfox_shell, |
109 |
| - '--cleanup', '-c', |
110 |
| - '--json', '-j', |
111 |
| - '--help', '-h' |
112 |
| - ), |
113 |
| - 'activate' .. vfox_shell, |
114 |
| - 'config' .. clink.argmatcher():nofiles():addarg(function() |
115 |
| - local res, vfox_config = {}, io.popen('vfox config -l') |
116 |
| - for line in vfox_config:lines() do |
117 |
| - local txt = line:gsub('%c%[%d+m', ''):match('^(%S+)') |
118 |
| - if txt then |
119 |
| - table.insert(res, txt) |
120 |
| - end |
121 |
| - end |
122 |
| - vfox_config:close() |
123 |
| - return res |
124 |
| - end):addflags('--list', '-l', '--unset', '-un', '--help', '-h'), |
125 |
| - 'cd' .. clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--plugin', '-p', '--help', '-h'), |
126 |
| - 'help', 'h' |
127 |
| - ):addflags('--debug', '--help', '-h', '--version', '-v', '-V') |
| 95 | + vfox:addarg( |
| 96 | + 'add' .. clink.argmatcher():nofiles():addarg({ |
| 97 | + onadvance = function() return 0 end, |
| 98 | + function(word, word_index, line_state) |
| 99 | + local res, line = {}, line_state:getline() |
| 100 | + for _, v in ipairs(vfox_sdk_table) do |
| 101 | + if not line:find(v) then |
| 102 | + table.insert(res, v) |
| 103 | + end |
| 104 | + end |
| 105 | + return res |
| 106 | + end |
| 107 | + }):addflags('--source', '-s', '--alias', '--help', '-h'), |
| 108 | + 'use' .. vfox_use, 'u' .. vfox_use, |
| 109 | + 'info' .. vfox_sdk, |
| 110 | + 'remove' .. vfox_sdk, |
| 111 | + 'search' .. vfox_sdk, |
| 112 | + 'update' .. clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--all', '-a', '--help', '-h'), |
| 113 | + 'available' .. vfox_help, |
| 114 | + 'upgrade' .. vfox_help, |
| 115 | + 'current' .. vfox_sdk, 'c' .. vfox_sdk, |
| 116 | + 'list' .. vfox_sdk, 'ls' .. vfox_sdk, |
| 117 | + 'uninstall' .. vfox_uninstall, 'un' .. vfox_uninstall, |
| 118 | + 'install' .. vfox_install, 'i' .. vfox_install, |
| 119 | + 'env' .. clink.argmatcher():nofiles():addflags( |
| 120 | + '--shell' .. vfox_shell, '-s' .. vfox_shell, |
| 121 | + '--cleanup', '-c', |
| 122 | + '--json', '-j', |
| 123 | + '--help', '-h' |
| 124 | + ), |
| 125 | + 'activate' .. vfox_shell, |
| 126 | + 'config' .. clink.argmatcher():nofiles():addarg(function() |
| 127 | + local res, vfox_config = {}, io.popen('vfox config -l') |
| 128 | + for line in vfox_config:lines() do |
| 129 | + local txt = line:gsub('%c%[%d+m', ''):match('^(%S+)') |
| 130 | + if txt then |
| 131 | + table.insert(res, txt) |
| 132 | + end |
| 133 | + end |
| 134 | + vfox_config:close() |
| 135 | + return res |
| 136 | + end):addflags('--list', '-l', '--unset', '-un', '--help', '-h'), |
| 137 | + 'cd' .. clink.argmatcher():nofiles():addarg(vfox_sdk_func):addflags('--plugin', '-p', '--help', '-h'), |
| 138 | + 'help', 'h' |
| 139 | + ):addflags('--debug', '--help', '-h', '--version', '-v', '-V') |
128 | 140 | end)
|
129 | 141 |
|
130 | 142 | local vfox_setenv = function(str)
|
131 |
| - local key, val = str:match('^set "(.+)=(.*)"') |
132 |
| - if key and val then |
133 |
| - return os.setenv(key, val ~= '' and val or nil) |
134 |
| - end |
| 143 | + local key, val = str:match('^set "(.+)=(.*)"') |
| 144 | + if key and val then |
| 145 | + return os.setenv(key, val ~= '' and val or nil) |
| 146 | + end |
135 | 147 | end
|
136 | 148 |
|
| 149 | +local vfox_filter = function() end |
137 | 150 | local vfox_task = coroutine.create(function()
|
138 |
| - os.setenv('__VFOX_PID', os.getpid()) |
139 |
| - local vfox_activate = io.popen('vfox activate clink') |
140 |
| - for line in vfox_activate:lines() do |
141 |
| - vfox_setenv(line) |
142 |
| - end |
143 |
| - vfox_activate:close() |
144 |
| - |
145 |
| - local cleanup = coroutine.create(function() |
146 |
| - os.execute('vfox env -c') |
147 |
| - end) |
148 |
| - coroutine.resume(cleanup) |
| 151 | + os.setenv('__VFOX_PID', os.getpid()) |
| 152 | + os.setenv('__VFOX_CURTMPPATH', nil) |
| 153 | + local vfox_activate = io.popen('vfox activate clink') |
| 154 | + for line in vfox_activate:lines() do |
| 155 | + vfox_setenv(line) |
| 156 | + end |
| 157 | + vfox_activate:close() |
| 158 | + os.execute('vfox env -c') |
| 159 | + vfox_filter = function() |
| 160 | + local env = io.popen('vfox env -s clink') |
| 161 | + for line in env:lines() do |
| 162 | + vfox_setenv(line) |
| 163 | + end |
| 164 | + env:close() |
| 165 | + end |
149 | 166 | end)
|
150 |
| -coroutine.resume(vfox_task) |
| 167 | +clink.runcoroutineuntilcomplete(vfox_task) |
151 | 168 |
|
152 | 169 | local vfox_prompt = clink.promptfilter(30)
|
153 |
| -function vfox_prompt:filter(prompt) |
154 |
| - clink.promptcoroutine(function() |
155 |
| - local env = io.popen('vfox env -s clink') |
156 |
| - for line in env:lines() do |
157 |
| - vfox_setenv(line) |
158 |
| - end |
159 |
| - env:close() |
160 |
| - end) |
| 170 | +function vfox_prompt:filter() |
| 171 | + clink.promptcoroutine(vfox_filter) |
161 | 172 | end
|
0 commit comments