Skip to content

Commit

Permalink
Fix --here option with window base index
Browse files Browse the repository at this point in the history
  • Loading branch information
remi committed Aug 20, 2014
1 parent 1d71263 commit eab12ec
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
8 changes: 6 additions & 2 deletions lib/teamocil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
require 'teamocil/cli'

# Command classes
require 'teamocil/command/list_panes'
require 'teamocil/command/list_windows'
require 'teamocil/command/new_window'
require 'teamocil/command/rename_session'
require 'teamocil/command/rename_window'
Expand All @@ -21,13 +23,15 @@
require 'teamocil/command/select_window'
require 'teamocil/command/send_keys'
require 'teamocil/command/send_keys_to_pane'
require 'teamocil/command/show_options'
require 'teamocil/command/show_window_options'
require 'teamocil/command/split_window'

# Tmux classes
require 'teamocil/tmux'
require 'teamocil/tmux/session'
require 'teamocil/tmux/window'
require 'teamocil/tmux/pane'
require 'teamocil/tmux/options'

module Teamocil
class << self
Expand All @@ -49,7 +53,7 @@ def self.system(*args)
end

def self.query_system(command)
`#{command}`
`tmux #{command.to_s}`
end

def self.parse_options!(arguments: nil)
Expand Down
15 changes: 15 additions & 0 deletions lib/teamocil/command/list_panes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Teamocil
module Command
class ListPanes < ClosedStruct.new(:index)
def to_s
"list-panes #{options.join(' ')}"
end

def options
[].tap do |options|
options << "-t '#{index}'" if index
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/teamocil/command/list_windows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Teamocil
module Command
class ListWindows < ClosedStruct.new(:foo)
def to_s
'list-windows'
end
end
end
end
9 changes: 9 additions & 0 deletions lib/teamocil/command/show_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Teamocil
module Command
class ShowOptions < ClosedStruct.new(:name)
def to_s
"show-options -gv #{name}"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/teamocil/command/show_window_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Teamocil
module Command
class ShowWindowOptions < ClosedStruct.new(:name)
def to_s
"show-window-options -gv #{name}"
end
end
end
end
25 changes: 25 additions & 0 deletions lib/teamocil/tmux.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Teamocil
module Tmux
def self.option(option, default: nil)
command = Teamocil::Command::ShowOptions.new(name: option)
value = Teamocil.query_system(command).chomp
value.empty? ? default : value.to_i
end

def self.window_option(option, default: nil)
command = Teamocil::Command::ShowWindowOptions.new(name: option)
value = Teamocil.query_system(command).chomp
value.empty? ? default : value.to_i
end

def self.window_count
command = Teamocil::Command::ListWindows.new
Teamocil.query_system(command).chomp.split("\n").size
end

def self.pane_count(index: nil)
command = Teamocil::Command::ListPanes.new(index: index)
Teamocil.query_system(command).chomp.split("\n").size
end
end
end
15 changes: 0 additions & 15 deletions lib/teamocil/tmux/options.rb

This file was deleted.

10 changes: 9 additions & 1 deletion lib/teamocil/tmux/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ def as_tmux
end

def window_base_index
@window_base_index ||= Teamocil::Tmux::Options.fetch_option('base-index', default: 0)
@window_base_index ||= begin
base_index = Teamocil::Tmux.option('base-index', default: 0)
current_window_count = Teamocil::Tmux.window_count

# If `--here` is specified, treat the current window as a new one
current_window_count -= 1 if Teamocil.options[:here]

base_index + current_window_count
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/teamocil/tmux/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def as_tmux
end

def pane_base_index
@pane_base_index ||= Teamocil::Tmux::Options.fetch_window_option('pane-base-index', default: 0)
@pane_base_index ||= Teamocil::Tmux.window_option('pane-base-index', default: 0)
end
end
end
Expand Down

0 comments on commit eab12ec

Please sign in to comment.