Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Manage commands associated with shell. 

3 

4Not fully implemented yet. 

5""" 

6import shlex 

7from shutil import which 

8from subprocess import PIPE, Popen, SubprocessError 

9from typing import Optional 

10 

11 

12class Shell: 

13 """ 

14 Run commands with subprocess. 

15 It is highly recommend to use this via `with` statement. 

16 """ 

17 

18 def __init__(self): 

19 self.subprocess: Optional[Popen] = None 

20 self.shell = "bash" if which("bash") else "cmd" 

21 

22 def __enter__(self): 

23 try: 

24 self.subprocess = Popen( 

25 self.shell, universal_newlines=True, stdin=PIPE, bufsize=1 

26 ) 

27 except SubprocessError: 

28 # TODO: exception handling required 

29 return None 

30 return self 

31 

32 def __exit__(self, type, value, traceback): 

33 self.subprocess.communicate(shlex.join(["exit"])) 

34 

35 def send(self, commands): 

36 for command in commands: 

37 self.subprocess.stdin.write(f"{command}\n")