Skip to content

Commit

Permalink
add tmux help
Browse files Browse the repository at this point in the history
  • Loading branch information
raojinlin committed Jul 11, 2024
1 parent 980bbba commit 372ef46
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))​。








## 插件开发指南

### 插件的作用
Expand Down
21 changes: 17 additions & 4 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 372ef46

Please sign in to comment.