Skip to content

Commit

Permalink
feat(terminal type): add clink mode terminal
Browse files Browse the repository at this point in the history
add clink injected cmd.exe terminal support
  • Loading branch information
jan-bar committed Mar 27, 2024
1 parent 6873bdb commit 7321390
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/shell/clink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package shell

import (
"fmt"

"github.com/version-fox/vfox/internal/env"
)

const clinkHook = `
{{.EnvContent}}
"{{.SelfPath}}" env --cleanup > nul 2> nul
`

type clink struct{}

var Clink = clink{}

func (b clink) Activate() (string, error) {
return clinkHook, nil
}

func (b clink) Export(envs env.Vars) (out string) {
for key, value := range envs {
if value == nil {
out += b.set(key, "")
} else {
out += b.set(key, *value)
}
}
return
}

func (b clink) set(key, value string) string {
return fmt.Sprintf("set \"%s=%s\"\n", key, value)
}
25 changes: 25 additions & 0 deletions internal/shell/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Find the clink script path: `clink info | findstr scripts`

Generate `vfox.cmd` script to clink script path
```bat
where /q vfox && for /f "tokens=*" %%i in ('vfox activate clink') do call %%i
```

Generate `vfox.lua` script to clink script path
```lua
os.setenv('__VFOX_PID', os.getpid())
local vfox_prompt = clink.promptfilter(30)
function vfox_prompt:filter(prompt)
local env = io.popen('vfox env -s clink')
for line in env:lines() do
local ei = string.find(line, '=')
if ei and ei > 1 then
local key = string.sub(line, 6, ei-1)
local val = string.sub(line, ei+1, #line-1)
--print('['.. key ..']','['.. val ..']')
os.setenv(key, val ~= '' and val or nil)
end
end
env:close()
end
```
2 changes: 2 additions & 0 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func NewShell(name string) Shell {
return Pwsh
case "fish":
return Fish
case "clink":
return Clink
}
return nil

Expand Down

0 comments on commit 7321390

Please sign in to comment.