Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending commands to individual windows #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions screenutils/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ def disable_logs(self):
def initialize(self):
"""initialize a screen, if does not exists yet"""
if not self.exists:
self._id=None
# Detach the screen once attached, on a new tread.
Thread(target=self._delayed_detach).start()
# support Unicode (-U),
# attach to a new/existing named screen (-R).
system('screen -UR ' + self.name)
self._id = None
system('screen -UdmS {0}'.format(self.name))

def interrupt(self):
"""Insert CTRL+C in the screen session"""
Expand All @@ -116,28 +112,28 @@ def kill(self):
"""Kill the screen applications then close the screen"""
self._screen_commands('quit')

def detach(self):
"""detach the screen"""
self._check_exists()
system("screen -d " + self.name)

def send_commands(self, *commands):
def send_commands(self, window=0, *commands):
"""send commands to the active gnu-screen"""
self._check_exists()
self._check_window_exists(window)
for command in commands:
self._screen_commands( 'stuff "' + command + '" ' ,
'eval "stuff \\015"' )
# HACK for -p to select window; should be part of _screen_commands as well.
# FIXME: doublequotes need to be escaped here
self._screen_commands('-p {0} stuff "{1}"'.format(window, command) , '-p {0} eval "stuff \\015"'.format(window))

def add_user_access(self, unix_user_name):
"""allow to share your session with an other unix user"""
self._screen_commands('multiuser on', 'acladd ' + unix_user_name)

def _check_window_exists(self, window):
pass

def _screen_commands(self, *commands):
"""allow to insert generic screen specific commands
a glossary of the existing screen command in `man screen`"""
self._check_exists()
for command in commands:
system('screen -x ' + self.name + ' -X ' + command)
system('screen -S {0} -X {1}'.format(self.name, command))
sleep(0.02)

def _check_exists(self, message="Error code: 404"):
Expand All @@ -156,9 +152,5 @@ def _set_screen_infos(self):
else:
self._status = infos[1][1:-1]

def _delayed_detach(self):
sleep(0.5)
self.detach()

def __repr__(self):
return "<%s '%s'>" % (self.__class__.__name__, self.name)