Class: Tmuxinator::Cli

Inherits:
Thor 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",
  open: "Alias of new",
  start: <<-DESC,
  stop: <<-DESC,
  local: "Start a tmux session using ./.tmuxinator.yml",
  debug: "Output the shell commands that are generated by tmuxinator",
  copy: <<-DESC,
    Copy an existing project to a new project and open it in
    your editor
  DESC
  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"
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#exit!, #yes_no

Class Method Details

.exit_on_failure?Boolean

By default, Thor returns exit(0) when an error occurs. Please see: github.com/tmuxinator/tmuxinator/issues/192

Returns:

  • (Boolean)


5
6
7
# File 'lib/tmuxinator/cli.rb', line 5

def self.exit_on_failure?
  true
end

Instance Method Details

#commands(shell = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tmuxinator/cli.rb', line 41

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



55
56
57
58
59
60
# File 'lib/tmuxinator/cli.rb', line 55

def completions(arg)
  if %w(start stop open copy delete).include?(arg)
    configs = Tmuxinator::Config.configs
    say configs.join("\n")
  end
end

#copy(existing, new) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tmuxinator/cli.rb', line 194

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?(existing)

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

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

#debug(name, *args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/tmuxinator/cli.rb', line 179

def debug(name, *args)
  params = {
    name: name,
    custom_name: options[:name],
    attach: options[:attach],
    args: args
  }
  project = create_project(params)
  say project.render
end

#delete(*projects) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/tmuxinator/cli.rb', line 215

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



259
260
261
262
263
264
265
266
267
268
# File 'lib/tmuxinator/cli.rb', line 259

def doctor
  say "Checking if tmux is installed ==> "
  yes_no Tmuxinator::Config.installed?

  say "Checking if $EDITOR is set ==> "
  yes_no Tmuxinator::Config.editor?

  say "Checking if $SHELL is set ==> "
  yes_no Tmuxinator::Config.shell?
end

#implodeObject



233
234
235
236
237
238
# File 'lib/tmuxinator/cli.rb', line 233

def implode
  if yes?("Are you sure you want to delete all tmuxinator configs?", :red)
    FileUtils.remove_dir(Tmuxinator::Config.root)
    say "Deleted all tmuxinator projects."
  end
end

#listObject



244
245
246
247
248
# File 'lib/tmuxinator/cli.rb', line 244

def list
  say "tmuxinator projects:"

  print_in_columns Tmuxinator::Config.configs
end

#localObject



168
169
170
# File 'lib/tmuxinator/cli.rb', line 168

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

#new(name) ⇒ Object



72
73
74
75
# File 'lib/tmuxinator/cli.rb', line 72

def new(name)
  project_file = find_project_file(name, options[:local])
  Kernel.system("$EDITOR #{project_file}") || doctor
end

#start(name, *args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/tmuxinator/cli.rb', line 143

def start(name, *args)
  params = {
    name: name,
    custom_name: options[:name],
    attach: options[:attach],
    args: args
  }
  project = create_project(params)
  render_project(project)
end

#stop(name) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/tmuxinator/cli.rb', line 157

def stop(name)
  params = {
    name: name
  }
  project = create_project(params)
  kill_project(project)
end

#versionObject



253
254
255
# File 'lib/tmuxinator/cli.rb', line 253

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