Sha256: e0027b3fa9323d1392b879a19fdefc63303cedc086427843ad7d266965208b5d

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require 'open3'

class Thor
  module Actions
    # execute a shell command and raise an error if non-zero exit code is returned
    # return the string output from the command
    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

    # launch configured editor to retreive message/string
    # see http://osdir.com/ml/ruby-talk/2010-06/msg01424.html
    # see https://gist.github.com/rkumar/456809
    # see http://rdoc.info/github/visionmedia/commander/master/Commander/UI.ask_editor
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitx-2.21.2.ci.130.1 lib/gitx/extensions/thor.rb
gitx-2.21.2 lib/gitx/extensions/thor.rb