Class: NA::Actions

Inherits:
Array
  • Object
show all
Defined in:
lib/na/actions.rb

Overview

Actions controller

Instance Method Summary collapse

Methods inherited from Array

#remove_bad, #wrap

Constructor Details

#initialize(actions = []) ⇒ Actions

Returns a new instance of Actions.



6
7
8
9
# File 'lib/na/actions.rb', line 6

def initialize(actions = [])
  super
  concat(actions)
end

Instance Method Details

#output(depth, config = {}) ⇒ String

Pretty print a list of actions

Parameters:

  • depth (Integer)

    The depth of the action

  • config (Hash) (defaults to: {})

    The configuration options

Options Hash (config):

  • :files (Array)

    The files to include in the output

  • :regexes (Array)

    The regexes to match against

  • :notes (Boolean)

    Whether to include notes in the output

  • :nest (Boolean)

    Whether to nest the output

  • :nest_projects (Boolean)

    Whether to nest projects in the output

  • :no_files (Boolean)

    Whether to include files in the output

Returns:

  • (String)

    The output string



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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/actions.rb', line 26

def output(depth, config = {})
  defaults = {
    files: nil,
    regexes: [],
    notes: false,
    nest: false,
    nest_projects: false,
    no_files: false,
  }
  config = defaults.merge(config)

  return if config[:files].nil?

  if config[:nest]
    template = NA.theme[:templates][:default]
    template = NA.theme[:templates][:no_file] if config[:no_files]

    parent_files = {}
    out = []

    if config[:nest_projects]
      each do |action|
        parent_files[action.file] ||= []
        parent_files[action.file].push(action)
      end

      parent_files.each do |file, acts|
        projects = NA.project_hierarchy(acts)
        out.push("#{file.sub(%r{^./}, "").shorten_path}:")
        out.concat(NA.output_children(projects, 0))
      end
    else
      template = NA.theme[:templates][:default]
      template = NA.theme[:templates][:no_file] if config[:no_files]

      each do |action|
        parent_files[action.file] ||= []
        parent_files[action.file].push(action)
      end

      parent_files.each do |file, acts|
        out.push("#{file.sub(%r{^\./}, "")}:")
        acts.each do |a|
          out.push("\t- [#{a.parent.join("/")}] #{a.action}")
          out.push("\t\t#{a.note.join("\n\t\t")}") unless a.note.empty?
        end
      end
    end
    NA::Pager.page out.join("\n")
  else
    template = if config[:no_files]
        NA.theme[:templates][:no_file]
      elsif config[:files].count.positive?
        config[:files].count == 1 ? NA.theme[:templates][:single_file] : NA.theme[:templates][:multi_file]
      elsif NA.find_files(depth: depth).count > 1
        depth > 1 ? NA.theme[:templates][:multi_file] : NA.theme[:templates][:single_file]
      else
        NA.theme[:templates][:default]
      end
    template += "%note" if config[:notes]

    config[:files].map { |f| NA.notify(f, debug: true) } if config[:files]

    output = map { |action| action.pretty(template: { templates: { output: template } }, regexes: config[:regexes], notes: config[:notes]) }
    NA::Pager.page(output.join("\n"))
  end
end