Skip to content

Commit

Permalink
support config PromptDetailItems #57
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Nov 26, 2023
1 parent 1100c5b commit 0c9e626
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ _`~/` 代表 HOME 目录。在 Windows 中,请将下文的 `~/` 替换成 `C:\
# tssh 搜索和选择服务器时,每页显示的记录数,默认为 10
PromptPageSize = 10
# tssh 搜索和选择服务器时,详情中显示的配置列表,默认如下:
PromptDetailItems = Alias Host Port User GroupLabels IdentityFile ProxyCommand ProxyJump RemoteCommand
```

## 其他功能
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/trzsz/go-arg v1.5.2
github.com/trzsz/iterm2 v0.1.1
github.com/trzsz/promptui v0.10.4
github.com/trzsz/promptui v0.10.5
github.com/trzsz/ssh_config v1.3.4
github.com/trzsz/trzsz-go v1.1.7-0.20231111144918-b45bed013817
golang.org/x/crypto v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/trzsz/go-arg v1.5.2 h1:zGxCuTKvtC3jBf7HbvNk0HooUjv8uKAy2mY+bHVhRas=
github.com/trzsz/go-arg v1.5.2/go.mod h1:IC6Z/FiVH7uYvcbp1/gJhDYCFPS/GkL0APYakVvgY4I=
github.com/trzsz/iterm2 v0.1.1 h1:UZ+Su5xOuBNDXpRStfuMCsTWhajNnKEHChJ4tfd62Mc=
github.com/trzsz/iterm2 v0.1.1/go.mod h1:PMI+3JcT7J9D0T6e3mOWv8ICYdrrNZwuge3Tm7zDLws=
github.com/trzsz/promptui v0.10.4 h1:EuoA4zGJ5sco3rmyssvkP42xb8ZxiYza1OeDIlhWOk8=
github.com/trzsz/promptui v0.10.4/go.mod h1:GMZtu6ZTzU73CBFkzGtmB4wnTROIAbv4GFA74fV8V8g=
github.com/trzsz/promptui v0.10.5 h1:tlzJkx+JOeE0sqKWmqgaoToZiYqj5G1Mz+QDV97VFu8=
github.com/trzsz/promptui v0.10.5/go.mod h1:GMZtu6ZTzU73CBFkzGtmB4wnTROIAbv4GFA74fV8V8g=
github.com/trzsz/ssh_config v1.3.4 h1:7of+6rUmdWdqfgXnH9csgJe1kNkriS9xOiFGx4KCkEw=
github.com/trzsz/ssh_config v1.3.4/go.mod h1:Dl1okTjVVfsrtTA8nqkJ1OnjiCrZY6DUEI2DGT2/YoQ=
github.com/trzsz/trzsz-go v1.1.7-0.20231111144918-b45bed013817 h1:AGD0x1Oo3xIV3Gv7Pv1mGRcVCf7jV69oaEGgDgPs7wM=
Expand Down
40 changes: 40 additions & 0 deletions tssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type tsshConfig struct {
defaultUploadPath string
defaultDownloadPath string
promptPageSize uint8
promptDetailItems string
loadConfig sync.Once
loadExConfig sync.Once
loadHosts sync.Once
Expand Down Expand Up @@ -132,6 +133,8 @@ func parseTsshConfig() {
} else {
userConfig.promptPageSize = uint8(pageSize)
}
case name == "promptdetailitems" && userConfig.promptDetailItems == "":
userConfig.promptDetailItems = value
}
}

Expand All @@ -150,6 +153,9 @@ func parseTsshConfig() {
if userConfig.promptPageSize != 0 {
debug("PromptPageSize = %d", userConfig.promptPageSize)
}
if userConfig.promptDetailItems != "" {
debug("PromptDetailItems = %s", userConfig.promptDetailItems)
}
}

func initUserConfig(configFile string) error {
Expand Down Expand Up @@ -486,3 +492,37 @@ func getPromptPageSize() int {
}
return 10
}

func getPromptDetailTemplate() string {
promptDetailItems := userConfig.promptDetailItems
if promptDetailItems == "" {
promptDetailItems = "Alias Host Port User GroupLabels IdentityFile ProxyCommand ProxyJump RemoteCommand"
}
var builder strings.Builder
builder.WriteString(`{{ "--------- SSH Alias ----------\n" }}`)
for _, item := range strings.Fields(promptDetailItems) {
switch strings.ToLower(item) {
case "alias":
builder.WriteString(`{{- if .Alias }}{{ "Alias:" | faint }}{{ "\t" }}{{ .Alias }}{{ "\n" }}{{ end }}`)
case "host":
builder.WriteString(`{{- if .Host }}{{ "Host:" | faint }}{{ "\t" }}{{ .Host }}{{ "\n" }}{{ end }}`)
case "port":
builder.WriteString(`{{- if ne .Port "22" }}{{ "Port:" | faint }}{{ "\t" }}{{ .Port }}{{ "\n" }}{{ end }}`)
case "user":
builder.WriteString(`{{- if .User }}{{ "User:" | faint }}{{ "\t" }}{{ .User }}{{ "\n" }}{{ end }}`)
case "grouplabels":
builder.WriteString(`{{- if .GroupLabels }}{{ "GroupLabels:" | faint }}{{ "\t" }}{{ .GroupLabels }}{{ "\n" }}{{ end }}`)
case "identityfile":
builder.WriteString(`{{- if .IdentityFile }}{{ "IdentityFile:" | faint }}{{ "\t" }}{{ .IdentityFile }}{{ "\n" }}{{ end }}`)
case "proxycommand":
builder.WriteString(`{{- if .ProxyCommand }}{{ "ProxyCommand:" | faint }}{{ "\t" }}{{ .ProxyCommand }}{{ "\n" }}{{ end }}`)
case "proxyjump":
builder.WriteString(`{{- if .ProxyJump }}{{ "ProxyJump:" | faint }}{{ "\t" }}{{ .ProxyJump }}{{ "\n" }}{{ end }}`)
case "remotecommand":
builder.WriteString(`{{- if .RemoteCommand }}{{ "RemoteCommand:" | faint }}{{ "\t" }}{{ .RemoteCommand }}{{ "\n" }}{{ end }}`)
default:
warning("Unknown prompt detail item: %s", item)
}
}
return builder.String()
}
26 changes: 1 addition & 25 deletions tssh/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,31 +562,7 @@ func chooseAlias(keywords string) (string, bool, error) {
` {{ .GroupLabels }}`, promptCursorIcon),
Inactive: ` {{ if .Selected }}{{ "✔ " | green }}{{ end }}{{ .Alias | cyan }} ({{ .Host | red }})` +
` {{ .GroupLabels }}`,
Details: `
--------- SSH Alias ----------
{{ "Alias:" | faint }} {{ .Alias }}
{{ "Host:" | faint }} {{ .Host }}
{{- if ne .Port "22" }}
{{ "Port:" | faint }} {{ .Port }}
{{- end }}
{{- if .User }}
{{ "User:" | faint }} {{ .User }}
{{- end }}
{{- if .GroupLabels }}
{{ "GroupLabels:" | faint }} {{ .GroupLabels }}
{{- end }}
{{- if .IdentityFile }}
{{ "IdentityFile:" | faint }} {{ .IdentityFile }}
{{- end }}
{{- if .ProxyCommand }}
{{ "ProxyCommand:" | faint }} {{ .ProxyCommand }}
{{- end }}
{{- if .ProxyJump }}
{{ "ProxyJump:" | faint }} {{ .ProxyJump }}
{{- end }}
{{- if .RemoteCommand }}
{{ "RemoteCommand:" | faint }} {{ .RemoteCommand }}
{{- end }}`,
Details: getPromptDetailTemplate(),
}

searcher := func(input string, index int) bool {
Expand Down

0 comments on commit 0c9e626

Please sign in to comment.