Method: NA::Action#pretty

Defined in:
lib/na/action.rb

#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



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
# File 'lib/na/action.rb', line 76

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