import os
from sublime import Region
from sublime_plugin import TextCommand

from .common.json_coverage_reader import JsonCoverageReader

PANEL_NAME = 'ruby-coverage-project'

class ShowProjectRubyCoverage(TextCommand):
    """Show coverage of all files in current file's project in a panel."""

    def run(self, edit):
        self.get_project_coverage()
        self.create_output_panel()
        self.display_project_coverage(edit)

    def get_project_coverage(self):
        filename = self.view.file_name()

        r = JsonCoverageReader(filename)
        self.coverage = r.get_project_coverage() if r else None

    def create_output_panel(self):
        self.panel = self.view.window().create_output_panel(PANEL_NAME)

    def display_project_coverage(self, edit):
        panel = self.panel
        panel.set_read_only(False)
        panel.erase(edit, Region(0, panel.size()))
        panel.insert(edit, 0, self.format_project_coverage())
        panel.set_read_only(True)
        panel.show(0)

        self.view.window().run_command("show_panel", {"panel": "output.{}".format(PANEL_NAME)})

    def format_project_coverage(self):
        files = self.coverage['files']

        output = ''

        max_filename_length = len(max(files, key=lambda file: len(file['filename']))['filename'])

        for file in files:
            output += file['filename'].ljust(max_filename_length) + ' {:>5.1f}% covered\n'.format(file['covered_percent'])

        return output

@@ -1128,32 +1128,59 @@
 coverage(self):%0A
+        panel = self.panel%0A
         files = 
@@ -1224,16 +1224,92 @@
 t = ''%0A%0A
+        viewport_width = int(panel.viewport_extent()%5B0%5D / panel.em_width())%0A
         
@@ -1402,157 +1402,535 @@
 '%5D)%0A
-%0A        for file in files:%0A            output += file%5B'filename'%5D.ljust(max_filename_length) + ' %7B:%3E5.1f%7D%25 covered%5Cn'.format(file%5B'covered_percent'%5D
+        coverage_length = len('100%25')%0A        graph_width = viewport_width - max_filename_length - coverage_length - 4%0A%0A        for file in files:%0A            graph_bar_width = int(file%5B'covered_percent'%5D / 100.0 * graph_width)%0A%0A            filename = file%5B'filename'%5D.ljust(max_filename_length)%0A            graph    = ''.ljust(graph_bar_width, '%E2%96%88') + ''.ljust(graph_width - graph_bar_width)%0A            coverage = '%7B:%3E5.1f%7D%25'.format(file%5B'covered_percent'%5D)%0A%0A            output += '%7B%7D %7B%7D %7B%7D%5Cn'.format(filename, graph, coverage
 )%0A%0A 

Add bar graph to project coverage view.