lab42_tmux2 0.0.1pre3 → 0.0.1pre4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 011cc804cf724cd9516c46971d0d21b60f333a0c
4
- data.tar.gz: 3a33a65919f51e7f885abdf714583ed55f90f18b
3
+ metadata.gz: d83b9c45369edab02ddee3ff9248a899dfd5f89d
4
+ data.tar.gz: 737d9d882cb972d5de3d87e6e6fd6823fe98aac8
5
5
  SHA512:
6
- metadata.gz: cab3d6012d10a33dae63ddfae8df3b5c0d1ad8738e2e92d0b8d681ec7a9ad97c76e3b64282b8af4aa9a1689dcbb68ab9e30193372cf732c2cf1d51e5bc761fdf
7
- data.tar.gz: 302912baf4d282e8345e28747f4c90fd3d2a55a6d8c056f33e43c8ddfdd2427ee97a21a265044bbb0dc618f0e8d4d0c5c01a04c186565bb81afe13b953c5e0ce
6
+ metadata.gz: 7023719f39716a788a03e60c3ca1fd632c453bbbbca3d5a911cebb492918166eb33af6b9a1f8e62078d23dfad5eac0fab9aa52ad191e1fab8abcbab80172de16
7
+ data.tar.gz: 9ef8c13b005882f231dba5a49a624e0d4b725b6a15a7fa8ca0cf20537453302a85db132db42413addb67b245f0bc5fc6f3611058979271f056084a6beb3ade69
data/README.md CHANGED
@@ -25,12 +25,12 @@ A simple API for launching tmux sessions from Ruby scripts
25
25
 
26
26
  session "vi_session" do
27
27
  configure do | c |
28
- c.home '.' # Would have been the default
29
- c.session_name = ARGV.first # Would have been the default
28
+ c.home '.' # Would have been the default
29
+ c.window_automatic_rename true # Default is false
30
30
  end
31
31
  new_window 'vi' do
32
32
  send_keys 'vi .'
33
- wait_for '.. (up a dir)' # NERDTree pane has loaded
33
+ wait_for '.. (up a dir)' # NERDTree pane has loaded (not yet implemented)
34
34
  send_keys_raw 'C-w', 'l'
35
35
  end
36
36
  new_window 'pry' do
@@ -40,6 +40,28 @@ A simple API for launching tmux sessions from Ruby scripts
40
40
  end
41
41
  ```
42
42
 
43
+ ### Hooks
44
+
45
+ Add a `after_new_window` hook for tmux commands to be executed after the creation of a new
46
+ window.
47
+
48
+ ```ruby
49
+ session 'my rails' do
50
+ after_new_window do
51
+ send_keys "rvm use default@my-rails"
52
+ end
53
+ # ...
54
+ end
55
+ ```
56
+
57
+
58
+ ### Plugins
59
+
60
+ Plugins are easy to write, just MP the `Lab42::Tmux::Session` class. However precise guidelines
61
+ how to do this will be available soon, also you might want to create some configuration.
62
+
63
+ The plugin guidelines and some plugins (vi, ruby, rvm, python's virtualenv) are planned for
64
+ the near future.
43
65
 
44
66
  ## Dev Notes
45
67
 
@@ -15,10 +15,13 @@ module Lab42
15
15
  end
16
16
  end
17
17
 
18
+ define_setter_getters :pre_wait_interval, :post_wait_interval, :wait_interval, :wait_timeout
18
19
  define_setter_getters :session_name, :window_automatic_rename
19
20
 
20
21
  private
21
22
  def initialize
23
+ @wait_interval = 0.5
24
+ @wait_timeout = 2
22
25
  end
23
26
  end # module Config
24
27
  end # module Tmux
@@ -7,7 +7,24 @@ module Lab42
7
7
  end
8
8
  def query *args
9
9
  puts args.join( ' ' )
10
- return args.first != 'has-session'
10
+ if args.first == 'capture-pane'
11
+ capture_pane( *args.drop( 1 ) )
12
+ else
13
+ args.first != 'has-session'
14
+ end
15
+ end
16
+
17
+ private
18
+ def capture_pane *args
19
+ pane_addr = args[1]
20
+ second_capture = (@panes ||= {})[pane_addr]
21
+ if second_capture
22
+ @panes[pane_addr] = false
23
+ 'hit'
24
+ else
25
+ @panes[pane_addr] = true
26
+ 'miss'
27
+ end
11
28
  end
12
29
  end # module Interface
13
30
  end # module Tmux
@@ -4,17 +4,47 @@ module Lab42
4
4
  module Tmux
5
5
  class Session
6
6
  module Commands
7
- def new_window name
8
- @win_number += 1
9
- commands << %W{ new-window #{session_address} -n #{name} }
7
+ def new_window window_name, &block
8
+ @window_number += 1
9
+ command 'new-window', '-t', session_name, '-n', window_name
10
10
  instance_exec( &@after_new_window_hook ) if @after_new_window_hook
11
- # TODO: Include after_new_window hoox
11
+ instance_exec( &block ) if block
12
12
  end
13
13
 
14
14
  def send_keys *keys
15
- # TODO: determine target of the << operation (if in hook we need something more complicated here)
16
- commands << %W{ send-keys #{window_address} #{keys.map(&:inspect).join(' ')} C-m }
15
+ command( 'send-keys', '-t', [session_name, window_number].join(':'), *keys.map(&:inspect), 'C-m' )
17
16
  end
17
+
18
+ def wait_for text_or_rgx, alternate_pane_addr=nil, &blk
19
+ require 'timeout'
20
+ pane_addr = alternate_pane_addr || window_address
21
+ text_or_rgx = %r{#{text_or_rgx}} unless Regexp === text_or_rgx
22
+ conditional_sleep configuration.pre_wait_interval
23
+ Timeout.timeout configuration.wait_timeout do
24
+ really_wait_for text_or_rgx, in_pane: pane_addr, dependent_actions: blk
25
+ end
26
+ rescue Timeout::Error
27
+ end
28
+
29
+ def really_wait_for( match_rgx, in_pane:, dependent_actions: )
30
+ loop do
31
+ text = capture_pane in_pane
32
+ if match_rgx === text
33
+ conditional_sleep configuration.post_wait_interval
34
+ return instance_exec( &dependent_actions )
35
+ end
36
+ sleep configuration.wait_interval
37
+ end
38
+ end
39
+
40
+ private
41
+ def capture_pane pane_addr
42
+ query( 'capture-pane', *(pane_addr.split), '-p' )
43
+ end
44
+ def conditional_sleep sleepy_or_falsy
45
+ sleep sleepy_or_falsy if sleepy_or_falsy
46
+ end
47
+
18
48
  end # module Commands
19
49
  include Commands
20
50
  end # class Session
@@ -3,10 +3,10 @@ module Lab42
3
3
  class Session
4
4
  module ParameterHelpers
5
5
  def session_address
6
- "-t #{name}"
6
+ "-t #{session_name}"
7
7
  end
8
8
  def window_address
9
- "-t #{name}:#{win_number}"
9
+ "-t #{session_name}:#{window_number}"
10
10
  end
11
11
  end # module ParameterHelpers
12
12
  include ParameterHelpers
@@ -2,36 +2,33 @@ require_relative 'errors'
2
2
  require_relative 'session/commands'
3
3
  require_relative 'session/hooks'
4
4
 
5
+ require 'forwarder'
6
+
5
7
  module Lab42
6
8
  module Tmux
7
9
  class Session
8
- attr_reader :commands, :configuration, :name, :win_number
10
+
11
+ attr_reader :commands, :configuration, :session_name, :window_number
12
+
13
+ extend Forwarder
14
+ forward_all :command, :query, to_object: Interface
9
15
 
10
16
  def config &block
11
17
  block.( configuration )
12
18
  end
13
19
 
14
- def define block
15
- @definition_block = block
16
- end
17
-
18
- def run
19
- register_commands
20
- run_registered_commands
21
- end
22
-
23
- def register_commands
20
+ def run &block
24
21
  return attach if running?
25
-
26
- create_session_and_windows
27
- instance_exec( &@definition_block ) if @definition_block
22
+ create_session
23
+ instance_exec( &block )
28
24
  attach
29
25
  end
30
26
 
27
+
31
28
  private
32
29
  def initialize sess_name
33
- @name = sess_name
34
- @win_number = 0
30
+ @session_name = sess_name
31
+ @window_number = 0
35
32
  @commands = []
36
33
  @configuration = Config.new
37
34
 
@@ -39,21 +36,18 @@ module Lab42
39
36
  end
40
37
 
41
38
  def attach
42
- add_command 'attach-session', '-t', name
43
- end
44
-
45
- def add_command *args
46
- commands << args
39
+ command 'attach-session', '-t', session_name
47
40
  end
48
41
 
49
- def create_session_and_windows
50
- add_command 'source-file', File.join( ENV["HOME"], '.tmux.conf' )
51
- add_command 'new-session', '-d', '-s', name, '-n', 'sh'
52
- add_command 'set-window-option', '-g', 'automatic-rename', 'off' unless configuration.window_automatic_rename
42
+ def create_session
43
+ command 'source-file', File.join( ENV["HOME"], '.tmux.conf' )
44
+ # TODO: replace 'sh' with a configuration value
45
+ command 'new-session', '-d', '-s', session_name, '-n', 'sh'
46
+ command 'set-window-option', '-g', 'automatic-rename', 'off' unless configuration.window_automatic_rename
53
47
  end
54
48
 
55
49
  def running?
56
- Interface.query 'has-session', '-t', name
50
+ Interface.query 'has-session', '-t', session_name
57
51
  end
58
52
 
59
53
  def run_command command
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Lab42
3
3
  module Tmux
4
- VERSION = '0.0.1pre3'
4
+ VERSION = '0.0.1pre4'
5
5
  end # module Tmux
6
6
  end # module Lab42
data/lib/lab42/tmux.rb CHANGED
@@ -8,8 +8,7 @@ module Lab42
8
8
  def new_session session_name=nil, &block
9
9
  raise ArgumentError, 'No block provided' unless block
10
10
  session = Session.new session_name || Config.session_name
11
- session.define block if block
12
- session.run
11
+ session.run &block
13
12
  end
14
13
 
15
14
  def dry_run!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_tmux2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1pre3
4
+ version: 0.0.1pre4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-12 00:00:00.000000000 Z
11
+ date: 2014-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder2