0

im trying to write a sublime function that will take the current file and run it in a new shell in ConEmu, Im having some issues with escaping a \t

when I have a path that looks like

c:\ps\test.ps1

the t gets dropped, i've tried to do some things with escaping it but havent had any luck.

here is the sublime function

class ConemuPstestCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      if self.view.file_name():
         if self.view.is_dirty():
            self.view.run_command("save")

      folder = path.dirname(self.view.file_name())
      testFile =  path.join(folder, "test.ps1")
      if(not (path.exists(testFile))):
         testFile = path.join(path.split(folder)[0],"test.ps1")
      if(path.exists(testFile)):
         testFile = testFile + "\n"

         print(testFile)
         testFile = re.sub(r'\t', r'\\t', testFile)
         #testFile = testFile.translate(str.maketrans({"\t":  r"\\\t","\\": r"\\"}))
         print(testFile)
         subprocess.call([CONEMUC, "-GUIMACRO:0","Task(""{Shells::PowerShell}"")"],startupinfo=si)
         #subprocess.call([CONEMUC, "-GUIMACRO:0","Recreate(0,0,0)"],startupinfo=si)
         subprocess.call([CONEMUC, "-GUIMACRO:0", "PASTE", "2", testFile], startupinfo=si)

basically the idea is that if you hit F6 in a powershell module it will run the test.ps1 in that folder. based on both of the print(testFile) the output in the sublime console looks correct, when it gets over to powershell the t on test is missing (oddly the \ is still there)

The really odd thing is if i comment out the Task() (new powershell console) line it copies it over correctly in to the current console (same conemu task type)

Im running version 150513 [64] and sublime 3 (3083)

capture

as a simple test, I ran this bit of python code from a a conemu console

import re,subprocess
CONEMUC = "C:\\Program Files\\ConEmu\\ConEmu\\ConEmuC64.exe"

si = subprocess.STARTUPINFO() 
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE

subprocess.call([CONEMUC, "-GUIMACRO:0","Task(""{Shells::PowerShell}"");context;print(""test"")"],startupinfo=si)

This will start the console fine, but it does not print the text

Also the Python version is Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32

An even simpler test, I started a cmd shell and ran this command

ConEmuC.exe -guimacro task("{Shells::cmd}");context;print("test")

It will report back OK;OK;OK The new shell is opened and test is printed in the console i ran this from. So it looks like context isnt really doing what it should?

I just tried another simple test

ConEmuC.exe -guimacro Create(0,0);context;print("test")

This time it worked correctly. It seems like Task() and Context dont want to work together?

It seems like not really sure if this is something conemu is doing or the sublime/python side of things. any thoughts?

thanks

jrich523
  • 598
  • 1
  • 5
  • 19

1 Answers1

2

Task is asynchronous function, so you have to wait a while for new virtual console object will be created in main thread. The following command is working (checked from {cmd} tab in ConEmu).

conemuc -guimacro task("{cmd}"); sleep(1000); context; print("abc");

So, in your case it will be something like this:

subprocess.call([CONEMUC, "-GUIMACRO:0","Task(""{Shells::PowerShell}"");sleep(1000);context;print(""test"")"],startupinfo=si)

Also, here are some "guidelines":

  1. Do not call ConEmuC twice. As described in GuiMacro docs you may and actually have to execute several macros in a single ConEmuC call. Delimit functions by semicolon for example.

  2. Same docs tells about verbatim string arguments. Only doublequotes must be doubled. Paste(2,@"Your ""string""")

  3. After creating new task you have to switch execution context by Context function to be able to paste into newly created console.

  4. At last, there are functions PasteFile and Print. They may be more suitable.

Maximus
  • 10,751
  • 8
  • 47
  • 65
  • Thanks, I had tried the double quote without much luck, I'll try putting this all together. Thanks! – jrich523 Jun 05 '15 at 23:15
  • Not having much luck, it creates the shell ok, not really sure if Context is suppose to have a visible reaction, but its definitely not printing test to the console. here is what i did: subprocess.call([CONEMUC, "-GUIMACRO:0","Task(""{Shells::PowerShell}"");context;print(""test"")"],startupinfo=si) – jrich523 Jun 07 '15 at 17:29
  • ConEmu version? Screenshot? – Maximus Jun 08 '15 at 00:35
  • did a whole lot of tests (updated original post) and it seems like Task() and Context dont work well together? – jrich523 Jun 08 '15 at 17:04
  • They do actually. Perhaps PowerShell clears input queue somewhere during starting up. Add Sleep function after context, experiment with sleep duration. – Maximus Jun 08 '15 at 22:21
  • I tried it with sleep(5000) after the context with no luck. Also, i dont think its that because as i said in the updated post, a Create() intsead of a Task() works just fine. – jrich523 Jun 09 '15 at 15:24