use_commands = use_commands or "use"
local function use_getparam(line)
local candidate = line:match("^[ \t]*([^ \t]+)")
if not candidate then
return
end
local commands = string.explode(use_commands or "use")
local command_name
for _,name in ipairs(commands) do
if candidate == name then
command_name = name
end
end
if not command_name then
return
end
local param = line:match("^[ \t]*[^ \t]+[ \t]+(.*)")
if not param then
return ""
end
return param
end
local function get_tmp_script()
local tmpdir = os.getenv("TMP") or os.getenv("TEMP") or "/tmp"
local filename = tmpdir .. "/use_clink.bat"
return filename
end
local function use_run(param)
local use_output = io.popen(::USE:: .. " " .. param)
local result = {}
if use_output then
local filename = get_tmp_script()
local file = io.open(filename, "w")
if file then
file:write("@echo off\n")
for line in use_output:lines() do
file:write(line)
file:write("\n")
end
file:write("echo.\n")
file:close()
table.insert(result, "call " .. filename .. "& echo \x1b[2A")
else
table.insert(result, "echo Failed to create file: " .. filename)
end
use_output:close()
end
return result
end
local function use_filter(line)
local param = use_getparam(line)
if not param then
return
end
if param == "" or param:find("^-") or param:find("^init") or param:find("^config") or param:find("^list") or param:find("^set") or param:find("^print") or param:find("^help") then
os.execute(::USE:: .. " " .. param)
return "", false
else
return use_run(param), false
end
end
clink.onfilterinput(use_filter)
local function list_envs()
local envs
local r = io.popen(::USE:: .. " list")
envs = {}
for line in r:lines() do
table.insert(envs, line)
end
return envs
end
os.setenv('USE_SHELL', 'cmd')
clink.argmatcher(table.unpack(string.explode(use_commands or "use")))
:addarg(list_envs())
:addflags("--help", "-h", "--version", "-V", "--create")
:nofiles()