diff --git a/README.md b/README.md index fa1fc72..533c916 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,33 @@ Ensure that the Go environment is installed. go get -u github.com/raojinlin/hostnav ``` +## Using tmux +To bind keys in tmux to open hostnav, you can add the following configurations to your ~/.tmux.conf file: + +```bash +# Bind Prefix-G to open hostnav in a new window +tmux bind g new-window 'hostnav' + +# Bind Prefix-G to split the current window and open hostnav in a new pane +tmux bind g split-window 'hostnav' +``` + +For a more specific example related to splitting windows and opening hostnav in different ways: + +```bash +# Bind Prefix-G to split the window and open hostnav in a new vertical pane +tmux bind g split-window -v 'hostnav' + +# Bind Prefix-G to split the window and open hostnav in a new horizontal pane +tmux bind g split-window -h 'hostnav' + +``` + +These commands bind the key G (when pressed after the tmux prefix key, usually Ctrl-b) to either create a new window or split the current window, and then execute hostnav in the new pane or window​ ([Koen Woortman](https://koenwoortman.com/tmux-remap-split-window-keys/))​​ ([Stack Overflow](https://stackoverflow.com/questions/38967247/tmux-how-do-i-bind-function-keys-to-commands))​. + + + + ## Prerequisites Before running hostnav, make sure the following commands are properly installed: diff --git a/README_CN.md b/README_CN.md index 2b72deb..5b311e8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -204,6 +204,37 @@ plugins: search: xxxx ``` + +### 使用tmux +要在 tmux 中绑定按键以打开 hostnav,你可以将以下配置添加到您的 ~/.tmux.conf 文件中: + +```bash +# 绑定 Prefix-G 在新窗口中打开 hostnav +tmux bind g split-window 'hostnav -new-window' + +# 绑定 Prefix-G 在当前窗口分隔出一个面板并打开 hostnav +tmux bind g split-window 'hostnav' +``` + +关于分隔窗口并以不同方式打开 hostnav 的更具体示例: + +```bash +# 绑定 Prefix-G 分隔出一个垂直面板并打开 hostnav +tmux bind g split-window -v 'hostnav' + +# 绑定 Prefix-G 分隔出一个水平面板并打开 hostnav +tmux bind g split-window -h 'hostnav' +``` + +这些命令绑定键 G(在按下 tmux 前缀键后,通常是 Ctrl-b),以创建一个新窗口或分隔当前窗口,并在新面板或窗口中执行 hostnav​ ([Koen Woortman](https://koenwoortman.com/tmux-remap-split-window-keys/))​​ ([Stack Overflow](https://stackoverflow.com/questions/38967247/tmux-how-do-i-bind-function-keys-to-commands))​。 + + + + + + + + ## 插件开发指南 ### 插件的作用 diff --git a/pkg/terminal/terminal.go b/pkg/terminal/terminal.go index fc74d88..ad31910 100644 --- a/pkg/terminal/terminal.go +++ b/pkg/terminal/terminal.go @@ -62,7 +62,11 @@ func (s *SSHInfo) Connect(option *ConnectionOption) error { } command := fmt.Sprintf("ssh %s %s@%s -p%d", opts, user, dest, port) - return tmux.NewWindow(s.Name, command) + if option.Tmux.NewWindow { + return tmux.NewWindow(s.Name, command) + } + + return tmux.SplitWindow(command) } func (s *SSHInfo) String() string { @@ -111,12 +115,21 @@ func (p *Pod) String() string { } func (p *Pod) Connect(option *ConnectionOption) error { + var command string + var windowName string if p.Namespace == NamespaceDocker { - return tmux.NewWindow("Container: "+p.Container.Name, fmt.Sprintf("docker exec -it %s %s", p.Container.Name, p.Container.Command)) + command = fmt.Sprintf("docker exec -it %s %s", p.Container.Name, p.Container.Command) + windowName = "Container: " + p.Container.Name + } else { + command = fmt.Sprintf("kubectl --kubeconfig %s exec -n %s -it %s -c %s -- %s", p.KubeConfig, p.Namespace, p.Name, p.Container.Name, p.Container.Command) + windowName = fmt.Sprintf("Pod: %s/%s", p.Name, p.Container.Name) + } + + if option.Tmux.NewWindow { + return tmux.NewWindow(windowName, command) } - command := fmt.Sprintf("kubectl --kubeconfig %s exec -n %s -it %s -c %s -- %s", p.KubeConfig, p.Namespace, p.Name, p.Container.Name, p.Container.Command) - return tmux.NewWindow(fmt.Sprintf("Pod: %s/%s", p.Name, p.Container.Name), command) + return tmux.SplitWindow(command) } type Container struct {