Module: Thor::Actions

Included in:
Gitx::Cli::BaseCommand
Defined in:
lib/gitx/extensions/thor.rb

Instance Method Summary collapse

Instance Method Details

#ask_editor(initial_text = '', editor: nil, footer: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitx/extensions/thor.rb', line 29

def ask_editor(initial_text = '', editor: nil, footer: nil)
  editor ||= ENV['EDITOR'] || 'vi'
  initial_text += "\n\n#{footer}" if footer
  text = Tempfile.open('text.md') do |f|
    f << initial_text
    f.flush

    flags = case editor
    when 'mate', 'emacs', 'subl'
      '-w'
    when 'mvim'
      '-f'
    else
      ''
    end
    pid = fork { exec([editor, flags, f.path].join(' ')) }
    Process.waitpid(pid)
    File.read(f.path)
  end
  text = text.gsub(footer, '') if footer
  text.chomp.strip
end

#run_cmd(*args) ⇒ Object

execute a shell command and raise an error if non-zero exit code is returned return the string output from the command



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitx/extensions/thor.rb', line 7

def run_cmd(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  cmd = args
  say "$ #{cmd.join(' ')}", :yellow
  output = ''

  Open3.popen2e(*cmd) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      say line, :yellow
      output << line
    end

    exit_status = wait_thr.value
    fail "#{cmd.join(' ')} failed" unless exit_status.success? || options[:allow_failure]
  end
  output
end