Module: NA::Editor

Included in:
NA
Defined in:
lib/na/editor.rb

Class Method Summary collapse

Class Method Details

.args_for_editor(editor) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/na/editor.rb', line 32

def args_for_editor(editor)
  return editor if editor =~ /-\S/

  args = case editor
         when /^(subl|code|mate)$/
           ['-w']
         when /^(vim|mvim)$/
           ['-f']
         else
           []
         end
  "#{editor} #{args.join(' ')}"
end

.default_editor(prefer_git_editor: true) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/na/editor.rb', line 4

def default_editor(prefer_git_editor: true)
  if prefer_git_editor
    editor ||= ENV['NA_EDITOR'] || ENV['GIT_EDITOR'] || ENV['EDITOR']
  else
    editor ||= ENV['NA_EDITOR'] || ENV['EDITOR'] || ENV['GIT_EDITOR']
  end

  return editor if editor&.good? && TTY::Which.exist?(editor)

  NA.notify("No EDITOR environment variable, testing available editors", debug: true)
  editors = %w[vim vi code subl mate mvim nano emacs]
  editors.each do |ed|
    try = TTY::Which.which(ed)
    if try
      NA.notify("Using editor #{try}", debug: true)
      return try
    end
  end

  NA.notify("#{NA.theme[:error]}No editor found", exit_code: 5)

  nil
end

.editor_with_argsObject



28
29
30
# File 'lib/na/editor.rb', line 28

def editor_with_args
  args_for_editor(default_editor)
end

.fork_editor(input = '', message: :default) ⇒ Object

Create a process for an editor and wait for the file handle to return

Parameters:

  • input (String) (defaults to: '')

    Text input for editor



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/na/editor.rb', line 51

def fork_editor(input = '', message: :default)
  # raise NonInteractive, 'Non-interactive terminal' unless $stdout.isatty || ENV['DOING_EDITOR_TEST']

  NA.notify("#{NA.theme[:error]}No EDITOR variable defined in environment", exit_code: 5) if default_editor.nil?

  tmpfile = Tempfile.new(['na_temp', '.na'])

  File.open(tmpfile.path, 'w+') do |f|
    f.puts input
    unless message.nil?
      f.puts message == :default ? '# First line is the action, lines after are added as a note' : message
    end
  end

  pid = Process.fork { system("#{editor_with_args} #{tmpfile.path}") }

  trap('INT') do
    begin
      Process.kill(9, pid)
    rescue StandardError
      Errno::ESRCH
    end
    tmpfile.unlink
    tmpfile.close!
    exit 0
  end

  Process.wait(pid)

  begin
    if $?.exitstatus == 0
      input = IO.read(tmpfile.path)
    else
      exit_now! 'Cancelled'
    end
  ensure
    tmpfile.close
    tmpfile.unlink
  end

  input.split(/\n/).delete_if(&:ignore?).join("\n")
end

.format_input(input) ⇒ Array

Takes a multi-line string and formats it as an entry

Parameters:

  • input (String)

    The string to parse

Returns:

  • (Array)
    [String]title, [Note]note


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/na/editor.rb', line 101

def format_input(input)
  NA.notify("#{NA.theme[:error]}No content in entry", exit_code: 1) if input.nil? || input.strip.empty?

  input_lines = input.split(/[\n\r]+/).delete_if(&:ignore?)
  title = input_lines[0]&.strip
  NA.notify("#{NA.theme[:error]}No content in first line", exit_code: 1) if title.nil? || title.strip.empty?

  title = title.expand_date_tags

  note = if input_lines.length > 1
           input_lines[1..-1]
         else
           []
         end

  unless note.empty?
    note.map!(&:strip)
    note.delete_if { |l| l =~ /^\s*$/ || l =~ /^#/ }
  end

  [title, note]
end