class Octokom::Editor
The ‘Octokom::Editor` class opens and parses files where you can insert a title and description for pull-requests or GitHub issues. This class is inspired by the `Pry::Editor` class (pry.github.io).
Public Class Methods
# File lib/octokom/editor.rb, line 13 def initialize(repo, title, desc) @repo = repo @title = title @desc = desc end
# File lib/octokom/editor.rb, line 6 def self.open(repo, title, desc) editor = Editor.new(repo, title, desc) editor.prepare_tempfile editor.open_editor editor.parse_user_input end
Public Instance Methods
Execute the command that is used to start the editor. This includes the waiting flag as well as the file and line number.
# File lib/octokom/editor.rb, line 39 def open_editor system("#{editor_name} #{waiting_flag} #{file_inc_line(file_name, 6)}") end
Opens the tempfile once again to parse the content and return the ‘title` and `description`.
# File lib/octokom/editor.rb, line 45 def parse_user_input input = {title: [], desc: []} parsing = false read_tempfile.each do |line| match = /^\s*#\s*(title|desc)/i.match(line) parsing = match[1].downcase.to_sym if match unless line =~ /^\s*#/ input[parsing] << line if parsing end end input.update(input) { |_, lines| lines.join("\n") }.values end
Save a tempfile that includes the input from the command line or the last commit message. TODO Remove Title and Description section and use first line; following lines. TODO use last commit message for title
# File lib/octokom/editor.rb, line 23 def prepare_tempfile File.open(file_name, 'w') do |f| f.puts '# Please enter a title and optional a description below' f.puts '# the comments. To continue, save end close the file.' f.puts "# Comment lines starting with '#' will be ignored." f.puts f.puts '# Title' f.puts @title f.puts f.puts '# Description' f.puts @desc end end
Private Instance Methods
# File lib/octokom/editor.rb, line 63 def editor_name @editor_name ||= ENV['GIT_EDITOR'] || ENV['VISUAL'] || ENV['EDITOR'] || 'vi' end
Different editors have different ways how to open a file starting at a specific line number.
# File lib/octokom/editor.rb, line 81 def file_inc_line(file_name, line) case editor_name when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/ "+#{line} #{file_name}" when /^mate/, /^geany/ "--line #{line} #{file_name}" when /^subl/ "#{file_name}:#{line}" when /^uedit32/ "#{file_name}/#{line}" when /^jedit/ "#{file_name} +line:#{line}" else file_name end end
# File lib/octokom/editor.rb, line 98 def file_name @file_name ||= "#{@repo.toplevel_path}/.git/OCTOKOM-#{Time.now.usec}" end
# File lib/octokom/editor.rb, line 102 def read_tempfile File.open(file_name).map(&:chop).reject(&:empty?) end
Make sure the editor waits for the file to be closed.
# File lib/octokom/editor.rb, line 68 def waiting_flag case editor_name when /^[gm]?vi/ '--nofork' when /^subl/, /^mate/ '--wait' when /^jedit/ '-wait' end end