Class: Tmuxinator::Cli

Inherits:
Thor
  • Object
show all
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.yml",
  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

Instance Method Summary collapse

Methods included from Util

#exit!, #yes_no

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



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/tmuxinator/cli.rb', line 416

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.exists?(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

Returns:

  • (Boolean)


7
8
9
# File 'lib/tmuxinator/cli.rb', line 7

def self.exit_on_failure?
  true
end

Instance Method Details

#commands(shell = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tmuxinator/cli.rb', line 53

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



67
68
69
70
71
72
# File 'lib/tmuxinator/cli.rb', line 67

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/tmuxinator/cli.rb', line 330

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.exists?(name: existing)

  new_exists = Tmuxinator::Config.exists?(name: new)
  question = "#{new} already exists, would you like to overwrite it?"
  if !new_exists || yes?(question, :red)
    say "Overwriting #{new}" if Tmuxinator::Config.exists?(name: new)
    FileUtils.copy_file(existing_config_path, new_config_path)
  end

  Kernel.system("$EDITOR #{new_config_path}")
end

#debug(name = nil, *args) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/tmuxinator/cli.rb', line 306

def debug(name = nil, *args)
  # project-config takes precedence over a named project in the case that
  # both are provided.
  if options["project-config"]
    args.unshift name if name
    name = nil
  end

  params = {
    args: args,
    attach: options[:attach],
    custom_name: options[:name],
    name: name,
    project_config: options["project-config"]
  }

  project = create_project(params)
  say project.render
end

#delete(*projects) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/tmuxinator/cli.rb', line 351

def delete(*projects)
  projects.each do |project|
    if Tmuxinator::Config.exists?(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

#doctorObject



397
398
399
400
401
402
403
404
405
406
# File 'lib/tmuxinator/cli.rb', line 397

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

#implodeObject



369
370
371
372
373
374
375
376
# File 'lib/tmuxinator/cli.rb', line 369

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

#listObject



382
383
384
385
386
# File 'lib/tmuxinator/cli.rb', line 382

def list
  say "tmuxinator projects:"

  print_in_columns Tmuxinator::Config.configs
end

#localObject



289
290
291
292
293
294
295
# File 'lib/tmuxinator/cli.rb', line 289

def local
  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  render_project(create_project(attach: options[:attach]))
end

#new(name, session = nil) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/tmuxinator/cli.rb', line 84

def new(name, session = nil)
  if session
    new_project_with_session(name, session)
  else
    new_project(name)
  end
end

#start(name = nil, *args) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/tmuxinator/cli.rb', line 243

def start(name = nil, *args)
  # project-config takes precedence over a named project in the case that
  # both are provided.
  if options["project-config"]
    args.unshift name if name
    name = nil
  end

  params = {
    args: args,
    attach: options[:attach],
    custom_name: options[:name],
    name: name,
    project_config: options["project-config"]
  }

  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  project = create_project(params)
  render_project(project)
end

#stop(name) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tmuxinator/cli.rb', line 272

def stop(name)
  params = {
    name: name
  }
  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  project = create_project(params)
  kill_project(project)
end

#versionObject



391
392
393
# File 'lib/tmuxinator/cli.rb', line 391

def version
  say "tmuxinator #{Tmuxinator::VERSION}"
end