import sublime, sublime_plugin
import os, subprocess

class RefreshCommand(sublime_plugin.TextCommand):
    """ Refresh the project """

    def run(self, edit):
        for folder in self.view.window().folders():
            label = "REFRESH: "
            status_msg = label

            f = str(folder)
            c = "source ~/.bash_profile && "

            # jekyll rebuild: `bundle exec jekyll --no-auto --no-server`
            if os.path.exists(os.path.join(f, '_config.yml')):
                cmd = "bundle exec jekyll --no-auto --no-server"
                status_msg += "`" + cmd + "`, "
                try:
                    p = subprocess.Popen(c+cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=f, shell=True)
                    result, err = p.communicate()
                    # status_msg += err
                except Exception as e:
                    status_msg += str(e)

            # Rack restart: `touch tmp/restart.txt`
            if os.path.exists(os.path.join(f, 'config.ru')):
                cmd = "mkdir -p tmp && touch tmp/restart.txt"
                status_msg += "`" + cmd + "`, "
                try:
                    p = subprocess.Popen(c+cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=f, shell=True)
                    result, err = p.communicate()
                    # status_msg += err
                except Exception as e:
                    status_msg += str(e)

            # Summary message
            if status_msg == label:
                status_msg += "nothing needed."
            else:
                status_msg += "done."

            sublime.status_message(status_msg)

@@ -138,144 +138,267 @@
     
-def run(self, edit):%0A        for folder in self.view.window().folders():%0A            label = %22REFRESH: %22%0A            status_msg = label%0A
+status_msg = %22%22%0A    label = %22REFRESH: %22%0A%0A    def run(self, edit):%0A        self.status_msg = %22%22%0A        self.set_status(self.label)%0A        sublime.set_timeout(self.run_folders, 1)%0A%0A    def run_folders(self):%0A        for folder in self.view.window().folders():
 %0A   
@@ -682,37 +682,39 @@
            s
-tatus_msg += 
+elf.set_status(
 %22%60%22 + cmd + 
@@ -710,32 +710,33 @@
 %60%22 + cmd + %22%60, %22
+)
 %0A               
@@ -910,48 +910,8 @@
 e()%0A
-                    # status_msg += err%0A
     
@@ -966,35 +966,38 @@
        s
-tatus_msg += 
+elf.set_status(
 str(e)
+)
 %0A%0A      
@@ -1182,29 +1182,31 @@
        s
-tatus_msg += 
+elf.set_status(
 %22%60%22 + cm
@@ -1214,16 +1214,17 @@
  + %22%60, %22
+)
 %0A       
@@ -1406,48 +1406,8 @@
 e()%0A
-                    # status_msg += err%0A
     
@@ -1466,27 +1466,30 @@
    s
-tatus_msg += 
+elf.set_status(
 str(e)
+)
 %0A%0A  
@@ -1531,16 +1531,21 @@
      if 
+self.
 status_m
@@ -1550,16 +1550,21 @@
 _msg == 
+self.
 label:%0A 
@@ -1579,29 +1579,31 @@
        s
-tatus_msg += 
+elf.set_status(
 %22nothing
@@ -1611,16 +1611,17 @@
 needed.%22
+)
 %0A       
@@ -1652,34 +1652,95 @@
    s
-tatus_msg += 
+elf.set_status(
 %22done.%22
+)
 %0A%0A    
+def set_status(self, msg):%0A        self.status_msg += msg%0A
     
@@ -1766,16 +1766,21 @@
 message(
+self.
 status_m
@@ -1783,8 +1783,9 @@
 us_msg)%0A
+%0A

update refresh.py status messaging