terminal_multiplexer 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,10 +17,10 @@ require 'terminal-multiplexer'
17
17
  tmux = Terminal::Multiplexer.new
18
18
  #create new session to hold all windows
19
19
  tmux.new_session
20
- tmux.new_session('window_name','command')
20
+ tmux.new_session(:session_name => 'session_name' , :window_name => 'window_name', :command => 'command')
21
21
  #create new window
22
22
  tmux.new_window
23
- tmux.new_window('window_name','command')
23
+ tmux.new_window(:window_name => 'window_name', :command => 'command')
24
24
  #start tmux
25
25
  tmux.start
26
26
  ```
@@ -10,8 +10,10 @@ class Default < Thor
10
10
  tmux.new_session
11
11
  tmux.new_window
12
12
  tmux.new_window
13
+ tmux.new_session(:session_name=>'session1',:window_name=>'window1',:command=>'zsh')
13
14
  tmux.new_window
14
- tmux.new_window('tests', 'guard')
15
+ tmux.new_window(:name => 'tests', :command=>'guard')
16
+ tmux.active_window(2)
15
17
  tmux.start
16
18
  end
17
19
  end
data/env CHANGED
@@ -11,7 +11,7 @@ class Default < Thor
11
11
  tmux.new_window
12
12
  tmux.new_window
13
13
  tmux.new_window
14
- tmux.new_window('tests', 'guard')
14
+ tmux.new_window(:name => 'tests', :command => 'guard')
15
15
  tmux.active_window(2)
16
16
  tmux.start
17
17
  end
@@ -9,39 +9,62 @@ module Terminal
9
9
  class Multiplexer
10
10
  include Exceptions
11
11
 
12
-
13
- def initialize(*options)
14
- # @options = {
15
- # :default_cmd => 'zsh'
16
- # }.update options
17
- #
12
+ def initialize
18
13
  @sessions = []
19
14
  end
20
15
 
21
- def new_session(name='default', command='zsh')
22
- if @sessions.find { |s| s.name == name }
23
- raise DuplicateSessionIdentifier , "Please do not reuse the session identifier. You used \"#{name}\" at least twice."
16
+ # Creates a new tmux session
17
+ #
18
+ # @session_name [String] name the name of the session
19
+ # @window_name [String] name the name of first window of the session
20
+ # @command [String] command the command which should be started in the first window of the session
21
+ def new_session(options={})
22
+
23
+ options = {
24
+ :session_name => 'default',
25
+ :window_name => 'tab',
26
+ :command => 'zsh'
27
+ }.update options
28
+
29
+ if @sessions.find { |s| s.session_name == options[:session_name] }
30
+ raise DuplicateSessionIdentifier , "Please do not reuse the session identifier. You used \"#{options[:session_name]}\" at least twice."
24
31
  end
25
32
 
26
- session = Session.new(name, command)
33
+ session = Session.new(options[:session_name], options[:window_name], options[:command])
27
34
  @sessions << session
28
35
  session
29
36
  end
30
37
 
38
+ # Creates a new tmux window
39
+ #
40
+ # @name [String] name the name of the window, which should be created
41
+ # @command [String] command the command which should be started in the created window
42
+ def new_window(options={})
43
+
44
+ options = {
45
+ :name => 'tab',
46
+ :command => 'zsh'
47
+ }.update options
31
48
 
32
- def new_window(name='tab', command='zsh')
49
+ # windows will be attached to the last session defined
33
50
  session = get_last_session
34
51
 
52
+ #if no sessions have been created, create a new one
35
53
  if session == nil
36
54
  say "Warning: No valid session found. Create a default one"
37
55
  session = new_session
38
56
  end
39
-
40
- session.add_window Window.new( name , command )
57
+
58
+ #windows belong to a session
59
+ session.add_window Window.new( options[:name] , options[:command] )
41
60
 
42
61
  end
43
-
62
+
63
+ # Set the active window in that session
64
+ #
65
+ # @num [Integer] number the number of window which should be active in that tmux session
44
66
  def active_window(num)
67
+ # the active window will always be set for the last defined session
45
68
  session = get_last_session
46
69
  session.active_window(num)
47
70
  end
@@ -49,10 +72,13 @@ module Terminal
49
72
 
50
73
  private
51
74
 
75
+ # Get the last defined session
52
76
  def get_last_session
53
77
  @sessions[-1]
54
78
  end
55
79
 
80
+ # Build the parameter string for all the sessions + windows
81
+ # to be used as a parameter for tmux to create that sessions and windows
56
82
  def build_multiplexer_command_string
57
83
  m_str = @sessions.collect{ |s| s.plain}.join(' ')
58
84
  m_str
@@ -60,10 +86,13 @@ module Terminal
60
86
 
61
87
  public
62
88
 
89
+ # Start tmux
90
+ #
91
+ # @process_info [Hash] process_information a hash containing information about the tmux process to be started
63
92
  def start(process_info={})
64
93
 
65
94
  process_info[:cmd] = 'tmux'
66
- process_info[:status] = system("#{process_info[:cmd]} #{build_multiplexer_command_string}")
95
+ process_info[:status] = system("#{process_info[:cmd]} #{build_multiplexer_command_string} attach")
67
96
 
68
97
  end
69
98
 
@@ -1,9 +1,16 @@
1
1
  module Terminal
2
2
  module Exceptions
3
3
 
4
+ # Exception which should be raised
5
+ # if no session is found which can be used
6
+ # for that particular command
7
+ #
4
8
  class SessionNotFound < Exception
5
9
  end
6
10
 
11
+ # If you create a session you could choose an identifier
12
+ # on your own. Every Session has to have its own
13
+ # unique session identifier.
7
14
  class DuplicateSessionIdentifier < Exception
8
15
  end
9
16
 
@@ -1,50 +1,67 @@
1
1
  module Terminal
2
2
  class Session
3
- attr_reader :name, :command
3
+ attr_reader :session_name, :window_name, :command
4
4
 
5
- def initialize(name = 'default', command = 'zsh')
5
+ # Create the session
6
+ #
7
+ # @session_name [String] session_name the name of the session
8
+ # @window_name [String] window_name the name of the first window of the session
9
+ # @command [String] command the command which should be executed in the first window of the session
10
+ def initialize(session_name, window_name, command)
6
11
 
7
- name = 'default' if name == nil or name == ''
8
- command = 'zsh' if command == nil or command == ''
9
-
10
- @name = name
12
+ @session_name = session_name
13
+ @window_name = window_name
11
14
  @command = command
12
15
  @windows = []
13
16
  @active_window = 1
14
17
  end
15
18
 
19
+ # Set the active window in that session
20
+ #
21
+ # @num [Integer] number the number of the active window of that session
16
22
  def active_window(num)
17
23
  @active_window=num
18
24
  end
19
25
 
26
+ # Build the whole session string for tmux
27
+ #
20
28
  def plain
21
29
  w_defs ||= build_window_string
22
- w_opts ||= build_window_options
30
+ s_opts ||= build_session_options
23
31
 
24
- "new-session #{w_opts} #{command}\\;#{w_defs}"
32
+ "new-session #{s_opts} #{command}\\;#{w_defs}"
25
33
  end
26
34
 
27
35
  private
28
36
 
29
- def build_window_options
30
- "-n #{name}"
37
+ # Build the session options
38
+ #
39
+ def build_session_options
40
+ "-s #{session_name} -n #{window_name}"
31
41
  end
32
42
 
33
43
 
44
+ # Build the string containing all created windows
45
+ #
34
46
  def build_window_string
35
47
  w_str = ''
36
48
 
49
+ #strings for the windows
37
50
  if @windows.count > 0
38
51
  w_str += ' '
39
52
  w_str += @windows.collect{|w| w.plain}.join(' ')
40
53
  end
41
54
 
55
+ #string for the selected window in that session
42
56
  w_str += " select-window -t:+#{@active_window}\\;"
43
57
 
44
58
  end
45
59
 
46
60
  public
47
61
 
62
+ # Add a window to the session
63
+ #
64
+ # @window [Window] window the object containing the window which should be added to the session
48
65
  def add_window(window)
49
66
  @windows << window
50
67
  end
@@ -1,3 +1,3 @@
1
1
  module Terminal
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,6 +2,10 @@ module Terminal
2
2
  class Window
3
3
  attr_accessor :name, :command
4
4
 
5
+ # Create a tmux window
6
+ #
7
+ # @name [String] name the name of the window
8
+ # @command [String] command the command executed in the window
5
9
  def initialize(name = 'tab', command = 'zsh')
6
10
 
7
11
  name = 'tab' if name == nil or name == ''
@@ -12,6 +16,8 @@ module Terminal
12
16
  @options = []
13
17
  end
14
18
 
19
+ # Return the tmux string for the window
20
+ #
15
21
  def plain
16
22
  @options.push "-n #{name}"
17
23
  "new-window #{@options.join(' ')} #{command}\\;"
@@ -2,36 +2,33 @@ require 'spec_helper'
2
2
 
3
3
  module Terminal
4
4
  describe Session do
5
- let(:sess) { Session.new }
5
+ let(:sess) { Session.new('default','tab','zsh') }
6
6
 
7
7
  describe "run with rubbish options" do
8
- let(:sess) { Session.new(nil,nil) }
8
+ let(:sess) { Session.new(nil,nil,nil) }
9
9
 
10
- it "has a name" do
11
- sess.name.should == 'default'
10
+ it "has a session name" do
11
+ sess.session_name.should == nil
12
12
  end
13
13
 
14
- it " has a command associated with it" do
15
- sess.command.should == 'zsh'
16
- end
17
- end
18
-
19
- describe "run with no option" do
20
-
21
- it "has a name" do
22
- sess.name.should == 'default'
14
+ it "has a window name" do
15
+ sess.window_name.should == nil
23
16
  end
24
17
 
25
- it " has a command associated with it" do
26
- sess.command.should == 'zsh'
18
+ it "has a command associated with it" do
19
+ sess.command.should == nil
27
20
  end
28
21
  end
29
22
 
30
23
  describe "run with option" do
31
- let(:sess) { Session.new('session','ksh') }
24
+ let(:sess) { Session.new('session','tab','ksh') }
25
+
26
+ it "has a session name" do
27
+ sess.session_name.should == 'session'
28
+ end
32
29
 
33
- it "has a name" do
34
- sess.name.should == 'session'
30
+ it "has a window name" do
31
+ sess.window_name.should == 'tab'
35
32
  end
36
33
 
37
34
  it "has a command associated with it" do
@@ -41,7 +38,7 @@ module Terminal
41
38
 
42
39
  describe "#plain" do
43
40
  it "returns the correct tmux string" do
44
- sess.plain.should == "new-session -n default zsh\\; select-window -t:+1\\;"
41
+ sess.plain.should == "new-session -s default -n tab zsh\\; select-window -t:+1\\;"
45
42
  end
46
43
  end
47
44
 
@@ -53,7 +50,7 @@ module Terminal
53
50
 
54
51
  it "returns the correct tmux string" do
55
52
  sess.add_window(@win)
56
- sess.plain.should == "new-session -n default zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
53
+ sess.plain.should == "new-session -s default -n tab zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
57
54
  end
58
55
  end
59
56
 
@@ -2,3 +2,8 @@ $LOAD_PATH << File.expand_path('../lib/terminal-multiplexer' , File.dirname(__FI
2
2
 
3
3
  require 'terminal-multiplexer'
4
4
  require 'pry'
5
+
6
+ RSpec.configure do |c|
7
+ c.treat_symbols_as_metadata_keys_with_true_values = true
8
+ end
9
+
@@ -11,18 +11,18 @@ module Terminal
11
11
 
12
12
  it "creates a new session with default name" do
13
13
  multi.new_session
14
- multi.send(:build_multiplexer_command_string).should == "new-session -n default zsh\\; select-window -t:+1\\;"
14
+ multi.send(:build_multiplexer_command_string).should == "new-session -s default -n tab zsh\\; select-window -t:+1\\;"
15
15
  end
16
16
 
17
17
  it "creates a new session with given name" do
18
- multi.new_session('session1','ksh')
19
- multi.send(:build_multiplexer_command_string).should == "new-session -n session1 ksh\\; select-window -t:+1\\;"
18
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
19
+ multi.send(:build_multiplexer_command_string).should == "new-session -s session1 -n tab ksh\\; select-window -t:+1\\;"
20
20
  end
21
21
 
22
22
  it "creates a string for all sessions created" do
23
23
  multi.new_session
24
- multi.new_session('session1','ksh')
25
- multi.send(:build_multiplexer_command_string).should == "new-session -n default zsh\\; select-window -t:+1\\; new-session -n session1 ksh\\; select-window -t:+1\\;"
24
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
25
+ multi.send(:build_multiplexer_command_string).should == "new-session -s default -n tab zsh\\; select-window -t:+1\\; new-session -s session1 -n tab ksh\\; select-window -t:+1\\;"
26
26
  end
27
27
 
28
28
  it "raises an exception if a session identifier is reused" do
@@ -36,47 +36,47 @@ module Terminal
36
36
  describe "#new_window" do
37
37
  it "creates a new window for the default session" do
38
38
  multi.new_window
39
- multi.send(:build_multiplexer_command_string).should == "new-session -n default zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
39
+ multi.send(:build_multiplexer_command_string).should == "new-session -s default -n tab zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
40
40
  end
41
41
  it "creates a new window for \"session1\"" do
42
- multi.new_session('session1','ksh')
42
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
43
43
  multi.new_window
44
- multi.send(:build_multiplexer_command_string).should == "new-session -n session1 ksh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
44
+ multi.send(:build_multiplexer_command_string).should == "new-session -s session1 -n tab ksh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
45
45
  end
46
46
 
47
47
  it "creates a new window for session \"default\"" do
48
48
  multi.new_session
49
49
  multi.new_window
50
- multi.send(:build_multiplexer_command_string).should == "new-session -n default zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
50
+ multi.send(:build_multiplexer_command_string).should == "new-session -s default -n tab zsh\\; new-window -n tab zsh\\; select-window -t:+1\\;"
51
51
  end
52
52
 
53
53
  it "creates 2 windows in session1 and 2 windows in session2 (the last defined session, after creating the windows)" do
54
- multi.new_session('session1','ksh')
55
- multi.new_window('window1','ksh')
56
- multi.new_window('window2','ksh')
57
- multi.new_session('session2','ksh')
58
- multi.new_window('window3','ksh')
59
- multi.new_window('window4','ksh')
60
- multi.send(:build_multiplexer_command_string).should == "new-session -n session1 ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+1\\; new-session -n session2 ksh\\; new-window -n window3 ksh\\; new-window -n window4 ksh\\; select-window -t:+1\\;"
54
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
55
+ multi.new_window(:name => 'window1', :command => 'ksh')
56
+ multi.new_window(:name => 'window2', :command => 'ksh')
57
+ multi.new_session(:session_name => 'session2', :window_name => 'tab', :command => 'ksh')
58
+ multi.new_window(:name => 'window3', :command => 'ksh')
59
+ multi.new_window(:name => 'window4', :command => 'ksh')
60
+ multi.send(:build_multiplexer_command_string).should == "new-session -s session1 -n tab ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+1\\; new-session -s session2 -n tab ksh\\; new-window -n window3 ksh\\; new-window -n window4 ksh\\; select-window -t:+1\\;"
61
61
  end
62
62
 
63
63
  end
64
64
 
65
65
  describe "#active_window" do
66
66
  it "selects the window from the last defined session" do
67
- multi.new_session('session1','ksh')
68
- multi.new_window('window1','ksh')
69
- multi.new_window('window2','ksh')
67
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
68
+ multi.new_window(:name => 'window1', :command => 'ksh')
69
+ multi.new_window(:name => 'window2', :command => 'ksh')
70
70
  multi.active_window(2)
71
- multi.send(:build_multiplexer_command_string).should == "new-session -n session1 ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+2\\;"
71
+ multi.send(:build_multiplexer_command_string).should == "new-session -s session1 -n tab ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+2\\;"
72
72
  end
73
73
 
74
74
  it "doesn't matter where you place the window selector. It selects the window from the last defined session" do
75
- multi.new_session('session1','ksh')
75
+ multi.new_session(:session_name => 'session1', :window_name => 'tab', :command => 'ksh')
76
76
  multi.active_window(2)
77
- multi.new_window('window1','ksh')
78
- multi.new_window('window2','ksh')
79
- multi.send(:build_multiplexer_command_string).should == "new-session -n session1 ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+2\\;"
77
+ multi.new_window(:name => 'window1', :command => 'ksh')
78
+ multi.new_window(:name => 'window2', :command => 'ksh')
79
+ multi.send(:build_multiplexer_command_string).should == "new-session -s session1 -n tab ksh\\; new-window -n window1 ksh\\; new-window -n window2 ksh\\; select-window -t:+2\\;"
80
80
  end
81
81
  end
82
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal_multiplexer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: highline
16
- requirement: &22895840 !ruby/object:Gem::Requirement
16
+ requirement: &13995980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *22895840
24
+ version_requirements: *13995980
25
25
  description:
26
26
  email:
27
27
  - dev@fedux.org