Class: NA::Action

Inherits:
Hash
  • Object
show all
Defined in:
lib/na/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_freeze, #deep_freeze!, #deep_merge, #deep_thaw, #deep_thaw!, #symbolize_keys

Constructor Details

#initialize(file, project, parent, action, idx, note = []) ⇒ Action

Returns a new instance of Action.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/na/action.rb', line 9

def initialize(file, project, parent, action, idx, note = [])
  super()

  @file = file
  @project = project
  @parent = parent
  @action = action.gsub(/\{/, '\\{')
  @tags = scan_tags
  @line = idx
  @note = note
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



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

def action
  @action
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/na/action.rb', line 5

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



5
6
7
# File 'lib/na/action.rb', line 5

def line
  @line
end

#noteObject

Returns the value of attribute note.



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

def note
  @note
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/na/action.rb', line 5

def parent
  @parent
end

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/na/action.rb', line 5

def project
  @project
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/na/action.rb', line 5

def tags
  @tags
end

Instance Method Details

#inspectObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/na/action.rb', line 56

def inspect
  <<~EOINSPECT
  @file: #{@file}
  @project: #{@project}
  @parent: #{@parent.join('>')}
  @action: #{@action}
  @tags: #{@tags}
  @note: #{@note}
  EOINSPECT
end

#pretty(extension: 'taskpaper', template: {}, regexes: [], notes: false, detect_width: true) ⇒ Object

Pretty print an action

Parameters:

  • extension (String) (defaults to: 'taskpaper')

    The file extension

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

    The template to use for colorization

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

    The regexes to highlight (searches)

  • notes (Boolean) (defaults to: false)

    Include notes



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/na/action.rb', line 77

def pretty(extension: 'taskpaper', template: {}, regexes: [], notes: false, detect_width: true)
  theme = NA::Theme.load_theme
  template = theme.merge(template)

  # Create the hierarchical parent string
  parents = @parent.map do |par|
    NA::Color.template("{x}#{template[:parent]}#{par}")
  end.join(NA::Color.template(template[:parent_divider]))
  parents = "#{NA.theme[:bracket]}[#{NA.theme[:error]}#{parents}#{NA.theme[:bracket]}]{x} "

  # Create the project string
  project = NA::Color.template("#{template[:project]}#{@project}{x} ")

  # Create the source filename string, substituting ~ for HOME and removing extension
  file = @file.sub(%r{^\./}, '').sub(/#{ENV['HOME']}/, '~')
  file = file.sub(/\.#{extension}$/, '') unless NA.include_ext
  # colorize the basename
  file = file.highlight_filename
  file_tpl = "#{template[:file]}#{file} {x}"
  filename = NA::Color.template(file_tpl)

  # colorize the action and highlight tags
  @action.gsub!(/\{(.*?)\}/, '\\{\1\\}')
  action = NA::Color.template("#{template[:action]}#{@action.sub(/ @#{NA.na_tag}\b/, '')}{x}")
  action = action.highlight_tags(color: template[:tags],
                                 parens: template[:value_parens],
                                 value: template[:values],
                                 last_color: template[:action])

  if detect_width
    width = TTY::Screen.columns
    prefix = NA::Color.uncolor(pretty(template: { templates: { output: template[:templates][:output].sub(/%action/, '').sub(/%note/, '') } }, detect_width: false))
    indent = prefix.length

    # Add notes if needed
    note = if notes && @note.count.positive?
             NA::Color.template(@note.wrap(width, indent, template[:note]))
           elsif !notes && @note.count.positive?
             action += "#{template[:note]}*"
           else
             ''
           end

    action = action.wrap(width, indent)
  else
    note = if notes && @note.count.positive?
             NA::Color.template("\n#{@note.map { |l| "  #{template[:note]}#{l.wrap(width, indent)}{x}" }.join("\n")}")
           elsif !notes && @note.count.positive?
             action += "#{template[:note]}*"
           else
             ''
           end
  end

  # Replace variables in template string and output colorized
  NA::Color.template(template[:templates][:output].gsub(/%filename/, filename)
                      .gsub(/%project/, project)
                      .gsub(/%parents?/, parents)
                      .gsub(/%action/, action.highlight_search(regexes))
                      .gsub(/%note/, note)).gsub(/\\\{/, '{')
end

#process(priority: 0, finish: false, add_tag: [], remove_tag: [], note: []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/na/action.rb', line 21

def process(priority: 0, finish: false, add_tag: [], remove_tag: [], note: [])
  string = @action.dup

  if priority&.positive?
    string.gsub!(/(?<=\A| )@priority\(\d+\)/, '')
    string.strip!
    string += " @priority(#{priority})"
  end

  remove_tag.each do |tag|
    string.gsub!(/(?<=\A| )@#{tag.gsub(/([()*?])/, '\\\\1')}(\(.*?\))?/, '')
    string.strip!
  end

  add_tag.each do |tag|
    string.gsub!(/(?<=\A| )@#{tag.gsub(/([()*?])/, '\\\\1')}(\(.*?\))?/, '')
    string.strip!
    string += " @#{tag}"
  end

  string = "#{string.strip} @done(#{Time.now.strftime('%Y-%m-%d %H:%M')})" if finish && string !~ /(?<=\A| )@done/

  @action = string.expand_date_tags
  @note = note unless note.empty?
end

#search_match?(any: [], all: [], none: [], include_note: true) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
# File 'lib/na/action.rb', line 143

def search_match?(any: [], all: [], none: [], include_note: true)
  search_matches_any(any, include_note: include_note) &&
    search_matches_all(all, include_note: include_note) &&
    search_matches_none(none, include_note: include_note)
end

#tags_match?(any: [], all: [], none: []) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/na/action.rb', line 139

def tags_match?(any: [], all: [], none: [])
  tag_matches_any(any) && tag_matches_all(all) && tag_matches_none(none)
end

#to_sObject



47
48
49
50
51
52
53
54
# File 'lib/na/action.rb', line 47

def to_s
  note = if @note.count.positive?
           "\n#{@note.join("\n")}"
         else
           ''
         end
  "(#{@file}:#{@line}) #{@project}:#{@parent.join('>')} | #{@action}#{note}"
end