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

Set pane title if provided in config file #891

Merged
merged 10 commits into from
Feb 8, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
### Misc
- Fix typos and remove extra whitespace
- Set pane title if provided in config file

## 3.1.0
- add a FAQ entry about how long commands may be lost/corrupted by TTY typeahead
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ be due [#651](https://github.com/tmuxinator/tmuxinator/issues/651). See [this
comment](https://github.com/tmuxinator/tmuxinator/issues/651#issuecomment-497780424)
for a workaround.

### Pane titles

It is also possible (starting with tmux v2.6) to give a title to panes.

```yaml
windows:
- editor:
layout: main-vertical
panes:
- editor:
- vim
- guard
```

**Note:** For the titles to display you will need to modify your .tmux.conf with the following entries.

```
set -g pane-border-format "#{pane_index} #{pane_title}"
set -g pane-border-status bottom
```

## Interpreter Managers & Environment Variables

To use tmuxinator with rbenv, RVM, NVM etc, use the `pre_window` option.
Expand Down
3 changes: 3 additions & 0 deletions lib/tmuxinator/assets/template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ cd <%= root || "." %>
<% end %>
<% else %>
<% window.panes.each do |pane| %>
<% unless Tmuxinator::Config.version < 2.6 %>
<%= pane.tmux_set_title %>
<% end %>
<% if pane.project.pre_window %>
<%= pane.tmux_pre_window_command %>
<% end %>
Expand Down
16 changes: 14 additions & 2 deletions lib/tmuxinator/pane.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Tmuxinator
class Pane
attr_reader :commands, :project, :index, :tab
attr_reader :commands, :project, :index, :tab, :title

def initialize(index, project, tab, *commands)
def initialize(index, project, tab, *commands, title: nil)
@commands = commands
@index = index
@project = project
@tab = tab
@title = title
end

def tmux_window_and_pane_target
Expand All @@ -29,6 +30,12 @@ def tmux_main_command(command)
end
end

def tmux_set_title
unless title.nil?
_set_title(title)
end
end

def name
project.name
end
Expand Down Expand Up @@ -61,5 +68,10 @@ def _send_target(e)
def _send_keys(t, e)
"#{project.tmux} send-keys -t #{t} #{e} C-m"
end

def _set_title(title)
target = tmux_window_and_pane_target
"#{project.tmux} select-pane -t #{target} -T #{title}"
end
end
end
20 changes: 10 additions & 10 deletions lib/tmuxinator/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def build_panes(panes_yml)
return if panes_yml.nil?

Array(panes_yml).map.with_index do |pane_yml, index|
commands = case pane_yml
when Hash
pane_yml.values.first
when Array
pane_yml
else
pane_yml
end

Tmuxinator::Pane.new(index, project, self, *commands)
commands, title = case pane_yml
when Hash
[pane_yml.values.first, pane_yml.keys.first]
when Array
[pane_yml, nil]
else
[pane_yml, nil]
end

Tmuxinator::Pane.new(index, project, self, *commands, title: title)
end.flatten
end

Expand Down
23 changes: 19 additions & 4 deletions spec/lib/tmuxinator/pane_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
describe Tmuxinator::Pane do
let(:klass) { described_class }
let(:instance) { klass.new(index, project, window, *commands) }
# let(:index) { "vim" }
# let(:project) { 0 }
# let(:tab) { nil }
# let(:commands) { nil }
let(:instance_with_title) do
klass.new(index, project, window, *commands, title: title)
end
let(:index) { 0 }
let(:project) { double }
let(:window) { double }
let(:commands) { ["vim", "bash"] }
let(:title) { "test" }

before do
allow(project).to receive(:name).and_return "foo"
allow(project).to receive(:base_index).and_return 0
allow(project).to receive(:pane_base_index).and_return 1
allow(project).to receive(:tmux).and_return "tmux"

allow(window).to receive(:project).and_return project
allow(window).to receive(:index).and_return 0
Expand All @@ -28,4 +29,18 @@
end

it { expect(subject.tmux_window_and_pane_target).to eql "foo:0.1" }

it "does not set pane title" do
expect(subject.tmux_set_title).to be_nil
end

context "when title is provided" do
subject { instance_with_title }

it "sets pane title" do
expect(subject.tmux_set_title).to eql(
"tmux select-pane -t foo:0.1 -T test"
)
end
end
end
20 changes: 20 additions & 0 deletions spec/lib/tmuxinator/window_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@
end
end

context "titled panes" do
let(:panes) do
[
{ "editor" => ["vim"] },
{ "run" => ["cmd1", "cmd2"] },
"top"
]
end

it "creates panes with titles" do
expect(window.panes).to match(
[
a_pane.with(index: 0, title: "editor").and_commands("vim"),
a_pane.with(index: 1, title: "run").and_commands("cmd1", "cmd2"),
a_pane.with(index: 2, title: nil).and_commands("top")
]
)
end
end

context "nested collections" do
let(:command1) { "cd /tmp/" }
let(:command2) { "ls" }
Expand Down
Loading