Class: NA::Actions
Overview
Actions controller
Instance Method Summary collapse
-
#initialize(actions = []) ⇒ Actions
constructor
A new instance of Actions.
-
#output(depth, config = {}) ⇒ String
Pretty print a list of actions.
Methods inherited from Array
Constructor Details
#initialize(actions = []) ⇒ Actions
Returns a new instance of Actions.
6 7 8 |
# File 'lib/na/actions.rb', line 6 def initialize(actions = []) super end |
Instance Method Details
#output(depth, config = {}) ⇒ String
Pretty print a list of actions
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 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 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 138 139 140 141 142 143 144 145 146 147 148 149 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 179 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 |
# File 'lib/na/actions.rb', line 21 def output(depth, config = {}) NA::Benchmark.measure('Actions.output') do defaults = { files: nil, regexes: [], notes: false, nest: false, nest_projects: false, no_files: false, times: false, human: false, only_timed: false, json_times: false } config = defaults.merge(config) return if config[:files].nil? # Optionally filter to only actions with a computable duration (@started and @done) filtered_actions = if config[:only_timed] self.select do |a| t = a. tl = t.transform_keys { |k| k.to_s.downcase } (tl['started'] || tl['start']) && tl['done'] end else self end 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] filtered_actions.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] filtered_actions.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 # Optimize template selection template = if config[:no_files] NA.theme[:templates][:no_file] elsif config[:files]&.any? config[:files].one? ? NA.theme[:templates][:single_file] : NA.theme[:templates][:multi_file] elsif depth > 1 NA.theme[:templates][:multi_file] else NA.theme[:templates][:default] end template += '%note' if config[:notes] # Show './' for current directory only when listing also includes subdir files if template == NA.theme[:templates][:multi_file] has_subdir = config[:files]&.any? { |f| File.dirname(f) != '.' } || depth > 1 NA.show_cwd_indicator = !has_subdir.nil? else NA.show_cwd_indicator = false end # Skip debug output if not verbose config[:files]&.each { |f| NA.notify(f, debug: true) } if config[:files] && NA.verbose # Optimize output generation - compile all output first, then apply regexes output = String.new total_seconds = 0 totals_by_tag = Hash.new(0) timed_items = [] NA::Benchmark.measure('Generate action strings') do filtered_actions.each_with_index do |action, idx| # Generate raw output without regex processing line = action.pretty(template: { templates: { output: template } }, regexes: [], notes: config[:notes]) if config[:times] # compute duration from @started/@done = action..transform_keys { |k| k.to_s.downcase } begun = ['started'] || ['start'] finished = ['done'] if begun && finished begin start_t = Time.parse(begun) end_t = Time.parse(finished) secs = [end_t - start_t, 0].max.to_i total_seconds += secs dur_color = NA.theme[:duration] || '{y}' line << NA::Color.template(" #{dur_color}[#{format_duration(secs, human: config[:human])}]{x}") # collect for JSON output timed_items << { action: NA::Color.uncolor(action.action), started: start_t.iso8601, ended: end_t.iso8601, duration: secs } # accumulate per-tag durations (exclude time-control tags) .each_key do |k| next if k =~ /^(start|started|done)$/i totals_by_tag[k.sub(/^@/, '')] += secs end rescue StandardError # ignore parse errors end end end unless config[:only_times] output << line output << "\n" unless idx == filtered_actions.size - 1 end end end # If JSON output requested, emit JSON and return immediately if config[:json_times] require 'json' json = { timed: timed_items, tags: totals_by_tag.map { |k, v| { tag: k, duration: v } }.sort_by { |h| -h[:duration] }, total: { seconds: total_seconds, timestamp: format_duration(total_seconds, human: false), human: format_duration(total_seconds, human: true) } } puts JSON.pretty_generate(json) return end # Apply regex highlighting to the entire output at once if config[:regexes].any? NA::Benchmark.measure('Apply regex highlighting') do output = output.highlight_search(config[:regexes]) end end if config[:times] && total_seconds.positive? # Build Markdown table of per-tag totals if totals_by_tag.empty? # No tag totals, just show total line dur_color = NA.theme[:duration] || '{y}' output << "\n" output << NA::Color.template("{x}#{dur_color}Total time: [#{format_duration(total_seconds, human: config[:human])}]{x}") else rows = totals_by_tag.sort_by { |_, v| -v }.map do |tag, secs| disp = format_duration(secs, human: config[:human]) ["@#{tag}", disp] end # Pre-compute total display for width calculation total_disp = format_duration(total_seconds, human: config[:human]) # Determine column widths, including footer labels/values tag_header = 'Tag' dur_header = config[:human] ? 'Duration (human)' : 'Duration' tag_width = ([tag_header.length, 'Total'.length] + rows.map { |r| r[0].length }).max dur_width = ([dur_header.length, total_disp.length] + rows.map { |r| r[1].length }).max # Header output << "\n" output << "| #{tag_header.ljust(tag_width)} | #{dur_header.ljust(dur_width)} |\n" # Separator for header output << "| #{'-' * tag_width} | #{'-' * dur_width} |\n" # Body rows rows.each do |tag, disp| output << "| #{tag.ljust(tag_width)} | #{disp.ljust(dur_width)} |\n" end # Footer separator (kramdown footer separator with '=') and footer row output << "| #{'=' * tag_width} | #{'=' * dur_width} |\n" output << "| #{'Total'.ljust(tag_width)} | #{total_disp.ljust(dur_width)} |\n" end end NA::Benchmark.measure('Pager.page call') do NA::Pager.page(output) end end end end |