Class: Tmuxinator::Cli
- Inherits:
-
Thor
- Object
- Thor
- Tmuxinator::Cli
- Includes:
- Util
- Defined in:
- lib/tmuxinator/cli.rb
Constant Summary collapse
- COMMANDS =
{ commands: "Lists commands available in tmuxinator", completions: "Used for shell completion", new: "Create a new project file and open it in your editor", edit: "Alias of new", open: "Alias of new", start: %w{ Start a tmux session using a project's name (with an optional [ALIAS] for project reuse) or a path to a project config file (via the -p flag) }.join(" "), stop: "Stop a tmux session using a project's tmuxinator config", local: "Start a tmux session using ./.tmuxinator.y[a]ml", debug: "Output the shell commands that are generated by tmuxinator", copy: %w{ Copy an existing project to a new project and open it in your editor }.join(" "), delete: "Deletes given project", implode: "Deletes all tmuxinator projects", version: "Display installed tmuxinator version", doctor: "Look for problems in your configuration", list: "Lists all tmuxinator projects" }.freeze
- THOR_COMMANDS =
For future reference: due to how tmuxinator currently consumes command-line arguments (see ::bootstrap, below), invocations of Thor’s base commands (i.e. ‘help’, etc) can be instead routed to #start (rather than to ::start). In order to prevent this, the THOR_COMMANDS and RESERVED_COMMANDS constants have been introduced. The former enumerates any/all Thor commands we want to insure get passed through to Thor.start. The latter is the superset of the Thor commands and any tmuxinator commands, defined in COMMANDS, above.
%w[-v help].freeze
- RESERVED_COMMANDS =
(COMMANDS.keys + THOR_COMMANDS).map(&:to_s).freeze
Class Method Summary collapse
-
.bootstrap(args = []) ⇒ Object
This method was defined as something of a workaround…
-
.exit_on_failure? ⇒ Boolean
By default, Thor returns exit(0) when an error occurs.
Instance Method Summary collapse
- #commands(shell = nil) ⇒ Object
- #completions(arg) ⇒ Object
- #copy(existing, new) ⇒ Object
- #debug(name = nil, *args) ⇒ Object
- #delete(*projects) ⇒ Object
- #doctor ⇒ Object
- #implode ⇒ Object
- #list ⇒ Object
- #local ⇒ Object
- #new(name, session = nil) ⇒ Object
- #start(name = nil, *args) ⇒ Object
- #stop(name = nil) ⇒ Object
- #version ⇒ Object
Methods included from Util
Class Method Details
.bootstrap(args = []) ⇒ Object
This method was defined as something of a workaround… Previously the conditional contained within was in the executable (i.e. bin/tmuxinator). It has been moved here so as to be testable. A couple of notes:
-
::start (defined in Thor::Base) expects the first argument to be an
array or ARGV, not a varargs. Perhaps ::bootstrap should as well?
-
::start has a different purpose from #start and hence a different
signature
432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/tmuxinator/cli.rb', line 432 def self.bootstrap(args = []) name = args[0] || nil if args.empty? && Tmuxinator::Config.local? Tmuxinator::Cli.new.local elsif name && !Tmuxinator::Cli::RESERVED_COMMANDS.include?(name) && Tmuxinator::Config.exist?(name: name) Tmuxinator::Cli.new.start(name, *args.drop(1)) else Tmuxinator::Cli.start(args) end end |
.exit_on_failure? ⇒ Boolean
By default, Thor returns exit(0) when an error occurs. Please see: github.com/tmuxinator/tmuxinator/issues/192
9 10 11 |
# File 'lib/tmuxinator/cli.rb', line 9 def self.exit_on_failure? true end |
Instance Method Details
#commands(shell = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tmuxinator/cli.rb', line 55 def commands(shell = nil) out = if shell == "zsh" COMMANDS.map do |command, desc| "#{command}:#{desc}" end.join("\n") else COMMANDS.keys.join("\n") end say out end |
#completions(arg) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/tmuxinator/cli.rb', line 69 def completions(arg) if %w(start stop edit open copy delete).include?(arg) configs = Tmuxinator::Config.configs say configs.join("\n") end end |
#copy(existing, new) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/tmuxinator/cli.rb', line 340 def copy(existing, new) existing_config_path = Tmuxinator::Config.project(existing) new_config_path = Tmuxinator::Config.project(new) exit!("Project #{existing} doesn't exist!") \ unless Tmuxinator::Config.exist?(name: existing) new_exists = Tmuxinator::Config.exist?(name: new) question = "#{new} already exists, would you like to overwrite it?" if !new_exists || yes?(question, :red) say "Overwriting #{new}" if Tmuxinator::Config.exist?(name: new) FileUtils.copy_file(existing_config_path, new_config_path) end Kernel.system("$EDITOR #{new_config_path}") end |
#debug(name = nil, *args) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/tmuxinator/cli.rb', line 316 def debug(name = nil, *args) # project-config takes precedence over a named project in the case that # both are provided. if ["project-config"] args.unshift name if name name = nil end params = { args: args, attach: [:attach], custom_name: [:name], name: name, project_config: ["project-config"] } project = create_project(params) say project.render end |
#delete(*projects) ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/tmuxinator/cli.rb', line 361 def delete(*projects) projects.each do |project| if Tmuxinator::Config.exist?(name: project) config = Tmuxinator::Config.project(project) if yes?("Are you sure you want to delete #{project}?(y/n)", :red) FileUtils.rm(config) say "Deleted #{project}" end else say "#{project} does not exist!" end end end |
#doctor ⇒ Object
413 414 415 416 417 418 419 420 421 422 |
# File 'lib/tmuxinator/cli.rb', line 413 def doctor say "Checking if tmux is installed ==> " yes_no Tmuxinator::Doctor.installed? say "Checking if $EDITOR is set ==> " yes_no Tmuxinator::Doctor.editor? say "Checking if $SHELL is set ==> " yes_no Tmuxinator::Doctor.shell? end |
#implode ⇒ Object
379 380 381 382 383 384 385 386 |
# File 'lib/tmuxinator/cli.rb', line 379 def implode if yes?("Are you sure you want to delete all tmuxinator configs?", :red) Tmuxinator::Config.directories.each do |directory| FileUtils.remove_dir(directory) end say "Deleted all tmuxinator projects." end end |
#list ⇒ Object
395 396 397 398 399 400 401 402 |
# File 'lib/tmuxinator/cli.rb', line 395 def list say "tmuxinator projects:" if [:newline] say Tmuxinator::Config.configs.join("\n") else print_in_columns Tmuxinator::Config.configs end end |
#local ⇒ Object
299 300 301 302 303 304 305 |
# File 'lib/tmuxinator/cli.rb', line 299 def local show_version_warning if version_warning?( ["suppress-tmux-version-warning"] ) render_project(create_project(attach: [:attach])) end |
#new(name, session = nil) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/tmuxinator/cli.rb', line 86 def new(name, session = nil) if session new_project_with_session(name, session) else new_project(name) end end |
#start(name = nil, *args) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/tmuxinator/cli.rb', line 244 def start(name = nil, *args) # project-config takes precedence over a named project in the case that # both are provided. if ["project-config"] args.unshift name if name name = nil end params = { args: args, attach: [:attach], custom_name: [:name], name: name, project_config: ["project-config"] } show_version_warning if version_warning?( ["suppress-tmux-version-warning"] ) project = create_project(params) render_project(project) end |
#stop(name = nil) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/tmuxinator/cli.rb', line 275 def stop(name = nil) # project-config takes precedence over a named project in the case that # both are provided. if ["project-config"] name = nil end params = { name: name, project_config: ["project-config"] } show_version_warning if version_warning?( ["suppress-tmux-version-warning"] ) project = create_project(params) kill_project(project) end |
#version ⇒ Object
407 408 409 |
# File 'lib/tmuxinator/cli.rb', line 407 def version say "tmuxinator #{Tmuxinator::VERSION}" end |