lab42_tmux 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a9e3d3a2d988cc697b9883c34ff111da4d293a1
4
- data.tar.gz: 2935cfbf73d68006024b52c06fb8970f76c60b66
3
+ metadata.gz: f15f6f7b7cc46ac2975640531b3cdccfcb5d6d90
4
+ data.tar.gz: 0af03c1673a9bde59186e9301e8eabdfed12e35d
5
5
  SHA512:
6
- metadata.gz: 02bee1fd05f6dc7cfa8548e083dd4ecc62a155662238ac0396a44bb216e39a99797ce96379c41b668a99ee971accac87151d1469db3a4e0a365cf1524fc0fb6e
7
- data.tar.gz: e35038f4a48f418c9d2f3310d0f3d528eeef4dc08ec39d8486353fda9e6e339b9afa6e762000def3a3d3f2ff445224b210e14a603033de66cbca106096db8feb
6
+ metadata.gz: 8aed7ad5a9495ded7b32b3a9d2a12223c630683f77a2403a876e129041bc40b84afe68793b4e7d6a3ecea898b555d8e23ec0304118d38e21d62f55cf0f20b47f
7
+ data.tar.gz: 6b8815601f598e2b85f6838786c000802ee44409c7ca9e6681b3d14c01eeec74c142fa33e4ce13f27156cc4d0b1989c4a23a29233c968c2c4b74a65df02104c6
data/README.md CHANGED
@@ -6,6 +6,24 @@
6
6
 
7
7
  #### lab42\_tmux
8
8
 
9
+ ```
10
+ lab42_ruby_session project_home [:dry_run] [repl: <repl and params>]
11
+ ```
12
+
13
+ Opens a tmux session with the following windows
14
+
15
+
16
+
17
+
18
+ repl, defaulting to "irb"
19
+ vi "vi ."
20
+ lib "vi lib"
21
+ spec "vi spec" iff project_home/spec exists
22
+ features "vi feartures" iff project_home/featutes exists
23
+ cucu "sh" iff project_home/featutes exists
24
+ a shell
25
+
26
+ *Broken in 0.0.2*
9
27
  ```
10
28
  lab42_tmux some_path [windows: n]
11
29
  ```
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'lab42/tmux/predefined/ruby_session'
3
+
4
+ Lab42::Tmux::Predefined::RubySession.new( *ARGV.dup ).exec!
@@ -10,11 +10,7 @@ module Lab42
10
10
  module Command
11
11
 
12
12
  def tmux cmd
13
- if @options.dry_run
14
- puts "tmux #{cmd}"
15
- else
16
- system "tmux #{cmd}"
17
- end
13
+ commands << "tmux #{cmd}"
18
14
  end
19
15
 
20
16
  def tmux_current_window
@@ -91,10 +87,10 @@ module Lab42
91
87
  tmux_send_keys [@options.cmd_prefix, cmd].mk_string, eol
92
88
  end
93
89
 
94
- def tmux_project_home dir=nil
95
- dir = File.join @options.dir, dir if dir && %r{\A[^/]} === dir
96
- tmux_send_keys "cd #{dir || @options.dir}"
97
- end
90
+ # def tmux_project_home dir=nil
91
+ # dir = File.join @options.dir, dir if dir && %r{\A[^/]} === dir
92
+ # tmux_send_keys "cd #{dir || @options.dir}"
93
+ # end
98
94
 
99
95
  def tmux_register_window_command noexec=false, &blk
100
96
  blk.() unless noexec
@@ -2,7 +2,7 @@ module Lab42
2
2
  class Tmux
3
3
  module Commands
4
4
  attr_accessor :window_count
5
- def tmux cmd; "tmux #{cmd}" end
5
+ def tmux cmd; commands << "tmux #{cmd}" end
6
6
 
7
7
  def tmux_new_session cmd=nil
8
8
  tmux "new-session -s #{session_name} -d#{lprefix cmd}"
@@ -13,7 +13,7 @@ module Lab42
13
13
  end
14
14
 
15
15
  def tmux_source_file
16
- "tmux source-file #{options.source_file || File.join( ENV["HOME"], ".tmux.conf" )}"
16
+ tmux "source-file #{options.source_file || File.join( ENV["HOME"], ".tmux.conf" )}"
17
17
  end
18
18
 
19
19
  def lprefix cmd, prefix=" "
@@ -0,0 +1,8 @@
1
+ # Replace by lab42_core ASAP
2
+ #
3
+ class Hash
4
+ def with_present key, &blk
5
+ return nil unless has_key? key
6
+ blk.( fetch key )
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ # Replace by lab42_core ASAP
2
+ #
3
+ class Object
4
+ def with_present attribute, &blk
5
+ val = send attribute
6
+ return nil unless val
7
+ blk.( val )
8
+ rescue NoMethodError
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'forwarder'
2
+ class OpenStruct
3
+ extend Forwarder
4
+ forward :fetch, to: :to_h
5
+ end
@@ -0,0 +1,15 @@
1
+ module Lab42
2
+ class Tmux
3
+ module Helpers
4
+ def if_dir dir, &blk
5
+ return unless File.directory? expand_dir dir
6
+ instance_eval_or_call blk
7
+ end
8
+
9
+ def expand_dir dir
10
+ return dir if %r{\A/} === dir
11
+ File.join project_home, dir
12
+ end
13
+ end # module Helpers
14
+ end # class Tmux
15
+ end # module Lab42
@@ -0,0 +1,41 @@
1
+ require 'lab42/tmux'
2
+ require 'forwarder'
3
+
4
+ module Lab42
5
+ class Tmux
6
+ module Predefined
7
+ class RubySession
8
+ attr_reader :tmux
9
+ extend Forwarder
10
+ forward_all :exec!, :render, to: :tmux
11
+
12
+ private
13
+ def initialize *args, &blk
14
+ @tmux = Lab42::Tmux.new args do
15
+ add_window "repl", command: options.fetch(:repl,"irb")
16
+ add_window "vi", command: "vi ."
17
+ # require 'pry'
18
+ # binding.pry
19
+ if_dir "lib" do
20
+ add_window "lib", command: "vi lib" do
21
+ vim_command "colorscheme morning"
22
+ end
23
+ end
24
+ if_dir "spec" do
25
+ add_window "spec", command: "vi spec" do
26
+ vim_commands "colorscheme solarized", background: "light"
27
+ end
28
+ end
29
+ if_dir "features" do
30
+ add_window "features", command: "vi features"
31
+ add_window "cucu"
32
+ end
33
+ add_window "sh"
34
+ # TODO: Specify this later if needed
35
+ # instance_eval_or_call blk if blk
36
+ end
37
+ end
38
+ end # class RubySession
39
+ end # module Predefined
40
+ end # class Tmux
41
+ end # module Lab42
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Tmux
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -0,0 +1,30 @@
1
+ module Lab42
2
+ class Tmux
3
+ class Window
4
+ module Command
5
+ module VimCommand
6
+ def vim_commands *cmds
7
+ cmds.each do | cmd |
8
+ vim_command cmd
9
+ end
10
+ end
11
+
12
+ def vim_command cmd
13
+ case cmd
14
+ when Hash
15
+ vim_set_commands cmd
16
+ else
17
+ tmux_send_keys ":#{cmd}"
18
+ end
19
+ end
20
+
21
+ def vim_set_commands set_cmds
22
+ set_cmds.each do |key, val|
23
+ tmux_send_keys ":set #{key}=#{val}"
24
+ end
25
+ end
26
+ end # module VimCommand
27
+ end # module Command
28
+ end # class Window
29
+ end # class Tmux
30
+ end # module Lab42
@@ -1,25 +1,45 @@
1
1
  require 'forwarder'
2
+ require 'lab42/tmux/window/command/vim_command.rb'
2
3
  module Lab42
3
4
  class Tmux
4
5
  class Window
5
6
  module Command
7
+ include VimCommand
6
8
  extend Forwarder
7
- forward_all :lprefix, :session_name, :tmux, to: :session
9
+ forward_all :lprefix, :session_name, to: :session
10
+
11
+ def tmux *args
12
+ added_commands << session.tmux( *args )
13
+ end
8
14
 
9
15
  def tmux_new_window( rename: false )
10
- [ tmux( "new-window -t #{session_name}" ) ] +
16
+ [ tmux( "new-window -t #{session_name}#{new_window_name}" ) ] +
11
17
  (rename ? [] : [
12
18
  tmux( "set-window-option -t #{designation} automatic-rename off" ),
13
19
  tmux( "set-window-option -t #{designation} allow-rename off" )
14
- ])
20
+ ]) +
21
+ [tmux_cd_project_home] +
22
+ tmux_commands
15
23
  end
16
24
 
17
25
  def tmux_cd_project_home
18
26
  tmux_send_keys "cd #{session.project_home}"
19
27
  end
20
28
 
29
+ def tmux_commands
30
+ [(commands || [])].flatten.map{ |cmd|
31
+ tmux_send_keys cmd
32
+ }
33
+ end
21
34
  def tmux_send_keys keys, cm: true
22
- tmux "send-keys -t #{designation} #{keys.inspect}#{lprefix( cm && "C-m" )}"
35
+ tmux "send-keys -t #{designation} #{keys.inspect}#{lprefix( cm && "C-m" )}"
36
+ end
37
+
38
+ private
39
+ def new_window_name
40
+ with_present( :name ){ | name |
41
+ " -n #{name}"
42
+ }
23
43
  end
24
44
 
25
45
  end # module Command
@@ -1,22 +1,43 @@
1
1
  require 'lab42/tmux/window/command'
2
+ require 'forwarder'
2
3
  module Lab42
3
4
  class Tmux
4
5
  class Window
5
6
  include Command
6
- attr_accessor :number, :session
7
+ extend Forwarder
8
+ attr_reader :command, :name, :number, :session
9
+ forward_all :commands, :lprefix, :session_name, :tmux, to: :session
10
+
7
11
  def designation
8
- [session.session_name, number].join ":"
9
- end
10
- def render
11
- [
12
- tmux_new_window,
13
- tmux_cd_project_home
14
- ].flatten
12
+ [session_name, number].join ":"
15
13
  end
16
14
  private
17
- def initialize nb, session
18
- self.session = session
19
- self.number = nb
15
+ def initialize nb, session, options={}
16
+ @session = session
17
+ @number = nb
18
+ @name = options[:name]
19
+ @command = options[:command]
20
+ tmux_window_commands
21
+ end
22
+
23
+ def tmux_window_command cmd, params
24
+ tmux "#{tmuxize cmd} -t #{designation} #{params}"
25
+ end
26
+ def tmux_window_commands
27
+ tmux "new-window -t #{session_name}#{name ? " -n #{name}" : ""}"
28
+ tmux_window_options
29
+ tmux_cd_project_home
30
+ tmux_send_keys command if command
31
+ end
32
+
33
+ def tmux_window_options( options: {} )
34
+ [:automatic_rename, :allow_rename].each do |option|
35
+ tmux_window_command :set_window_option, "#{tmuxize option} #{options[option] ? "on" : "off"}"
36
+ end
37
+ end
38
+
39
+ def tmuxize sym
40
+ sym.to_s.gsub "_", "-"
20
41
  end
21
42
  end # class Window
22
43
  end # class Tmux
data/lib/lab42/tmux.rb CHANGED
@@ -2,20 +2,27 @@ require 'lab42/options'
2
2
  require 'lab42/tmux/core_helpers'
3
3
  require 'lab42/tmux/commands'
4
4
  require 'lab42/tmux/window'
5
+ require 'lab42/tmux/extensions/ostruct'
6
+ require 'lab42/tmux/extensions/hash'
7
+ require 'lab42/tmux/extensions/object'
8
+ require 'lab42/tmux/helpers'
5
9
 
6
10
  module Lab42
7
11
  class Tmux
8
12
  include Commands
13
+ include Helpers
9
14
 
10
- attr_accessor :options, :windows
11
- attr_reader :project_home, :session_name
15
+ attr_reader :commands, :options, :project_home, :session_name, :windows
12
16
 
13
- def add_window &blk
14
- self.windows << Window.new( windows.size, self )
15
- instance_eval_or_call blk if blk
17
+ def add_window name=nil, options={}, &blk
18
+ window = Window.new windows.size, self, name: name, command: options[:command]
19
+ windows << window
20
+
21
+ window.instance_eval_or_call blk if blk
16
22
  end
17
23
 
18
24
  def current_window; windows.last end
25
+ def current_window?; !!current_window end
19
26
 
20
27
  def exec!
21
28
  if options.dry_run
@@ -26,57 +33,41 @@ module Lab42
26
33
  end
27
34
 
28
35
  def render
29
- commands =
30
- render_new_session_or_empty +
31
- render_final_attach_command
32
-
33
- commands.join "\n"
36
+ (commands + final_attach_command).join "\n"
34
37
  end
35
38
 
36
39
  private
37
- def initialize &blk
38
- self.options = Lab42::Options.new.parse( *ARGV.dup )
40
+ def initialize args, &blk
41
+ @options = Lab42::Options.new.parse( *args.dup )
42
+ @commands = []
43
+ @windows = []
39
44
  process_options
40
45
  raise ArgumentError, "unable to determine session name" unless session_name
41
46
  raise ArgumentError, "no such directory #{project_home}" unless project_home && File.directory?( project_home )
42
- self.windows = [Window.new( 0, self )]
47
+ session_commands
43
48
  instance_eval_or_call blk
44
49
  end
45
50
 
46
-
47
51
  def process_options
48
52
  @project_home = options.args.first
49
53
  @session_name = File.basename project_home rescue nil
50
54
  end
51
55
 
52
- def render_final_attach_command
56
+ def final_attach_command
53
57
  [
54
- tmux( "attach -t #{session_name}" )
58
+ "tmux attach -t #{session_name}"
55
59
  ]
56
60
  end
57
61
 
58
- def render_new_session_or_empty
59
- if session_exists?
60
- []
61
- else
62
- render_session_commands +
63
- render_window_commands
64
- end
65
- end
66
-
67
- def render_session_commands
68
- [
69
- tmux_new_session,
70
- tmux_source_file,
71
- tmux_no_session_rename
72
- ].compact
73
- end
74
-
75
- def render_window_commands
76
- windows.map(&:render).flatten
62
+ def session_commands
63
+ return if session_exists?
64
+ tmux_new_session
65
+ tmux_source_file
66
+ tmux_no_session_rename
77
67
  end
78
68
 
79
69
  def session_exists?
70
+ return false if options.dry_run
80
71
  system "tmux has-session -t #{session_name}"
81
72
  $?.to_i.zero?
82
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_tmux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-06 00:00:00.000000000 Z
11
+ date: 2013-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lab42_options
@@ -100,20 +100,28 @@ description: A Gem that will give us helpers to dry up the creation of TMUX wind
100
100
  email: robert.dober@gmail.com
101
101
  executables:
102
102
  - lab42_tmux
103
+ - lab42_ruby_session
103
104
  extensions: []
104
105
  extra_rdoc_files: []
105
106
  files:
106
107
  - lib/lab42/tmux.rb
107
108
  - lib/lab42/tmux/core_helpers.rb
109
+ - lib/lab42/tmux/extensions/ostruct.rb
110
+ - lib/lab42/tmux/extensions/object.rb
111
+ - lib/lab42/tmux/extensions/hash.rb
108
112
  - lib/lab42/tmux/options.rb
113
+ - lib/lab42/tmux/helpers.rb
109
114
  - lib/lab42/tmux/version.rb
115
+ - lib/lab42/tmux/predefined/ruby_session.rb
110
116
  - lib/lab42/tmux/commands.rb
117
+ - lib/lab42/tmux/window/command/vim_command.rb
111
118
  - lib/lab42/tmux/window/command.rb
112
119
  - lib/lab42/tmux/command.rb
113
120
  - lib/lab42/tmux/window.rb
114
121
  - LICENSE
115
122
  - README.md
116
123
  - bin/lab42_tmux
124
+ - bin/lab42_ruby_session
117
125
  homepage: https://github.com/RobertDober/lab42_tmux
118
126
  licenses:
119
127
  - MIT