Module: NA

Extended by:
Editor
Defined in:
lib/na/colors.rb,
lib/na/todo.rb,
lib/na/pager.rb,
lib/na/theme.rb,
lib/na/action.rb,
lib/na/editor.rb,
lib/na/prompt.rb,
lib/na/actions.rb,
lib/na/project.rb,
lib/na/next_action.rb

Overview

Next Action methods

Defined Under Namespace

Modules: Color, Editor, Pager, Prompt, Theme Classes: Action, Actions, Project, Todo

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Editor

args_for_editor, default_editor, editor_with_args, fork_editor, format_input

Class Attribute Details

.commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/na/next_action.rb', line 8

def command
  @command
end

.command_lineObject

Returns the value of attribute command_line.



8
9
10
# File 'lib/na/next_action.rb', line 8

def command_line
  @command_line
end

.cwdObject

Returns the value of attribute cwd.



8
9
10
# File 'lib/na/next_action.rb', line 8

def cwd
  @cwd
end

.cwd_isObject

Returns the value of attribute cwd_is.



8
9
10
# File 'lib/na/next_action.rb', line 8

def cwd_is
  @cwd_is
end

.extensionObject

Returns the value of attribute extension.



8
9
10
# File 'lib/na/next_action.rb', line 8

def extension
  @extension
end

.global_fileObject

Returns the value of attribute global_file.



8
9
10
# File 'lib/na/next_action.rb', line 8

def global_file
  @global_file
end

.globalsObject

Returns the value of attribute globals.



8
9
10
# File 'lib/na/next_action.rb', line 8

def globals
  @globals
end

.include_extObject

Returns the value of attribute include_ext.



8
9
10
# File 'lib/na/next_action.rb', line 8

def include_ext
  @include_ext
end

.na_tagObject

Returns the value of attribute na_tag.



8
9
10
# File 'lib/na/next_action.rb', line 8

def na_tag
  @na_tag
end

.stdinObject

Returns the value of attribute stdin.



8
9
10
# File 'lib/na/next_action.rb', line 8

def stdin
  @stdin
end

.verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/na/next_action.rb', line 8

def verbose
  @verbose
end

Class Method Details

.add_action(file, project, action, note = [], priority: 0, finish: false, append: false) ⇒ Object

Add an action to a todo file

Parameters:

  • file (String)

    The target file

  • project (String)

    The project name

  • action (String)

    The action

  • note (String) (defaults to: [])

    The note



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/na/next_action.rb', line 419

def add_action(file, project, action, note = [], priority: 0, finish: false, append: false)
  parent = project.split(%r{[:/]})

  if NA.global_file
    if NA.cwd_is == :tag
      add_tag = [NA.cwd]
    else
      project = NA.cwd
    end
  end

  action = Action.new(file, project, parent, action, nil, note)

  update_action(file, nil, add: action, project: project, add_tag: add_tag, priority: priority, finish: finish, append: append)
end

.backup_file(target) ⇒ Object

Create a backup file

Parameters:

  • target (String)

    The file to back up



906
907
908
909
910
# File 'lib/na/next_action.rb', line 906

def backup_file(target)
  FileUtils.cp(target, backup_path(target))
  save_modified_file(target)
  NA.notify("#{NA.theme[:warning]}Backup file created for #{target.highlight_filename}", debug: true)
end

.backup_filesArray

Get list of backed up files

Returns:

  • (Array)

    list of file paths



675
676
677
678
679
680
681
682
683
684
# File 'lib/na/next_action.rb', line 675

def backup_files
  db = database_path(file: 'last_modified.txt')
  if File.exist?(db)
    IO.read(db).strip.split(/\n/).map(&:strip)
  else
    NA.notify("#{NA.theme[:error]}Backup database not found")
    File.open(db, 'w') { |f| f.puts }
    []
  end
end

.backup_path(file) ⇒ Object

Get the backup file path for a file

Parameters:

  • file

    The file



704
705
706
707
708
709
710
711
712
713
# File 'lib/na/next_action.rb', line 704

def backup_path(file)
  backup_home = File.expand_path('~/.local/share/na/backup')
  backup = old_backup_path(file)
  backup_dir = File.join(backup_home, File.dirname(backup))
  FileUtils.mkdir_p(backup_dir) unless File.directory?(backup_dir)

  backup_target = File.join(backup_home, backup)
  FileUtils.mv(backup, backup_target) if File.exist?(backup)
  backup_target
end

.choose_from(options, prompt: 'Make a selection: ', multiple: false, sorted: true, fzf_args: []) ⇒ String, Array

Generate a menu of options and allow user selection

Parameters:

  • options (Array)

    The options from which to choose

  • prompt (String) (defaults to: 'Make a selection: ')

    The prompt

  • multiple (Boolean) (defaults to: false)

    If true, allow multiple selections

  • sorted (Boolean) (defaults to: true)

    If true, sort selections alphanumerically

  • fzf_args (Array) (defaults to: [])

    Additional fzf arguments

Returns:



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/na/next_action.rb', line 942

def choose_from(options, prompt: 'Make a selection: ', multiple: false, sorted: true, fzf_args: [])
  return nil unless $stdout.isatty

  options.sort! if sorted

  res = if TTY::Which.exist?('fzf')
          default_args = [%(--prompt="#{prompt}"), "--height=#{options.count + 2}", '--info=inline']
          default_args << '--multi' if multiple
          default_args << '--bind ctrl-a:select-all' if multiple
          header = "esc: cancel,#{multiple ? ' tab: multi-select, ctrl-a: select all,' : ''} return: confirm"
          default_args << %(--header="#{header}")
          default_args.concat(fzf_args)
          options = NA::Color.uncolor(NA::Color.template(options.join("\n")))
          `echo #{Shellwords.escape(options)}|#{TTY::Which.which('fzf')} #{default_args.join(' ')}`.strip
        elsif TTY::Which.exist?('gum')
          args = [
            '--cursor.foreground="151"',
            '--item.foreground=""'
          ]
          args.push '--no-limit' if multiple
          puts NA::Color.template("#{NA.theme[:prompt]}#{prompt}{x}")
          options = NA::Color.uncolor(NA::Color.template(options.join("\n")))
          `echo #{Shellwords.escape(options)}|#{TTY::Which.which('gum')} choose #{args.join(' ')}`.strip
        else
          reader = TTY::Reader.new
          puts
          options.each.with_index do |f, i|
            puts NA::Color.template(format("#{NA.theme[:prompt]}%<idx> 2d{xw}) #{NA.theme[:filename]}%<action>s{x}\n", idx: i + 1, action: f))
          end
          result = reader.read_line(NA::Color.template("#{NA.theme[:prompt]}#{prompt}{x}")).strip
          if multiple
            mult_res = []
            result = result.gsub(/,/, ' ').gsub(/ +/, ' ').split(/ /)
            result.each do |r|
              mult_res << options[r.to_i - 1] if r.to_i&.positive?
            end
            mult_res.join("\n")
          else
            result.to_i&.positive? ? options[result.to_i - 1] : nil
          end
        end

  return false if res&.strip&.size&.zero?
  # pp NA::Color.uncolor(NA::Color.template(res))
  multiple ? NA::Color.uncolor(NA::Color.template(res)).split(/\n/) : NA::Color.uncolor(NA::Color.template(res))
end

.color_single_options(choices = %w[y n])) ⇒ String

Helper function to colorize the Y/N prompt

Parameters:

  • choices (Array) (defaults to: %w[y n]))

    The choices with default capitalized

Returns:

  • (String)

    colorized string



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/na/next_action.rb', line 74

def color_single_options(choices = %w[y n])
  out = []
  choices.each do |choice|
    case choice
    when /[A-Z]/
      out.push(NA::Color.template("{bw}#{choice}{x}"))
    else
      out.push(NA::Color.template("{dw}#{choice}{xg}"))
    end
  end
  NA::Color.template("{xg}[#{out.join('/')}{xg}]{x}")
end

.create_todo(target, basename, template: nil) ⇒ Object

Create a new todo file

Parameters:

  • target (String)

    The target path

  • basename (String)

    The project base name



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/na/next_action.rb', line 93

def create_todo(target, basename, template: nil)
  File.open(target, 'w') do |f|
    if template && File.exist?(template)
      content = IO.read(template)
    else
      content = "        Inbox:\n        \#{basename}:\n        \\tFeature Requests:\n        \\tIdeas:\n        \\tBugs:\n        Archive:\n        Search Definitions:\n        \\tTop Priority @search(@priority = 5 and not @done)\n        \\tHigh Priority @search(@priority > 3 and not @done)\n        \\tMaybe @search(@maybe)\n        \\tNext @search(@\#{NA.na_tag} and not @done and not project = \\\"Archive\\\")\n      ENDCONTENT\n    end\n    f.puts(content)\n  end\n  save_working_dir(target)\n  notify(\"\#{NA.theme[:warning]}Created \#{NA.theme[:file]}\#{target}\")\nend\n"

.database_path(file: 'tdlist.txt') ⇒ String

Get path to database of known todo files

Returns:



747
748
749
750
751
752
# File 'lib/na/next_action.rb', line 747

def database_path(file: 'tdlist.txt')
  db_dir = File.expand_path('~/.local/share/na')
  # Create directory if needed
  FileUtils.mkdir_p(db_dir) unless File.directory?(db_dir)
  File.join(db_dir, file)
end

.delete_search(strings = nil) ⇒ Object



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/na/next_action.rb', line 863

def delete_search(strings = nil)
  NA.notify("#{NA.theme[:error]}Name of search required", exit_code: 1) if strings.nil? || strings.empty?

  file = database_path(file: 'saved_searches.yml')
  NA.notify("#{NA.theme[:error]}No search definitions file found", exit_code: 1) unless File.exist?(file)

  strings = [strings] unless strings.is_a? Array

  searches = YAML.safe_load(file.read_file)
  keys = searches.keys.delete_if { |k| k !~ /(#{strings.map(&:wildcard_to_rx).join('|')})/ }

  NA.notify("#{NA.theme[:error]}No search named #{strings.join(', ')} found", exit_code: 1) if keys.empty?

  res = yn(NA::Color.template(%(#{NA.theme[:warning]}Remove #{keys.count > 1 ? 'searches' : 'search'} #{NA.theme[:filename]}"#{keys.join(', ')}"{x})),
           default: false)

  NA.notify("#{NA.theme[:error]}Cancelled", exit_code: 1) unless res

  searches.delete_if { |k| keys.include?(k) }

  File.open(file, 'w') { |f| f.puts(YAML.dump(searches)) }

  NA.notify("#{NA.theme[:warning]}Deleted {bw}#{keys.count}{x}#{NA.theme[:warning]} #{keys.count > 1 ? 'searches' : 'search'}", exit_code: 0)
end

.edit_file(file: nil, app: nil) ⇒ Object



492
493
494
# File 'lib/na/next_action.rb', line 492

def edit_file(file: nil, app: nil)
  os_open(file, app: app) if file && File.exist?(file)
end

.edit_searchesObject



888
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/na/next_action.rb', line 888

def edit_searches
  file = database_path(file: 'saved_searches.yml')
  searches = load_searches

  NA.notify("#{NA.theme[:error]}No search definitions found", exit_code: 1) unless searches.count.positive?

  editor = NA.default_editor
  NA.notify("#{NA.theme[:error]}No $EDITOR defined", exit_code: 1) unless editor && TTY::Which.exist?(editor)

  system %(#{editor} "#{file}")
  NA.notify("#{NA.theme[:success]}Opened #{file} in #{editor}", exit_code: 0)
end

.find_actions(target, search, tagged = nil, all: false, done: false, project: nil, search_note: true) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/na/next_action.rb', line 150

def find_actions(target, search, tagged = nil, all: false, done: false, project: nil, search_note: true)
  todo = NA::Todo.new({ search: search,
                        search_note: search_note,
                        require_na: false,
                        file_path: target,
                        project: project,
                        tag: tagged,
                        done: done })

  unless todo.actions.count.positive?
    NA.notify("#{NA.theme[:error]}No matching actions found in #{File.basename(target, ".#{NA.extension}").highlight_filename}")
    return
  end

  return [todo.projects, todo.actions] if todo.actions.count == 1 || all

  options = todo.actions.map { |action| "#{action.line} % #{action.parent.join('/')} : #{action.action}" }
  res = choose_from(options, prompt: 'Make a selection: ', multiple: true, sorted: true)

  NA.notify("#{NA.theme[:error]}Cancelled", exit_code: 1) unless res && res.length.positive?

  selected = NA::Actions.new
  res.each do |result|
    idx = result.match(/^(\d+)(?= % )/)[1]
    action = todo.actions.select { |a| a.line == idx.to_i }.first
    selected.push(action)
  end
  [todo.projects, selected]
end

.find_exact_dir(dirs, search) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
# File 'lib/na/next_action.rb', line 601

def find_exact_dir(dirs, search)
  terms = search.filter { |s| !s[:negate] }.map { |t| t[:token] }.join(' ')
  out = dirs
  dirs.each do |dir|
    if File.basename(dir).sub(/\.#{NA.extension}$/, '') =~ /^#{terms}$/
      out = [dir]
      break
    end
  end
  out
end

.find_files(depth: 1) ⇒ Object

Use the *nix find command to locate files matching NA.extension

Parameters:

  • depth (Number) (defaults to: 1)

    The depth at which to search



501
502
503
504
505
506
507
# File 'lib/na/next_action.rb', line 501

def find_files(depth: 1)
  return [NA.global_file] if NA.global_file

  files = `find . -maxdepth #{depth} -name "*.#{NA.extension}"`.strip.split("\n")
  files.each { |f| save_working_dir(File.expand_path(f)) }
  files
end

.find_files_matching(options = {}) ⇒ Object



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/na/next_action.rb', line 509

def find_files_matching(options = {})
  defaults = {
    depth: 1,
    done: false,
    file_path: nil,
    negate: false,
    project: nil,
    query: nil,
    regex: false,
    require_na: true,
    search: nil,
    tag: nil
  }
  options = defaults.merge(options)
  files = find_files(depth: options[:depth])

  files.delete_if do |file|
    cmd_options = {
      depth: options[:depth],
      done: options[:done],
      file_path: file,
      negate: options[:negate],
      project: options[:project],
      query: options[:query],
      regex: options[:regex],
      require_na: options[:require_na],
      search: options[:search],
      tag: options[:tag]
    }
    todo = NA::Todo.new(cmd_options)
    todo.actions.empty?
  end

  files
end

.find_projects(target) ⇒ Object



145
146
147
148
# File 'lib/na/next_action.rb', line 145

def find_projects(target)
  todo = NA::Todo.new(require_na: false, file_path: target)
  todo.projects
end

.insert_project(target, project, projects) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/na/next_action.rb', line 180

def insert_project(target, project, projects)
  path = project.split(%r{[:/]})
  todo = NA::Todo.new(file_path: target)
  built = []
  last_match = nil
  final_match = nil
  new_path = []
  matches = nil
  path.each_with_index do |part, i|
    built.push(part)
    matches = todo.projects.select { |proj| proj.project =~ /^#{built.join(':')}/i }
    if matches.count.zero?
      final_match = last_match
      new_path = path.slice(i, path.count - i)
      break
    else
      last_match = matches.last
    end
  end

  content = target.read_file
  if final_match.nil?
    indent = 0
    input = []

    new_path.each do |part|
      input.push("#{"\t" * indent}#{part.cap_first}:")
      indent += 1
    end

    if new_path.join('') =~ /Archive/i
      line = todo.projects.last&.last_line || 0
      content = content.split(/\n/).insert(line, input.join("\n")).join("\n")
    else
      split = content.split(/\n/)
      line = todo.projects.first&.line || 0
      before = split.slice(0, line).join("\n")
      after = split.slice(line, split.count - 0).join("\n")
      content = "#{before}\n#{input.join("\n")}\n#{after}"
    end

    new_project = NA::Project.new(path.map(&:cap_first).join(':'), indent - 1, line, line)
  else
    line = final_match.last_line + 1
    indent = final_match.indent + 1
    input = []
    new_path.each do |part|
      input.push("#{"\t" * indent}#{part.cap_first}:")
      indent += 1
    end
    content = content.split(/\n/).insert(line, input.join("\n")).join("\n")
    new_project = NA::Project.new(path.map(&:cap_first).join(':'), indent - 1, line + input.count - 1, line + input.count - 1)
  end

  File.open(target, 'w') do |f|
    f.puts content
  end

  new_project
end

.last_modified_file(search: nil) ⇒ Object

Get the last modified file from the database

Parameters:

  • search (defaults to: nil)

    The search



650
651
652
653
654
# File 'lib/na/next_action.rb', line 650

def last_modified_file(search: nil)
  files = backup_files
  files.delete_if { |f| f !~ Regexp.new(search.dir_to_rx(require_last: true)) } if search
  files.last
end

.list_projects(query: [], file_path: nil, depth: 1, paths: true) ⇒ Object



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/na/next_action.rb', line 785

def list_projects(query: [], file_path: nil, depth: 1, paths: true)
  files = if NA.global_file
            [NA.global_file]
          elsif !file_path.nil?
            [file_path]
          elsif query.nil?
            NA.find_files(depth: depth)
          else
            match_working_dir(query)
          end

  target = files.count > 1 ? NA.select_file(files) : files[0]
  return if target.nil?

  projects = find_projects(target)
  projects.each do |proj|
    parts = proj.project.split(/:/)
    output = if paths
               "{bg}#{parts.join('{bw}/{bg}')}{x}"
             else
               parts.fill('{bw}—{bg}', 0..-2)
               "{bg}#{parts.join(' ')}{x}"
             end

    puts NA::Color.template(output)
  end
end

.list_todos(query: []) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/na/next_action.rb', line 813

def list_todos(query: [])
  dirs = if query
           match_working_dir(query, distance: 2, require_last: false)
         else
           file = database_path
           content = File.exist?(file) ? file.read_file.strip : ''
           notify("#{NA.theme[:error]}Database empty", exit_code: 1) if content.empty?

           content.split(/\n/)
         end

  dirs.map! do |dir|
    dir.highlight_filename
  end

  puts NA::Color.template(dirs.join("\n"))
end

.load_searchesObject



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File 'lib/na/next_action.rb', line 847

def load_searches
  file = database_path(file: 'saved_searches.yml')
  if File.exist?(file)
    searches = YAML.safe_load(file.read_file)
  else
    searches = {
      'soon' => 'tagged "due<in 2 days,due>yesterday"',
      'overdue' => 'tagged "due<now"',
      'high' => 'tagged "prio>3"',
      'maybe' => 'tagged "maybe"'
    }
    File.open(file, 'w') { |f| f.puts(YAML.dump(searches)) }
  end
  searches
end

.match_working_dir(search, distance: 1, require_last: true) ⇒ Array

Find a matching path using semi-fuzzy matching. Search tokens can include ! and + to negate or make required.

Parameters:

  • search (Array)

    search tokens to match

  • distance (Integer) (defaults to: 1)

    allowed distance between characters

  • require_last (Boolean) (defaults to: true)

    require regex to match last element of path

Returns:

  • (Array)

    array of matching directories/todo files



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/na/next_action.rb', line 559

def match_working_dir(search, distance: 1, require_last: true)
  file = database_path
  NA.notify("#{NA.theme[:error]}No na database found", exit_code: 1) unless File.exist?(file)

  dirs = file.read_file.split("\n")

  optional = search.filter { |s| !s[:negate] }.map { |t| t[:token] }
  required = search.filter { |s| s[:required] && !s[:negate] }.map { |t| t[:token] }
  negated = search.filter { |s| s[:negate] }.map { |t| t[:token] }

  optional.push('*') if optional.count.zero? && required.count.zero? && negated.count.positive?
  if optional == negated
    required = ['*']
    optional = ['*']
  end

  NA.notify("Optional directory regex: {x}#{optional.map { |t| t.dir_to_rx(distance: distance) }}", debug: true)
  NA.notify("Required directory regex: {x}#{required.map { |t| t.dir_to_rx(distance: distance) }}", debug: true)
  NA.notify("Negated directory regex: {x}#{negated.map { |t| t.dir_to_rx(distance: distance, require_last: false) }}", debug: true)

  if require_last
    dirs.delete_if { |d| !d.sub(/\.#{NA.extension}$/, '').dir_matches(any: optional, all: required, none: negated) }
  else
    dirs.delete_if do |d|
      !d.sub(/\.#{NA.extension}$/, '')
        .dir_matches(any: optional, all: required, none: negated, distance: 2, require_last: false)
    end
  end

  dirs = dirs.sort_by { |d| File.basename(d) }.uniq

  dirs = find_exact_dir(dirs, search) unless optional == ['*']

  if dirs.empty? && require_last
    NA.notify("#{NA.theme[:warning]}No matches, loosening search", debug: true)
    match_working_dir(search, distance: 2, require_last: false)
  else
    NA.notify("Matched files: {x}#{dirs.join(', ')}", debug: true)
    dirs
  end
end

.move_deprecated_backupsObject



686
687
688
689
690
691
692
693
# File 'lib/na/next_action.rb', line 686

def move_deprecated_backups
  backup_files.each do |file|
    if File.exist?(old_backup_path(file))
      NA.notify("Moving deprecated backup to new backup folder (#{file})", debug: true)
      backup_path(file)
    end
  end
end

.notify(msg, exit_code: false, debug: false) ⇒ Object

Output to STDERR

Parameters:

  • msg (String)

    The message

  • exit_code (Number) (defaults to: false)

    The exit code, no exit if false

  • debug (Boolean) (defaults to: false)

    only display message if running :verbose



22
23
24
25
26
27
28
29
30
31
# File 'lib/na/next_action.rb', line 22

def notify(msg, exit_code: false, debug: false)
  return if debug && !NA.verbose

  if debug
    $stderr.puts NA::Color.template("{x}#{NA.theme[:debug]}#{msg}{x}")
  else
    $stderr.puts NA::Color.template("{x}#{msg}{x}")
  end
  Process.exit exit_code if exit_code
end

.old_backup_path(file) ⇒ Object



695
696
697
# File 'lib/na/next_action.rb', line 695

def old_backup_path(file)
  File.join(File.dirname(file), ".#{File.basename(file)}.bak")
end

.os_open(file, app: nil) ⇒ Object

Platform-agnostic open command

Parameters:

  • file (String)

    The file to open



759
760
761
762
763
764
765
766
767
768
769
# File 'lib/na/next_action.rb', line 759

def os_open(file, app: nil)
  os = RbConfig::CONFIG['target_os']
  case os
  when /darwin.*/i
    darwin_open(file, app: app)
  when /mingw|mswin/i
    win_open(file)
  else
    linux_open(file)
  end
end

.output_children(children, level = 1) ⇒ Object

Output an Omnifocus-friendly action list

Parameters:

  • children

    The children

  • level (defaults to: 1)

    The indent level



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/na/next_action.rb', line 457

def output_children(children, level = 1)
  out = []
  indent = "\t" * level
  children.each do |k, v|
    if k.to_s =~ /actions/
      indent += "\t"

      v.each do |a|
        item = "#{indent}- #{a.action}"

        unless a.tags.empty?
          tags = []
          a.tags.each do |key, val|
            next if key =~ /^(due|flagged|done)$/

            tag = key
            tag += "-#{val}" unless val.nil? || val.empty?
            tags.push(tag)
          end

          item += " @tags(#{tags.join(',')})" unless tags.empty?
        end

        item += "\n#{indent}\t#{a.note.join("\n#{indent}\t")}" unless a.note.empty?

        out.push(item)
      end
    else
      out.push("#{indent}#{k}:")
      out.concat(output_children(v, level + 1))
    end
  end
  out
end

.priority_mapObject



33
34
35
36
37
38
39
# File 'lib/na/next_action.rb', line 33

def priority_map
  {
    'h' => 5,
    'm' => 3,
    'l' => 1
  }
end

.project_hierarchy(actions) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/na/next_action.rb', line 435

def project_hierarchy(actions)
  parents = { actions: [] }
  actions.each do |a|
    parent = a.parent
    current_parent = parents
    parent.each do |par|
      if !current_parent.key?(par)
        current_parent[par] = { actions: [] }
      end
      current_parent = current_parent[par]
    end

    current_parent[:actions].push(a)
  end
  parents
end

.request_input(options, prompt: 'Enter text') ⇒ Object

Request terminal input from user, readline style

Parameters:

  • options (Hash)

    The options

  • prompt (String) (defaults to: 'Enter text')

    The prompt



918
919
920
921
922
923
924
925
926
927
928
# File 'lib/na/next_action.rb', line 918

def request_input(options, prompt: 'Enter text')
  if $stdin.isatty && TTY::Which.exist?('gum') && (options[:tagged].nil? || options[:tagged].empty?)
    opts = [%(--placeholder "#{prompt}"),
            '--char-limit=500',
            "--width=#{TTY::Screen.columns}"]
    `gum input #{opts.join(' ')}`.strip
  elsif $stdin.isatty && options[:tagged].empty?
    NA.notify("#{NA.theme[:prompt]}#{prompt}:")
    reader.read_line(NA::Color.template("#{NA.theme[:filename]}> #{NA.theme[:action]}")).strip
  end
end

.restore_last_modified_file(search: nil) ⇒ Object

Get last modified file and restore a backup

Parameters:

  • search (defaults to: nil)

    The search



661
662
663
664
665
666
667
668
# File 'lib/na/next_action.rb', line 661

def restore_last_modified_file(search: nil)
  file = last_modified_file(search: search)
  if file
    restore_modified_file(file)
  else
    NA.notify("#{NA.theme[:error]}No matching file found")
  end
end

.restore_modified_file(file) ⇒ Object

Restore a file from backup

Parameters:

  • file

    The file



730
731
732
733
734
735
736
737
738
739
740
# File 'lib/na/next_action.rb', line 730

def restore_modified_file(file)
  bak_file = backup_path(file)
  if File.exist?(bak_file)
    FileUtils.mv(bak_file, file)
    NA.notify("#{NA.theme[:success]}Backup restored for #{file.highlight_filename}")
  else
    NA.notify("#{NA.theme[:error]}Backup file for #{file.highlight_filename} not found")
  end

  weed_modified_files(file)
end

.save_modified_file(file) ⇒ Object

Save a backed-up file to the database

Parameters:

  • file

    The file



632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/na/next_action.rb', line 632

def save_modified_file(file)
  db = database_path(file: 'last_modified.txt')
  file = File.expand_path(file)
  if File.exist? db
    files = IO.read(db).split(/\n/).map(&:strip)
    files.delete(file)
    files << file
    File.open(db, 'w') { |f| f.puts(files.join("\n")) }
  else
    File.open(db, 'w') { |f| f.puts(file) }
  end
end

.save_search(title, search) ⇒ Object



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/na/next_action.rb', line 831

def save_search(title, search)
  file = database_path(file: 'saved_searches.yml')
  searches = load_searches
  title = title.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_')

  if searches.key?(title)
    res = yn('Overwrite existing definition?', default: true)
    notify("#{NA.theme[:error]}Cancelled", exit_code: 0) unless res

  end

  searches[title] = search
  File.open(file, 'w') { |f| f.puts(YAML.dump(searches)) }
  NA.notify("#{NA.theme[:success]}Search #{NA.theme[:filename]}#{title}#{NA.theme[:success]} saved", exit_code: 0)
end

.save_working_dir(todo_file) ⇒ Object

Save a todo file path to the database

Parameters:

  • todo_file

    The todo file path



618
619
620
621
622
623
624
625
# File 'lib/na/next_action.rb', line 618

def save_working_dir(todo_file)
  file = database_path
  content = File.exist?(file) ? file.read_file : ''
  dirs = content.split(/\n/)
  dirs.push(File.expand_path(todo_file))
  dirs.sort!.uniq!
  File.open(file, 'w') { |f| f.puts dirs.join("\n") }
end

.select_file(files, multiple: false) ⇒ String, Array

Note:

If gum or fzf are available, they’ll be used (in that order)

Select from multiple files

Parameters:

  • files (Array)

    The files

  • multiple (Boolean) (defaults to: false)

    allow multiple selections

Returns:



128
129
130
131
132
133
134
# File 'lib/na/next_action.rb', line 128

def select_file(files, multiple: false)
  res = choose_from(files, prompt: multiple ? 'Select files' : 'Select a file', multiple: multiple)

  notify("#{NA.theme[:error]}No file selected, cancelled", exit_code: 1) unless res && res.length.positive?

  res
end

.shift_index_after(projects, idx, length = 1) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/na/next_action.rb', line 136

def shift_index_after(projects, idx, length = 1)
  projects.map do |proj|
    proj.line = proj.line - length if proj.line > idx
    proj.last_line = proj.last_line - length if proj.last_line > idx

    proj
  end
end

.themeObject



10
11
12
# File 'lib/na/next_action.rb', line 10

def theme
  @theme ||= NA::Theme.load_theme
end

.update_action(target, search, search_note: true, add: nil, add_tag: [], all: false, append: false, delete: false, done: false, edit: false, finish: false, note: [], overwrite: false, priority: 0, project: nil, move: nil, remove_tag: [], replace: nil, tagged: nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/na/next_action.rb', line 241

def update_action(target,
                  search,
                  search_note: true,
                  add: nil,
                  add_tag: [],
                  all: false,
                  append: false,
                  delete: false,
                  done: false,
                  edit: false,
                  finish: false,
                  note: [],
                  overwrite: false,
                  priority: 0,
                  project: nil,
                  move: nil,
                  remove_tag: [],
                  replace: nil,
                  tagged: nil)

  projects = find_projects(target)

  target_proj = nil

  if move
    move = move.sub(/:$/, '')
    target_proj = projects.select { |pr| pr.project =~ /#{move.gsub(/:/, '.*?:.*?')}/i }.first
    if target_proj.nil?
      res = NA.yn(NA::Color.template("#{NA.theme[:warning]}Project #{NA.theme[:file]}#{move}#{NA.theme[:warning]} doesn't exist, add it"), default: true)
      if res
        target_proj = insert_project(target, move, projects)
        projects << target_proj
      else
        NA.notify("#{NA.theme[:error]}Cancelled", exit_code: 1)
      end
    end
  end

  contents = target.read_file.split(/\n/)

  if add.is_a?(Action)
    add_tag ||= []
    add.process(priority: priority, finish: finish, add_tag: add_tag, remove_tag: remove_tag)

    projects = find_projects(target)

    target_proj = if target_proj
                    projects.select { |proj| proj.project =~ /^#{target_proj.project}$/i }.first
                  else
                    projects.select { |proj| proj.project =~ /^#{add.parent.join(':')}$/i }.first
                  end

    if target_proj.nil?
      res = NA.yn(NA::Color.template("#{NA.theme[:warning]}Project #{NA.theme[:file]}#{add.project}#{NA.theme[:warning]} doesn't exist, add it"), default: true)

      if res
        target_proj = insert_project(target, project, projects)
        projects << target_proj
      else
        NA.notify("#{NA.theme[:error]}Cancelled", exit_code: 1)
      end

      NA.notify("#{NA.theme[:error]}Error parsing project #{NA.theme[:filename]}#{target}", exit_code: 1) if target_proj.nil?

      projects = find_projects(target)
      contents = target.read_file.split("\n")
    end

    indent = "\t" * target_proj.indent
    note = note.split("\n") unless note.is_a?(Array)
    note = if note.empty?
             add.note
           else
             overwrite ? note : add.note.concat(note)
           end

    note = note.empty? ? '' : "\n#{indent}\t\t#{note.join("\n#{indent}\t\t").strip}"

    if append
      this_idx = 0
      projects.each_with_index do |proj, idx|
        if proj.line == target_proj.line
          this_idx = idx
          break
        end
      end
      target_line = if this_idx == projects.length - 1
                      contents.count
                    else
                      projects[this_idx].last_line + 1
                    end
    else
      target_line = target_proj.line + 1
    end

    contents.insert(target_line, "#{indent}\t- #{add.action}#{note}")

    notify(add.pretty)
  else
    _, actions = find_actions(target, search, tagged, done: done, all: all, project: project, search_note: search_note)

    return if actions.nil?

    actions.sort_by(&:line).reverse.each do |action|
      contents.slice!(action.line, action.note.count + 1)
      next if delete

      projects = shift_index_after(projects, action.line, action.note.count + 1)

      if edit
        editor_content = "#{action.action}\n#{action.note.join("\n")}"
        new_action, new_note = Editor.format_input(Editor.fork_editor(editor_content))
        action.action = new_action
        action.note = new_note
      end

      # If replace is defined, use search to search and replace text in action
      action.action.sub!(Regexp.new(Regexp.escape(search), Regexp::IGNORECASE), replace) if replace

      action.process(priority: priority, finish: finish, add_tag: add_tag, remove_tag: remove_tag)

      target_proj = if target_proj
                      projects.select { |proj| proj.project =~ /^#{target_proj.project}$/ }.first
                    else
                      projects.select { |proj| proj.project =~ /^#{action.parent.join(':')}$/ }.first
                    end

      indent = "\t" * target_proj.indent
      note = note.split("\n") unless note.is_a?(Array)
      note = if note.empty?
               action.note
             else
               overwrite ? note : action.note.concat(note)
             end
      note = note.empty? ? '' : "\n#{indent}\t\t#{note.join("\n#{indent}\t\t").strip}"

      if append
        this_idx = 0
        projects.each_with_index do |proj, idx|
          if proj.line == target_proj.line
            this_idx = idx
            break
          end
        end

        target_line = if this_idx == projects.length - 1
                        contents.count
                      else
                        projects[this_idx].last_line + 1
                      end
      else
        target_line = target_proj.line + 1
      end

      contents.insert(target_line, "#{indent}\t- #{action.action}#{note}")

      notify(action.pretty)
    end
  end

  backup_file(target)
  File.open(target, 'w') { |f| f.puts contents.join("\n") }

  if add
    notify("#{NA.theme[:success]}Task added to #{NA.theme[:filename]}#{target}")
  else
    notify("#{NA.theme[:success]}Task updated in #{NA.theme[:filename]}#{target}")
  end
end

.weed_cache_fileObject

Remove entries from cache database that no longer exist



774
775
776
777
778
779
780
781
782
783
# File 'lib/na/next_action.rb', line 774

def weed_cache_file
  db_dir = File.expand_path('~/.local/share/na')
  db_file = 'tdlist.txt'
  file = File.join(db_dir, db_file)
  return unless File.exist?(file)

  dirs = file.read_file.split("\n")
  dirs.delete_if { |f| !File.exist?(f) }
  File.open(file, 'w') { |f| f.puts dirs.join("\n") }
end

.weed_modified_files(file = nil) ⇒ Object



715
716
717
718
719
720
721
722
723
# File 'lib/na/next_action.rb', line 715

def weed_modified_files(file = nil)
  files = backup_files

  files.delete_if { |f| f =~ /#{file}/ } if file

  files.delete_if { |f| !File.exist?(backup_path(f)) }

  File.open(database_path(file: 'last_modified.txt'), 'w') { |f| f.puts files.join("\n") }
end

.yn(prompt, default: true) ⇒ Boolean

Display and read a Yes/No prompt

Parameters:

  • prompt (String)

    The prompt string

  • default (Boolean) (defaults to: true)

    default value if return is pressed or prompt is skipped

Returns:

  • (Boolean)

    result



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/na/next_action.rb', line 51

def yn(prompt, default: true)
  return default unless $stdout.isatty

  tty_state = `stty -g`
  system 'stty raw -echo cbreak isig'
  yn = color_single_options(default ? %w[Y n] : %w[y N])
  $stdout.syswrite "\e[1;37m#{prompt} #{yn}\e[1;37m? \e[0m"
  res = $stdin.sysread 1
  res.chomp!
  puts
  system 'stty cooked'
  system "stty #{tty_state}"
  res.empty? ? default : res =~ /y/i
end