class Watson::Printer

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

Constants

DEBUG

Debug printing for this class

Public Class Methods

cprint(msg = "", color = "") click to toggle source

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

# 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
new(config) click to toggle source

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

# File lib/watson/printer.rb, line 92
def initialize(config)

        # Identify method entry
        debug_print "#{ self } : #{ __method__ }\n"
        
        @config = config
        return true
end
print_header() click to toggle source

Standard header print for static call (uses static cprint)

print_status(msg, color) click to toggle source

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

Public Instance Methods

cprint(msg = "", color = "") click to toggle source

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

# 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) click to toggle source

Individual entry printer Uses issue hash to format printed output

# 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] == false
                debug_print "No issues for #{ entry }\n"
                print_status "o", GREEN
                cprint BOLD + UNDERLINE + GREEN + "#{ entry[:relative_path] }" + RESET + "\n"
                return true
        else
                debug_print "Issues found for #{ entry }\n"
                cprint "\n"
                print_status "x", RED
                cprint BOLD + UNDERLINE + RED + "#{entry[:relative_path]}" + RESET + "\n"
        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"
                        cprint "\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
print_header() click to toggle source

Standard header print for class call (uses member cprint)

print_status(msg, color) click to toggle source

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

print_structure(structure) click to toggle source

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

run(structure) click to toggle source

Take parsed structure and print out in specified formatting

# 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')
        elsif
                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 #{ @config.tmp_file }")
                debug_print "File displayed with less, now deleting...\n"
                File.delete(@config.tmp_file)
        end

        return true
end