rtmux 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/rtmux +179 -0
  3. data/lib/rtmux.rb +104 -0
  4. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a332f0ab076b868caeba90ccd342daad183a728
4
+ data.tar.gz: e04290ecbd85ddff1b415fcc02cc020e8ca246ac
5
+ SHA512:
6
+ metadata.gz: 91ad374d37ebf97e6e4d517f335ea4dd5bf49d505f01389dca47a2300031da0e80e8625a6cf399c055f6aa12f9b5357b64a7a752989735fc96cbcba061d81b2d
7
+ data.tar.gz: 115c596e9af48535b3c3987e8619145c694dd4d39771c168df02f19a7fe341f7a33701525b964155bdb0bb706ae4c22c0c70e1636ddf51a8696e3cce5888dd0f
data/bin/rtmux ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: UTF-8
3
+
4
+ # rtmux
5
+ #
6
+ # excutable for rtmux.rb
7
+ #
8
+ # created on : 2013.08.12
9
+ # last update: 2013.08.13
10
+ #
11
+ # by meinside@gmail.com
12
+
13
+ require "yaml"
14
+ require "thor"
15
+
16
+ require "rtmux"
17
+
18
+ module RTmux
19
+
20
+ CONFIG_FILEPATH = File.expand_path("~/.rtmux.conf")
21
+
22
+ # predefined variables:
23
+ # %d: current directory(where this script is run)'s name
24
+ # %h: host name
25
+ # %Y: year (eg. 2012)
26
+ # %M: month (eg. 11)
27
+ # %D: day (eg. 22)
28
+ RTMUX_CONFIG_EXAMPLE = <<EXAMPLE
29
+ ---
30
+ rails: # for rails projects
31
+ session_name: "%d"
32
+ windows:
33
+ - name: console
34
+ - name: models
35
+ cmd: cd ./app/models; clear
36
+ - name: views
37
+ cmd: cd ./app/views; clear
38
+ - name: controllers
39
+ cmd: cd ./app/controllers; clear
40
+ - name: configs
41
+ cmd: cd ./config; clear
42
+ - name: server
43
+ split:
44
+ vertical: true
45
+ percentage: 50
46
+ panes:
47
+ - pane: 1
48
+ cmd: rails server
49
+ - pane: 2
50
+ cmd: rails console
51
+ focus:
52
+ name: console
53
+ pane: 1
54
+ EXAMPLE
55
+
56
+ # replace text for tmux session/window names
57
+ # @param txt [String] text
58
+ # @return [String] replaced text
59
+ def self.replace(txt)
60
+ unless txt
61
+ nil
62
+ else
63
+ txt
64
+ .gsub("%d", File.basename(Dir.pwd))
65
+ .gsub("%h", `hostname`)
66
+ .gsub("%Y", "%04d" %[Time.now.year])
67
+ .gsub("%M", "%02d" %[Time.now.month])
68
+ .gsub("%D", "%02d" %[Time.now.day])
69
+ end
70
+ end
71
+
72
+ # parse config with given filepath or text
73
+ # @param config_filepath_or_data [String] filepath or text
74
+ # @return [Hash] parsed config
75
+ def self.parse_config(config_filepath_or_data)
76
+ if File.exists? config_filepath_or_data
77
+ YAML.load_file(config_filepath_or_data)
78
+ else
79
+ YAML.load(config_filepath_or_data)
80
+ end
81
+ rescue
82
+ puts "# yml parse error: #{$!}"
83
+ return nil
84
+ end
85
+
86
+ class Exec < Thor
87
+ desc "launch [SESSION]", "Starts/resumes a session with given SESSION name"
88
+ long_desc <<LAUNCH_DESC
89
+ You can preset sessions by placing a yml file named '.rtmux.conf' on $HOME.
90
+
91
+ $ rtmux
92
+
93
+ or
94
+
95
+ $ rtmux launch
96
+
97
+ => Starts/resumes a session with default name(= hostname)
98
+
99
+
100
+ $ rtmux [SESSION]
101
+
102
+ or
103
+
104
+ $ rtmux launch [SESSION]
105
+
106
+ => Starts/resumes a session with given SESSION name
107
+ LAUNCH_DESC
108
+ method_option :verbose, type: :boolean
109
+ def launch(session = nil)
110
+ verbose = options.verbose?
111
+
112
+ # read config
113
+ if File.exists? CONFIG_FILEPATH
114
+ puts "> Loading config file: #{CONFIG_FILEPATH}" if verbose
115
+ configs = RTmux::parse_config(CONFIG_FILEPATH) || {}
116
+ else
117
+ puts "> Loading sample config" if verbose
118
+ configs = RTmux::parse_config(RTMUX_CONFIG_EXAMPLE) || {}
119
+ end
120
+ config = configs[session] || {}
121
+
122
+ # create/resume session
123
+ RTmux::TmuxHelper.new(RTmux::replace(config["session_name"]) || session){|tmux|
124
+ unless tmux.session_created?
125
+ # create, split windows and run commands in them
126
+ if windows = config["windows"]
127
+ windows.each{|window|
128
+ window_name = RTmux::replace(window["name"])
129
+ tmux.create_window(window_name, cmd: RTmux::replace(window["cmd"]))
130
+
131
+ if split = window["split"]
132
+ tmux.split(window_name, vertical: split["vertical"], percentage: split["percentage"])
133
+ if panes = split["panes"]
134
+ panes.each{|pane|
135
+ tmux.cmd(RTmux::replace(pane["cmd"]), window_name, pane: pane["pane"])
136
+ }
137
+ end
138
+ end
139
+ }
140
+ else
141
+ tmux.create_window # blank window
142
+ end
143
+
144
+ # focus on window
145
+ if focus = config["focus"]
146
+ tmux.focus(RTmux::replace(focus["name"]), pane: focus["pane"])
147
+ end
148
+ end
149
+
150
+ # finally, attach to it
151
+ tmux.attach
152
+ }
153
+ end
154
+
155
+ desc "genconfig", "Generate a sample config file at #{CONFIG_FILEPATH}"
156
+ method_option :overwrite, type: :boolean
157
+ def genconfig
158
+ overwrite = options.overwrite?
159
+ if !overwrite && File.exists?(CONFIG_FILEPATH)
160
+ puts "* config file already exists at: #{CONFIG_FILEPATH}"
161
+ else
162
+ File.open(CONFIG_FILEPATH, "w"){|f|
163
+ f.write RTMUX_CONFIG_EXAMPLE
164
+ }
165
+ puts "* sample config file was written at: #{CONFIG_FILEPATH}"
166
+ end
167
+ end
168
+
169
+ def method_missing(method, *args)
170
+ launch method.to_s
171
+ end
172
+
173
+ # when run without any command, it just launches with no parameter
174
+ default_task :launch
175
+ end
176
+ end
177
+
178
+ RTmux::Exec::start(ARGV)
179
+
data/lib/rtmux.rb ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: UTF-8
3
+
4
+ # lib/rtmux.rb
5
+ #
6
+ # help create/resume tmux sessions
7
+ #
8
+ # created on : 2012.11.22
9
+ # last update: 2013.08.13
10
+ #
11
+ # by meinside@gmail.com
12
+
13
+ module RTmux
14
+
15
+ =begin
16
+ tmux helper class
17
+
18
+ @note example:
19
+ tmux = TmuxHelper.new("some_session_name")
20
+
21
+ tmux.create_window("window1")
22
+ tmux.create_window("window2")
23
+ tmux.create_window("logs", cmd: "cd /var/log; ls")
24
+
25
+ tmux.split("window1")
26
+ tmux.focus("window1", pane: 2)
27
+
28
+ tmux.split("window2", vertical: false, percentage: 30)
29
+
30
+ tmux.attach
31
+ =end
32
+ class TmuxHelper
33
+ # initializer
34
+ # @param session_name [String] session name
35
+ def initialize(session_name, &block)
36
+ @session_name = session_name || `hostname`.strip
37
+
38
+ if block_given?
39
+ yield self
40
+ end
41
+ end
42
+
43
+ # check if session is already created or not
44
+ # @return [true,false]
45
+ def session_created?
46
+ `tmux has-session -t #{@session_name} 2> /dev/null`
47
+ $?.exitstatus != 1
48
+ end
49
+
50
+ # check if window is already created or not
51
+ # @param window_name [String] window name
52
+ # @return [true,false]
53
+ def window_created?(window_name)
54
+ window_name ? `tmux list-windows -t #{@session_name} -F \"\#{window_name}\" 2> /dev/null` =~ /^#{window_name}$/ : false
55
+ end
56
+
57
+ # create window with given name and options
58
+ # @param name [String] window name
59
+ # @param options [Hash] options
60
+ def create_window(name = nil, options = {cmd: nil})
61
+ if session_created?
62
+ unless window_created?(name)
63
+ `tmux new-window -t #{@session_name} #{name.nil? ? "" : "-n #{name}"}`
64
+ else
65
+ return # don't create duplicated windows
66
+ end
67
+ else
68
+ `tmux new-session -s #{@session_name} #{name.nil? ? "" : "-n #{name}"} -d`
69
+ end
70
+ cmd(options[:cmd], name) if options && options[:cmd]
71
+ end
72
+
73
+ # execute command
74
+ # @param cmd [String] command
75
+ # @param window [String] window
76
+ # @param options [Hash] options
77
+ def cmd(cmd, window, options = {pane: nil})
78
+ `tmux send-keys -t #{@session_name}:#{window}#{options && options[:pane] ? ".#{options[:pane]}" : ""} '#{cmd}' C-m`
79
+ end
80
+
81
+ # focus on given window
82
+ # @param window [String] window
83
+ # @param options [Hash] options
84
+ def focus(window, options = {pane: nil})
85
+ `tmux select-window -t #{@session_name}:#{window}`
86
+ `tmux select-pane -t #{options[:pane]}` if options && options[:pane]
87
+ end
88
+
89
+ # split given window
90
+ # @param window [String] window
91
+ # @param options [Hash] options
92
+ def split(window, options = {vertical: true, percentage: 50, pane: nil})
93
+ `tmux split-window #{options && options[:vertical] ? "-h" : "-v"} -p #{options[:percentage]} -t #{@session_name}:#{window}#{options && options[:pane] ? ".#{options[:pane]}" : ""}`
94
+ end
95
+
96
+ # attach to current session
97
+ def attach
98
+ `tmux attach -t #{@session_name}`
99
+ end
100
+
101
+ attr_reader :session_name
102
+ end
103
+ end
104
+
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtmux
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sungjin Han
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: help create/resume tmux sessions
28
+ email: meinside@gmail.com
29
+ executables:
30
+ - rtmux
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/rtmux.rb
35
+ - bin/rtmux
36
+ homepage: http://github.com/meinside/rtmux
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.0.3
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: tmux helper
60
+ test_files: []