Class: Watson::Printer

Inherits:
Object
  • Object
show all
Extended by:
Watson
Includes:
Watson
Defined in:
lib/watson/printer.rb

Overview

Printer class that handles all formatting and printing of parsed dir/file structure

Constant Summary collapse

DEBUG =

Debug printing for this class

false

Constants included from Watson

BLUE, BOLD, CYAN, GLOBAL_DEBUG_OFF, GLOBAL_DEBUG_ON, GRAY, GREEN, MAGENTA, RED, RESET, UNDERLINE, VERSION, WHITE, YELLOW

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Watson

check_less, debug_print

Constructor Details

#initialize(config) ⇒ Printer

Printer initialization method to setup necessary parameters, states, and vars



92
93
94
95
96
97
98
99
# File 'lib/watson/printer.rb', line 92

def initialize(config)

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  @config = config
  return true
end

Class Method Details

.cprint(msg = "", color = "") ⇒ Object

Custom color print for static call (only writes to STDOUT)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/watson/printer.rb', line 41

def cprint (msg = "", color = "")

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # This little check will allow us to take a Constant defined color
  # As well as a [0-256] value if specified
  if (color.is_a?(String))
    debug_print "Custom color specified for cprint\n"
    STDOUT.write(color)
  elsif (color.between?(0, 256))
    debug_print "No or Default color specified for cprint\n"
    STDOUT.write("\e[38;5;#{ color }m")
  end

  STDOUT.write(msg)
end

Standard header print for static call (uses static cprint)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watson/printer.rb', line 62

def print_header

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # Header
  cprint BOLD + "------------------------------\n" + RESET
  cprint BOLD + "watson" + RESET
  cprint " - " + RESET
  cprint BOLD + YELLOW + "inline issue manager\n" + RESET
  cprint BOLD + "------------------------------\n\n" + RESET

  return true
end

Status printer for static call (uses static cprint) Print status block in standard format



81
82
83
84
85
86
# File 'lib/watson/printer.rb', line 81

def print_status(msg, color)
  cprint RESET + BOLD
  cprint WHITE + "[ "
  cprint "#{ msg } ", color
  cprint WHITE + "] " + RESET
end

Instance Method Details

#cprint(msg = "", color = "") ⇒ Object

Custom color print for member call Allows not only for custom color printing but writing to file vs STDOUT



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/watson/printer.rb', line 144

def cprint (msg = "", color = "")

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # This little check will allow us to take a Constant defined color
  # As well as a [0-256] value if specified
  if (color.is_a?(String))
    debug_print "Custom color specified for cprint\n"
    @output.write(color)
  elsif (color.between?(0, 256))
    debug_print "No or Default color specified for cprint\n"
    @output.write("\e[38;5;#{ color }m")
  end

  @output.write(msg)
end

#print_entry(entry) ⇒ Object

Individual entry printer Uses issue hash to format printed output



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
# File 'lib/watson/printer.rb', line 220

def print_entry(entry)

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # If no issues for this file, print that and break
  # The filename print is repetative, but reduces another check later
  if !entry[:has_issues]
    if @config.show_type != 'dirty'
      debug_print "No issues for #{ entry }\n"
      print_status "o", GREEN
      cprint BOLD + UNDERLINE + GREEN + "#{ entry[:relative_path] }" + RESET + "\n"
      return true
    end
  else
    if @config.show_type != 'clean'
      debug_print "Issues found for #{ entry }\n"
      cprint "\n"
      print_status "x", RED
      cprint BOLD + UNDERLINE + RED + "#{entry[:relative_path]}" + RESET + "\n"
    else
      return true
    end
  end


  # [review] - Should the tag structure be self contained in the hash
  #      Or is it ok to reference @config to figure out the tags
  @config.tag_list.each do | _tag |
    debug_print "Checking for #{ _tag }\n"

    # [review] - Better way to ignore tags through structure (hash) data
    # Maybe have individual has_issues for each one?
    if entry[_tag].size.zero?
      debug_print "#{ _tag } has no issues, skipping\n"
      next
    end

    debug_print "#{ _tag } has issues in it, print!\n"
    print_status "#{ _tag }", BLUE
    cprint "\n"

    # Go through each issue in tag
    entry[_tag].each do | _issue |
      cprint WHITE + "  line #{ _issue[:line_number] } - " + RESET
      cprint BOLD + "#{ _issue[:title] }" + RESET


      # Check to see if it has been resolved on GitHub/Bitbucket
      debug_print "Checking if issue has been resolved\n"
      @config.github_issues[:closed].each do | _closed |
        if _closed["body"].include?(_issue[:md5])
          debug_print "Found in #{ _closed[:comment] }, not posting\n"
          cprint BOLD + " [" + RESET
          cprint GREEN + BOLD + "Resolved on GitHub" + RESET
          cprint BOLD + "]" + RESET
        end
        debug_print "Did not find in #{ _closed[:comment] }\n"
      end

      debug_print "Checking if issue has been resolved\n"
      @config.bitbucket_issues[:closed].each do  | _closed |
        if _closed["content"].include?(_issue[:md5])
          debug_print "Found in #{ _closed["content"] }, not posting\n"
          cprint BOLD + " [" + RESET
          cprint GREEN + BOLD + "Resolved on Bitbucket" + RESET
          cprint BOLD + "]\n" + RESET
        end
        debug_print "Did not find in #{ _closed["title"] }\n"
      end
      cprint "\n"

    end
    cprint "\n"
  end
end

Standard header print for class call (uses member cprint)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/watson/printer.rb', line 165

def print_header
  # Identify method entry

  debug_print "#{ self } : #{ __method__ }\n"

  # Header
  cprint BOLD + "------------------------------\n" + RESET
  cprint BOLD + "watson" + RESET
  cprint " - " + RESET
  cprint BOLD + YELLOW + "inline issue manager\n\n" + RESET
  cprint "Run in: #{ Dir.pwd }\n"
  cprint "Run @ #{ Time.now.asctime }\n"
  cprint BOLD + "------------------------------\n\n" + RESET

  return true
end

Status printer for member call (uses member cprint) Print status block in standard format



186
187
188
189
190
191
# File 'lib/watson/printer.rb', line 186

def print_status(msg, color)
  cprint RESET + BOLD
  cprint WHITE + "[ "
  cprint "#{ msg } ", color
  cprint WHITE + "] " + RESET
end

Go through all files and directories and call necessary printing methods Print all individual entries, call print_structure on each subdir



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/watson/printer.rb', line 197

def print_structure(structure)

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # First go through all the files in the current structure
  # The current "structure" should reflect a dir/subdir
  structure[:files].each do | _file |
    debug_print "Printing info for #{ _file }\n"
    print_entry(_file)
  end

  # Next go through all the subdirs and pass them to print_structure
  structure[:subdirs].each do | _subdir |
    debug_print "Entering #{ _subdir } to print further\n"
    print_structure(_subdir)
  end
end

#run(structure) ⇒ Object

Take parsed structure and print out in specified formatting



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
# File 'lib/watson/printer.rb', line 104

def run(structure)

  # Identify method entry
  debug_print "#{ self } : #{ __method__ }\n"

  # Check Config to see if we have access to less for printing
  # If so, open our temp file as the output to write to
  # Else, just print out to STDOUT
  if @config.use_less
    debug_print "Unix less avaliable, setting output to #{ @config.tmp_file }\n"
    @output = File.open(@config.tmp_file, 'w')
  else
    debug_print "Unix less is unavaliable, setting output to STDOUT\n"
    @output = STDOUT
  end

  # Print header for output
  debug_print "Printing Header\n"
  print_header

  # Print out structure that was passed to this Printer
  debug_print "Starting structure printing\n"
  print_structure(structure)

  # If we are using less, close the output file, display with less, then delete
  if @config.use_less
    @output.close
    # [review] - Way of calling a native Ruby less?
    system("less -R #{ @config.tmp_file }")
    debug_print "File displayed with less, now deleting...\n"
    File.delete(@config.tmp_file)
  end

  return true
end