1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-18 19:15:14 +00:00
starship/src/init/starship.lua
Rashil Gandhi c335b4267b
feat: add support for cmd (#3277)
* feat: add support for cmd

* add preprompt and precmd support

* add keymap support

* add info about minimum Clink version

* simplify escaping

* add handling for cmd custom commands

* add support for transient_prompt and transient_rprompt

* Revert 914057952508e81e20086fcb707ba2a0be85fdd3

This reverts commit "add support for transient_prompt and transient_rprompt"

* Apply suggestions from code review

* disable cmd shell custom commands

* any shell other than cmd can be used

* better error and correct script location

* move shell check in `map_no_escaping`
2022-01-09 23:47:53 -06:00

62 lines
1.7 KiB
Lua

if (clink.version_encoded or 0) < 10020030 then
error("Starship requires a newer version of Clink; please upgrade to Clink v1.2.30 or later.")
end
local starship_prompt = clink.promptfilter(5)
start_time = os.clock()
end_time = 0
curr_duration = 0
is_line_empty = true
clink.onbeginedit(function ()
end_time = os.clock()
if not is_line_empty then
curr_duration = end_time - start_time
end
end)
clink.onendedit(function (curr_line)
if starship_precmd_user_func ~= nil then
starship_precmd_user_func(curr_line)
end
start_time = os.clock()
if string.len(string.gsub(curr_line, '^%s*(.-)%s*$', '%1')) == 0 then
is_line_empty = true
else
is_line_empty = false
end
end)
function starship_prompt:filter(prompt)
if starship_preprompt_user_func ~= nil then
starship_preprompt_user_func(prompt)
end
return io.popen([[::STARSHIP::]].." prompt"
.." --status="..os.geterrorlevel()
.." --cmd-duration="..math.floor(curr_duration*1000)
.." --terminal-width="..console.getwidth()
.." --keymap="..rl.getvariable('keymap')
):read("*a")
end
function starship_prompt:rightfilter(prompt)
return io.popen([[::STARSHIP::]].." prompt --right"
.." --status="..os.geterrorlevel()
.." --cmd-duration="..math.floor(curr_duration*1000)
.." --terminal-width="..console.getwidth()
.." --keymap="..rl.getvariable('keymap')
):read("*a")
end
local characterset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
local randomkey = ""
math.randomseed(os.time())
for i = 1, 16 do
local rand = math.random(#characterset)
randomkey = randomkey..string.sub(characterset, rand, rand)
end
os.setenv('STARSHIP_SHELL', 'cmd')
os.setenv('STARSHIP_SESSION_KEY', randomkey)