Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support clink、cmder shell on Windows #129

Closed
ViCrack opened this issue Mar 19, 2024 · 20 comments · Fixed by #167
Closed

[Feature]: Support clink、cmder shell on Windows #129

ViCrack opened this issue Mar 19, 2024 · 20 comments · Fixed by #167
Labels
enhancement New feature or request
Milestone

Comments

@ViCrack
Copy link

ViCrack commented Mar 19, 2024

1. Your usage scenarios?

windows上我一般使用clink或者cmder来增强cmd,因为powershell启动速度太慢了

而在cmd.exe运行的话,会出现The current shell lacks hook support or configuration

2. What is your expected outcome?

我看clink和cmder等终端貌似有hook的扩展接口提供,不知道作者是否考虑以后也支持这类终端

https://chrisant996.github.io/clink/clink.html#extending-clink

@ViCrack ViCrack added the enhancement New feature or request label Mar 19, 2024
@aooohan
Copy link
Member

aooohan commented Mar 20, 2024

因为cmd不支持hook所以会有提示语.

我看clink和cmder等终端貌似有hook的扩展接口提供,不知道作者是否考虑以后也支持这类终端

如果支持hook的话可以支持的, 参照下面的进行实现就可以:

https://github.com/version-fox/vfox/blob/main/internal/shell/powershell.go

https://github.com/version-fox/vfox/blob/main/internal/shell/shell.go

有兴趣可以提个PR ;)

@aooohan aooohan changed the title [Feature]: 支持更多windows shell,比如clink、cmder [Feature]: Support clink、cmder shell on Windows Mar 20, 2024
@ViCrack
Copy link
Author

ViCrack commented Mar 21, 2024

验证过可以支持hook的,用lua脚本扩展

大体上hook脚本可以这么写,甚至clink还可以加上vfox命令自动完成 (https://github.com/vladimir-kotikov/clink-completions/blob/master/nvm.lua)

local originalPath = os.getenv('PATH')

local cwd_prompt = clink.promptfilter(30)
function cwd_prompt:filter(prompt)
    -- if os.isfile('.tool-versions') then
    -- end
    -- Run vfox command to get env vars.
    local handle = io.popen("vfox env -s pwsh")
    local result = handle:read("*a")
    handle:close()
    -- 去掉开头的部分
    if string.len(result) > 0 then
        result = string.gsub(result, "$env:PATH='", "")
        result = string.gsub(result, "';", "")

        print(result)
        -- local currentPath = os.getenv('PATH')
        -- os.setenv("PATH", result .. ";" .. currentPath)
    end
end

不过我不太清楚pwsh实现方式,为什么需要加载EnvContent,和最后进程退出的env --cleanup是在干什么
https://github.com/version-fox/vfox/blob/main/internal/shell/powershell.go

按我目前的理解设计是这样:

通过hook shell prompt,能够在shell每次进入目录时候执行命令vfox env命令,执行该命令之后拿到在当前目录下nodejs的真实路径,
然后获取当前最新的环境变量PATH,在开头拼接上nodejs的真实路径,然后设置shell的环境变量PATH即可

result = 'vfox nodejs path.....'
local currentPath = os.getenv('PATH')
os.setenv("PATH", result .. ";" .. currentPath)

另外这里为了防止PATH无限的拼接到开头,或许可以加个(@vfoxstart@和@vfoxend@)等标识符,每次都先判断如果存在就去掉老的这部分,然后再拼接(不知道还有什么简单的方法)

result = 'vfox nodejs path'
local currentPath = os.getenv('PATH')
os.setenv("PATH", ";@vfoxstart@;" .. result .. ";@vfoxend@;" .. currentPath)

至于那个env --cleanup好像没啥用,设置的PATH环境变量只会对当前进程有效,当shell退出后,不需要手动清理

@aooohan
Copy link
Member

aooohan commented Mar 21, 2024

不过我不太清楚pwsh实现方式,为什么需要加载EnvContent,

EnvContent就是一个占位符, vfox会把知道需要配置的环境变量和PATH(初始的环境的和全局SDK的), 通过你实现的shell翻译成对应的语句(也就是Export函数的实现), 替换到EnvContent上.

和最后进程退出的env --cleanup是在干什么

清理临时文件, 因为vfox use --session模式, 会产生临时文件, 这就需要shell退出的时候及时清理.

通过hook shell prompt,能够在shell每次进入目录时候执行命令vfox env命令

不是每次进入目录, 而是每次执行完命令都需要调用vfox env命令

@jan-bar
Copy link
Contributor

jan-bar commented Mar 27, 2024

jan-bar@7321390

我自己测试了这个改动,目前完成全局、会话、项目三种切换模式都没问题。没找到clink退出终端的hook,但是我看清理逻辑会删除一定时间之前的会话文件,应该没啥大问题。

按照readme描述在clink脚本目录添加2个脚本文件即可。也不知道有没有更好的方案,所以没提PR。

@aooohan
Copy link
Member

aooohan commented Mar 28, 2024

image 我按照你说的配置之后, 好像没有生效... 提示Warning:xxx就是没生效..

@jan-bar
Copy link
Contributor

jan-bar commented Mar 28, 2024

image 我按照你说的配置之后, 好像没有生效... 提示Warning:xxx就是没生效..

不好意没有测试纯clink的场景,我习惯用cmder了,我的测试是在cmder里面成功。你可以下载cmder试试看。
https://github.com/cmderdev/cmder/releases

我刚用纯clink也出现相同问题,我再查查看怎么解决这个问题吧。

@jan-bar
Copy link
Contributor

jan-bar commented Mar 28, 2024

我更新了方法,现在只需要在clink脚本路径放一个lua脚本就可以了。之前多了个vfox.cmd脚本是因为cmder启动脚本会自动执行,而原生的clink不会自动执行,但会自动执行脚本目录下所有*.lua脚本。因此我把所有逻辑都写到lua里面,并且在clink和cmderr中都测试通过了。

这是改动:jan-bar@f3d4a5d

@aooohan aooohan added this to the 0.3.1 milestone Apr 1, 2024
@aooohan
Copy link
Member

aooohan commented Apr 1, 2024

这个放在下个版本处理.

@jan-bar
Copy link
Contributor

jan-bar commented Apr 1, 2024

-- vfox
local vfox_sdk_table = {}
local vfox_add_table = {}
local vfox_available = io.popen('vfox available')
for line in vfox_available:lines() do
    local sdk,name = line:match('^([%a-]+)/([%a-]+)')
    if sdk and name then
        table.insert(vfox_sdk_table, sdk)
        table.insert(vfox_add_table, sdk..'/'..name)
    end
end
vfox_available:close()

local vfox_sdk = clink.argmatcher():addarg(vfox_sdk_table):nofiles()
local vfox_add = clink.argmatcher():addarg(vfox_add_table):nofiles()
local vfox_use = clink.argmatcher():addflags({
'-g'..vfox_sdk,'-s'..vfox_sdk,'-p'..vfox_sdk,
}):nofiles()

clink.argmatcher('vfox'):addarg({
"add"..vfox_add,
"use"..vfox_use,'u'..vfox_use,
"info"..vfox_sdk,
"remove"..vfox_sdk,
"search"..vfox_sdk,
"update"..vfox_sdk,
"available"..vfox_sdk,
"install",'i',"current",'c',"list",'ls',"uninstall",'un',
}):nofiles()

GIF 2024-4-1, 星期一 11-20-31

简单的写个自动补全脚本,如果需要vfox use -s xxx@x.y.x带版本号的话需要解析vfox ls的结果,有点麻烦没有弄。不过直接用vfox use -s xxx会弹出选择界面,我觉得也足够了。

不过我看最新仓库代码换了插件相关逻辑,到时候这个自动补全脚本还得优化额。。暂时我的 https://github.com/jan-bar/vfox/tree/feat_clink 这个分支代码我自己用起来还可以额。

@aooohan
Copy link
Member

aooohan commented Apr 1, 2024

@jan-bar 有兴趣加入VersionFox吗? 哈哈哈, clink玩的太6了.

@jan-bar
Copy link
Contributor

jan-bar commented Apr 1, 2024

这都是看clink官方文档,然后再照着cmder写好的一些命令脚本改改额。其实我只用到clink的很小一部分功能额。

image

话说VersionFox咋加入?是 discord 这个么?我已经加入了额。。

@aooohan
Copy link
Member

aooohan commented Apr 1, 2024

话说VersionFox咋加入?是 discord 这个么?我已经加入了额。。

不是Discord, 我给你发邮件, 注意看邮箱

@jan-bar
Copy link
Contributor

jan-bar commented Apr 1, 2024

不是Discord, 我给你发邮件, 注意看邮箱

好的,已经加入了。

jan-bar added a commit to jan-bar/vfox that referenced this issue Apr 2, 2024
Support the clink terminal's automatic completion and vfox environment switching functions by writing Lua scripts

link: version-fox#129
@jan-bar
Copy link
Contributor

jan-bar commented Apr 2, 2024

#167 我创建了clink的所有功能,包括自动补全和vfox环境切换。再测测看看有没有问题吧。这次加上了use带版本的自动补全。

GIF 2024-4-2, 星期二 10-20-21

@aooohan
Copy link
Member

aooohan commented Apr 2, 2024

太帅了! 我下午测试一下.

@ViCrack
Copy link
Author

ViCrack commented Apr 2, 2024

666

@jan-bar
Copy link
Contributor

jan-bar commented Apr 3, 2024

我提交了一些修改,vfox use 不用带-s/-g/-p参数去补全了,命令是支持直接vfox use xxx@x.y.z。而vfox env必须 vfox env -s xxx才需要补全终端名称。感觉这个自动补全需要看体验去优化啊。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

我提交了一些修改,vfox use 不用带-s/-g/-p参数去补全了,命令是支持直接vfox use xxx@x.y.z。而vfox env必须 vfox env -s xxx才需要补全终端名称。感觉这个自动补全需要看体验去优化啊。

不用一次就写得很完美, 开源项目嘛, 慢慢来!

试了, 非常棒!

@jan-bar
Copy link
Contributor

jan-bar commented Apr 3, 2024

不用一次就写得很完美, 开源项目嘛, 慢慢来!

试了, 非常棒!

嗯嗯,感谢支持。那个lua脚本,我暂时也想不出有啥需要改的了。我已经按照自己的用法,比对每个命令的参数做好补全了额。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

不用一次就写得很完美, 开源项目嘛, 慢慢来!
试了, 非常棒!

嗯嗯,感谢支持。那个lua脚本,我暂时也想不出有啥需要改的了。我已经按照自己的用法,比对每个命令的参数做好补全了额。

也感谢你的贡献!

@aooohan aooohan closed this as completed Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants